Changed to Platform.io

This commit is contained in:
starcalc 2022-11-27 10:23:08 +01:00
parent 5c15123d00
commit 201dc4a844
14 changed files with 144 additions and 219 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View File

@ -1,42 +0,0 @@
Rotary Dial
===========
Arduino library to read numbers from a rotary dial (or otherwise using pulse dialing; see [Wikipedia: Rotary dial](http://en.wikipedia.org/wiki/Rotary_dial)).
![front of a rotary telephone dial](http://www.markfickett.com/umbrella/images/111105rotarydialfront-sm.jpg "Rotary Dial")
This implementation is for the North American system, where [1, 9] pulses correspond to the numbers [1, 9], and 0 is represented by 10 pulses. This library was written for use with the dial [demonstrated here](http://commons.wikimedia.org/wiki/File:Rotary_Dial,_Dialing_Back_with_LEDs.ogv) and pictured above.
Connection & Circuit
--------------------
On the back of the dial are two connections relevant for this implementation. One (the 'ready' switch) is normally open (NO), and is closed whenever the rotor is not at rest (specifically, as soon as the user draws back the rotor, and until it finishes returning). The other (the 'pulse' switch) is normally closed (NC), and is opened briefly for each pulse (roughly 10 - 20 Hz).
The expected circuit is:
Rotary Dial Arduino
/---------------------- readyPin
/- ready switch (NO) -- pull-up resistor -- VCC
/-- pulse switch (NC) -- pull-up resistor -/
\ \---------------------- pulsePin
\------------------------------------------ GND
The expected sequence is:
readyPin pulsePin state
HIGH n/a default (waiting)
LOW LOW ready to dial / for first pulse
LOW HIGH pulse received (number = 1)
LOW LOW ready for next pulse
LOW HIGH pulse received (number = 2)
LOW ... (repeat)
HIGH n/a rotation complete, count recorded
There is 15ms allowed for debounce, which is the implementation's only
constraint on pulse speed.
See Also
--------
See also: [USB output from the Arduino](http://www.arduino.cc/playground/Main/InterfacingWithHardware#USB), including [presenting the Arduino as a keyboard by installing new firmware](http://hunt.net.nz/users/darran/?tag=keyboard).

View File

@ -1,100 +0,0 @@
#include "RotaryDialer.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define NO_NUMBER -1
// Require DEBOUNCE_DELAY milliseconds between state changes. This value is
// noted in the class documentation.
#define DEBOUNCE_DELAY 15
RotaryDialer::RotaryDialer(int readyPin, int pulsePin) :
pinReady(readyPin), pinPulse(pulsePin), hasCompletedNumber(false),
state(WAITING)
{ }
void RotaryDialer::setup() {
pinMode(pinReady, INPUT);
pinMode(pinPulse, INPUT);
digitalWrite(pinReady, HIGH);
digitalWrite(pinPulse, HIGH);
lastStateChangeMillis = millis();
}
bool RotaryDialer::changeStateIfDebounced(enum State newState) {
unsigned long currentMillis = millis();
if (currentMillis < lastStateChangeMillis) {
// clock wrapped; ignore (but could figure it out in this case)
lastStateChangeMillis = currentMillis;
return false;
} else if (currentMillis - lastStateChangeMillis > DEBOUNCE_DELAY) {
state = newState;
lastStateChangeMillis = currentMillis;
return true;
} else {
return false;
}
}
void RotaryDialer::completeDial() {
if (!changeStateIfDebounced(WAITING)) {
return;
}
if (number > 0 && number <= 10) {
if (number == 10) {
number = 0;
}
hasCompletedNumber = true;
}
}
bool RotaryDialer::update() {
int readyStatus = digitalRead(pinReady);
int pulseStatus = digitalRead(pinPulse);
switch(state) {
case WAITING:
if (readyStatus == LOW
&& changeStateIfDebounced(LISTENING_NOPULSE))
{
hasCompletedNumber = false;
number = 0;
}
break;
case LISTENING_NOPULSE:
if (readyStatus == HIGH) {
completeDial();
} else if (pulseStatus == HIGH) {
changeStateIfDebounced(LISTENING_PULSE);
}
break;
case LISTENING_PULSE:
if (readyStatus == HIGH) {
completeDial();
} else if (pulseStatus == LOW
&& changeStateIfDebounced(LISTENING_NOPULSE))
{
number++;
}
break;
}
return hasCompletedNumber;
}
bool RotaryDialer::hasNextNumber() {
return hasCompletedNumber;
}
int RotaryDialer::getNextNumber() {
if (hasCompletedNumber) {
hasCompletedNumber = false;
return number;
} else {
return NO_NUMBER;
}
}

View File

@ -1,71 +0,0 @@
#pragma once
/**
* Read numbers from a rotary dial (or otherwise using pulse dialing; see
* http://en.wikipedia.org/wiki/Rotary_dial ).
*
* See the README for further documentation.
*/
class RotaryDialer {
private:
int pinReady;
int pinPulse;
bool hasCompletedNumber;
int number;
enum State {
WAITING,
LISTENING_NOPULSE,
LISTENING_PULSE
};
enum State state;
unsigned long lastStateChangeMillis;
/**
* Change state, but only if enough time has elapsed since
* the last state change (to protect from noise).
*/
bool changeStateIfDebounced(enum State newState);
/**
* To be called when ready returns HIGH (when the rotor returns
* to its rest position); save the number, if valid.
*/
void completeDial();
public:
/**
* Create a new RotaryDialer listening on the given pins.
* @param readyPin connected to a NO (HIGH) switch on the rotor
* which is closed (LOW) during dialing
* @param pulsePin connected to a NC (LOW) switch on the rotor
* which is opened (HIGH) during each pulse
*/
RotaryDialer(int readyPin, int pulsePin);
/**
* Initialize the pins; digital read pins, held HIGH.
*/
void setup();
/**
* Check the pins and update state (in or out of a pulse,
* dialing complete, etc). This must be called at least as
* pulses; assuming 10 pulses per second, every 50ms.
*/
bool update();
/**
* @return whether a new number has been dialed since the last
* getNextNumber call
*/
bool hasNextNumber();
/**
* Get the most recently dialed number. After this is called,
* hasNextNumber will return false until a new number is dialed.
* @return the most recently dialed number, and clear
* hasNextNumber
*/
int getNextNumber();
};

View File

@ -6,7 +6,7 @@
"password": "12345678" "password": "12345678"
}, },
"mqtt": { "mqtt": {
"host": "raum.ctdo.de", "host": "mqtt.ctdo.de",
"port": 1883, "port": 1883,
"auth": false "auth": false
}, },

39
include/README Normal file
View File

@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

46
lib/README Normal file
View File

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

20
platformio.ini Normal file
View File

@ -0,0 +1,20 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino
upload_speed = 1500000
monitor_speed = 115200
lib_deps =
bblanchon/ArduinoJson
me-no-dev/ESP Async WebServer@1.2.3
https://github.com/homieiot/homie-esp8266

View File

@ -1,7 +1,4 @@
#include <Homie.h> #include "dialtoserial.h"
#include <ArduinoOTA.h>
#include "RotaryDialer.h"
#define PIN_READY D6 #define PIN_READY D6
#define PIN_PULSE D5 #define PIN_PULSE D5
@ -10,7 +7,7 @@
#define MINMAP 0 #define MINMAP 0
#define MAXMAP 500 #define MAXMAP 500
HomieNode dialNode("dial", "dialnode"); HomieNode dialNode("dial", "dialnode", "commands");
RotaryDialer dialer = RotaryDialer(PIN_READY, PIN_PULSE); RotaryDialer dialer = RotaryDialer(PIN_READY, PIN_PULSE);
bool meterHandler(const HomieRange& range, const String& value) { bool meterHandler(const HomieRange& range, const String& value) {

10
src/dialtoserial.h Normal file
View File

@ -0,0 +1,10 @@
#include <Homie.h>
#include <ArduinoOTA.h>
#include "RotaryDialer.h"
void setMeter(int pos);
void setNumber(int pos);
void setRaw(int pos);
void loopHandler();

11
test/README Normal file
View File

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html