From b1b268671408692ec6aef7a2415dc0df08b896b2 Mon Sep 17 00:00:00 2001 From: DELL4 Date: Thu, 19 Jan 2023 02:31:31 +0100 Subject: [PATCH] initial commit --- .cproject | 1693 ++++++++++++++++++++++++++ .gitignore | 1 + .project | 27 + .settings/PlatformIO Debugger.launch | 38 + .settings/language.settings.xml | 25 + .settings/org.eclipse.cdt.core.prefs | 11 + include/README | 39 + lib/README | 46 + platformio.ini | 21 + src/main.cpp | 159 +++ test/README | 11 + 11 files changed, 2071 insertions(+) create mode 100644 .cproject create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/PlatformIO Debugger.launch create mode 100644 .settings/language.settings.xml create mode 100644 .settings/org.eclipse.cdt.core.prefs create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..2857b54 --- /dev/null +++ b/.cproject @@ -0,0 +1,1693 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + platformio + -f -c eclipse + run --target program + true + false + false + + + platformio + -f -c eclipse + run --target uploadfs + true + false + false + + + platformio + -f -c eclipse + run + true + false + false + + + platformio + -f -c eclipse + run --verbose + true + false + false + + + platformio + -f -c eclipse + run --target upload + true + false + false + + + platformio + -f -c eclipse + run --target upload --verbose + true + false + false + + + platformio + -f -c eclipse + run --target clean + true + false + false + + + platformio + -f -c eclipse + test + true + false + false + + + platformio + -f -c eclipse + remote run --target upload + true + false + false + + + platformio + -f -c eclipse + init --ide eclipse + true + false + false + + + platformio + -f -c eclipse + device list + true + false + false + + + platformio + -f -c eclipse + lib update + true + false + false + + + platformio + -f -c eclipse + update + true + false + false + + + platformio + -f -c eclipse + upgrade + true + false + false + + + + \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fad476a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.pio \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..179c348 --- /dev/null +++ b/.project @@ -0,0 +1,27 @@ + + + cwkeyer + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/.settings/PlatformIO Debugger.launch b/.settings/PlatformIO Debugger.launch new file mode 100644 index 0000000..6f6b34f --- /dev/null +++ b/.settings/PlatformIO Debugger.launch @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml new file mode 100644 index 0000000..80e9a50 --- /dev/null +++ b/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.settings/org.eclipse.cdt.core.prefs b/.settings/org.eclipse.cdt.core.prefs new file mode 100644 index 0000000..adcb9fa --- /dev/null +++ b/.settings/org.eclipse.cdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +environment/project/0.910961921/PATH/delimiter=\: +environment/project/0.910961921/PATH/operation=replace +environment/project/0.910961921/PATH/value=/usr/local/bin\:/usr/bin\:/bin\:/usr/local/sbin\:/usr/lib/jvm/default/bin\:/usr/bin/site_perl\:/usr/bin/vendor_perl\:/usr/bin/core_perl\:/usr/local/bin\:/usr/bin\:/bin\:/usr/local/sbin\:/usr/lib/jvm/default/bin\:/usr/bin/site_perl\:/usr/bin/vendor_perl\:/usr/bin/core_perl${PathDelimiter}${PATH} +environment/project/0.910961921/append=true +environment/project/0.910961921/appendContributed=true +environment/project/0.910961921.1363900502/PATH/delimiter=\: +environment/project/0.910961921.1363900502/PATH/operation=replace +environment/project/0.910961921.1363900502/PATH/value=/usr/local/bin\:/usr/bin\:/bin\:/usr/local/sbin\:/usr/lib/jvm/default/bin\:/usr/bin/site_perl\:/usr/bin/vendor_perl\:/usr/bin/core_perl\:/usr/local/bin\:/usr/bin\:/bin\:/usr/local/sbin\:/usr/lib/jvm/default/bin\:/usr/bin/site_perl\:/usr/bin/vendor_perl\:/usr/bin/core_perl${PathDelimiter}${PATH} +environment/project/0.910961921.1363900502/append=true +environment/project/0.910961921.1363900502/appendContributed=true \ No newline at end of file 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..a7c3a6e --- /dev/null +++ b/platformio.ini @@ -0,0 +1,21 @@ +; 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:esp32doit-devkit-v1] +platform = espressif32 +framework = arduino +board = esp32doit-devkit-v1 +upload_protocol = esptool +upload_speed = 921600 +board_build.flash_mode = dio +build_flags = -DCORE_DEBUG_LEVEL=4 +lib_deps = + yellobyte/DacESP32@^1.0.10 + madhephaestus/ESP32Encoder@^0.10.1 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..2049d29 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,159 @@ +#include +#include +#include + +#define PIN_L 4 // pin GPIO T4 = 13 +#define PIN_R 15 // pin GPIO T5 = 12 + +#define MODE 0 // set 1 for ESP32-S2 and -S3 (higher touchRead value = contact) + +enum cw_touchmode_t { + TOUCH, + PADDLE +} cw_touchmode = PADDLE; + +uint8_t thresh_L = 10; +uint8_t thresh_R = 10; + +DacESP32 dac1(DAC_CHANNEL_1); // GPIO 25 + +enum cw_element_t { + IDLE, + DIT, + DAH, + DELAY, +}; + +cw_element_t prevEl = IDLE; +cw_element_t currEl = IDLE; +cw_element_t nextEl = IDLE; +int currElEndTime = 0; + +int wpm = 20; + + +ESP32Encoder encoder; + +void setup() +{ + if (cw_touchmode == TOUCH) { + thresh_L = (int) ((float)touchRead(PIN_L) * 0.9f); + thresh_R = (int) ((float)touchRead(PIN_R) * 0.9f); + } else if (cw_touchmode == PADDLE) { + pinMode(PIN_L, INPUT_PULLUP); + pinMode(PIN_R, INPUT_PULLUP); + } + + pinMode(LED_BUILTIN, OUTPUT); + dac1.outputCW(1000); + dac1.dacCwDeselect(); + + ESP32Encoder::useInternalWeakPullResistors=UP; + encoder.attachHalfQuad(16, 17); + encoder.setCount(wpm * 2); +} + +void toneStart() { + digitalWrite(LED_BUILTIN, HIGH); + dac1.dacCwSelect(); +} + +void toneStop() { + digitalWrite(LED_BUILTIN, LOW); + dac1.dacCwDeselect(); +} + +void loop() +{ + auto ditLength = 1200 / wpm; + auto time = millis(); + + long newWpm = encoder.getCount() / 2; + if (newWpm != wpm) { + if (newWpm > 3) { + wpm = newWpm; + log_d("Changing WPM to %d\n", wpm); + } + } + + bool holding_L, holding_R; + if (cw_touchmode == TOUCH) { + auto val_L = touchRead(PIN_L); + auto val_R = touchRead(PIN_R); + + holding_L = (val_L < thresh_L); + holding_R = (val_R < thresh_R); + } else if (cw_touchmode == PADDLE) { + holding_L = !digitalRead(PIN_L); + holding_R = !digitalRead(PIN_R); + } + holding_L ^= MODE; + holding_R ^= MODE; + // in MODE 0: L = dit, R = dah + + // log_v("prev: %d, curr: %d, next: %d, L: %d (trig %d) => %d | R: %d (trig %d) => %d @ %d / %d\n", prevEl, currEl, nextEl, val_L, thresh_L, holding_L, val_R, thresh_R, holding_R, time, currElEndTime); + + switch (currEl) { + case IDLE: + if (holding_L && !holding_R) { + currEl = DIT; + currElEndTime = time + ditLength; + } else if (!holding_L && holding_R) { + currEl = DAH; + currElEndTime = time + 3 * ditLength; + } else if (holding_L && holding_R && nextEl == IDLE) { // both pressed at the same time + currEl = DIT; + nextEl = DAH; + currElEndTime = time + 3 * ditLength; + } + toneStop(); + break; + case DIT: + if (holding_R && nextEl == IDLE) { + log_v("holding R, queuing DAH\n"); + nextEl = DAH; + } + if (time >= currElEndTime) { + currEl = DELAY; + currElEndTime = time + ditLength; + } + toneStart(); + break; + + case DAH: + if (holding_L && nextEl == IDLE) { + log_v("holding L, queuing DIT\n"); + nextEl = DIT; + } + if (time >= currElEndTime) { + currEl = DELAY; + currElEndTime = time + ditLength; + } + toneStart(); + break; + case DELAY: + if (time >= currElEndTime) { // delay ended + log_v("delay ended, curr: %d, next: %d\n", currEl, nextEl); + currEl = nextEl; // play the next element + if (currEl == DIT) { + log_v("playing DIT next\n"); + currElEndTime = time + ditLength; + } else if (currEl == DAH) { + log_v("playing DAH next\n"); + currElEndTime = time + 3 * ditLength; + } + nextEl = IDLE; + } + if (prevEl == DIT && holding_R && nextEl == IDLE) { + nextEl = DAH; + } else if (prevEl == DAH && holding_L && nextEl == IDLE) { + nextEl = DIT; + } + toneStop(); + break; + } + + prevEl = currEl; + + +} 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