diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -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" + ] +} diff --git a/README.markdown b/README.markdown deleted file mode 100644 index 1e88cdb..0000000 --- a/README.markdown +++ /dev/null @@ -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). - diff --git a/RotaryDialer.cpp b/RotaryDialer.cpp deleted file mode 100644 index 902b30e..0000000 --- a/RotaryDialer.cpp +++ /dev/null @@ -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; - } -} - diff --git a/RotaryDialer.h b/RotaryDialer.h deleted file mode 100644 index ff1b7c2..0000000 --- a/RotaryDialer.h +++ /dev/null @@ -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(); -}; - diff --git a/examples/dialtoserial/data/homie/config.json b/data/homie/config.json similarity index 88% rename from examples/dialtoserial/data/homie/config.json rename to data/homie/config.json index fccfc76..4654167 100644 --- a/examples/dialtoserial/data/homie/config.json +++ b/data/homie/config.json @@ -6,7 +6,7 @@ "password": "12345678" }, "mqtt": { -"host": "raum.ctdo.de", +"host": "mqtt.ctdo.de", "port": 1883, "auth": false }, diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -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 diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -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 +#include + +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 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..ab5712a --- /dev/null +++ b/platformio.ini @@ -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 diff --git a/examples/dialtoserial/RotaryDialer.cpp b/src/RotaryDialer.cpp similarity index 100% rename from examples/dialtoserial/RotaryDialer.cpp rename to src/RotaryDialer.cpp diff --git a/examples/dialtoserial/RotaryDialer.h b/src/RotaryDialer.h similarity index 100% rename from examples/dialtoserial/RotaryDialer.h rename to src/RotaryDialer.h diff --git a/examples/dialtoserial/dialtoserial.ino b/src/dialtoserial.cpp similarity index 96% rename from examples/dialtoserial/dialtoserial.ino rename to src/dialtoserial.cpp index 9c4ae7e..d8f6743 100644 --- a/examples/dialtoserial/dialtoserial.ino +++ b/src/dialtoserial.cpp @@ -1,7 +1,4 @@ -#include -#include - -#include "RotaryDialer.h" +#include "dialtoserial.h" #define PIN_READY D6 #define PIN_PULSE D5 @@ -10,7 +7,7 @@ #define MINMAP 0 #define MAXMAP 500 -HomieNode dialNode("dial", "dialnode"); +HomieNode dialNode("dial", "dialnode", "commands"); RotaryDialer dialer = RotaryDialer(PIN_READY, PIN_PULSE); bool meterHandler(const HomieRange& range, const String& value) { diff --git a/src/dialtoserial.h b/src/dialtoserial.h new file mode 100644 index 0000000..53a2041 --- /dev/null +++ b/src/dialtoserial.h @@ -0,0 +1,10 @@ +#include +#include + +#include "RotaryDialer.h" + + +void setMeter(int pos); +void setNumber(int pos); +void setRaw(int pos); +void loopHandler(); \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -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