From 5acab6eac7bee455bc65c1506d3c93b4774103cf Mon Sep 17 00:00:00 2001 From: gsd Date: Sat, 15 Nov 2025 01:49:21 +0300 Subject: [PATCH] io firmware with encoder support --- .vscode/settings.json | 5 +++ pipboyIO/encoder1.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++ pipboyIO/encoder1.h | 4 +++ pipboyIO/pipboyIO.ino | 35 +++++++----------- pipboyIO/readme | 5 ++- 5 files changed, 107 insertions(+), 24 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 pipboyIO/encoder1.cpp create mode 100644 pipboyIO/encoder1.h diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..af13e3a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "cstdint": "cpp" + } +} \ No newline at end of file diff --git a/pipboyIO/encoder1.cpp b/pipboyIO/encoder1.cpp new file mode 100644 index 0000000..76b6efa --- /dev/null +++ b/pipboyIO/encoder1.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include "encoder1.h" + +#define PinA 27 // контат AB +#define PinC 14 // контат BC +//#define PinS 5 // кнопка + +#define encoder1 + +uint8_t pinA = 0; // состояние контата AB +uint8_t pinC = 0; // состояние контата BC + +uint8_t blockA = false; // блокировка прерывания на контакте AB +uint8_t blockC = false; // блокировка прерывания на контакте BC + +uint8_t encoderResult = 0; // Результирующая переменная +int16_t encoderCount = 0; // Счетчик щелчков энкодера +int16_t oldencoderCount = 0; // Счетчик старое значение + +int16_t getEncoderCount() { + return encoderCount; +} + +int16_t getOldencoderCount() { + return oldencoderCount; +} + +void setOldencoderCount(int16_t val) { + oldencoderCount = val; +} + + +void encoderFilter () { + encoderResult = encoderResult & B00001111; + if (encoderResult == B1011) encoderCount ++; // по часовой стрелке + if (encoderResult == B0111) encoderCount --; // против часосовой стрелки + } + +void pinACHANGE () { // Изменилось состояние контакта АB + if (blockA == true) return; + pinA = !digitalRead(PinA); + pinC = !digitalRead(PinC); + encoderResult <<= 1; + bitWrite(encoderResult, 0, pinA); + encoderResult <<= 1; + bitWrite(encoderResult, 0, pinC); + encoderFilter(); + if (!pinA && !pinC) blockA = false; else blockA = true; + blockC = false; +} + +void pinCCHANGE () { // Изменилось состояние контакта BC + if (blockC == true) return; + pinA = !digitalRead(PinA); + pinC = !digitalRead(PinC); + encoderResult <<= 1; + bitWrite(encoderResult, 0, pinA); + encoderResult <<= 1; + bitWrite(encoderResult, 0, pinC); + encoderFilter(); + if (!pinA && !pinC) blockC = false; else blockC = true; + blockA = false; +} + +void setupEncoder1() { + pinMode (PinA, INPUT_PULLUP); // Как вход и внутренняя подтяжка + pinMode (PinC, INPUT_PULLUP); // Как вход и внутренняя подтяжка + attachInterrupt(digitalPinToInterrupt(PinA), pinACHANGE, CHANGE); + attachInterrupt(digitalPinToInterrupt(PinC), pinCCHANGE, CHANGE); +} + +/*void loop() { + + if (oldencoderCount != encoderCount) { + Serial.println (encoderCount); + oldencoderCount = encoderCount; + } + +}*/ diff --git a/pipboyIO/encoder1.h b/pipboyIO/encoder1.h new file mode 100644 index 0000000..a5bf747 --- /dev/null +++ b/pipboyIO/encoder1.h @@ -0,0 +1,4 @@ +void setupEncoder1(); +int16_t getEncoderCount(); +int16_t getOldencoderCount(); +void setOldencoderCount(int16_t val); \ No newline at end of file diff --git a/pipboyIO/pipboyIO.ino b/pipboyIO/pipboyIO.ino index da250bb..fa48405 100644 --- a/pipboyIO/pipboyIO.ino +++ b/pipboyIO/pipboyIO.ino @@ -1,18 +1,18 @@ -#include +#include "encoder1.h" const int input[] = {36, 39, 34 ,35 ,32 ,33 ,25 ,26}; long inputValue[] = {0, 0, 0, 0, 0, 0, 0, 0}; const int numPins = sizeof(input) / sizeof(input[0]); -Encoder enc1(14,27); -Encoder enc2(13,12); -long positions[3] = {0, 0, 0}; - #define RXD2 (16) #define TXD2 (17) void setup() { - //Serial.begin(115200); + Serial.begin(115200); + //#ifdef encoder1 + setupEncoder1(); + //#endif + Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); analogReadResolution(12); analogSetAttenuation(ADC_11db); @@ -26,14 +26,6 @@ void values2uart() { Serial2.print(inputValue[i]); Serial2.print("\n"); } - - for (int i = 0; i < 2; i++) { - Serial2.print("10"); - Serial2.print(i); - Serial2.print("-"); - Serial2.print(positions[i]); - Serial2.print("\n"); - } } void loop() { @@ -41,16 +33,13 @@ void loop() { inputValue[i] = map(analogRead(input[i]), 0, 4096, 0, 25); } - long newPos1 = enc1.read(); - long newPos2 = enc2.read(); - - if (newPos1 != positions[0]) { - positions[0] = newPos1; - } - - if (newPos2 != positions[1]) { - positions[1] = newPos2; + //#ifdef encoder1 + // Serial.print(0); + if (getOldencoderCount() != getEncoderCount()) { + Serial.println (getEncoderCount()); + setOldencoderCount(getEncoderCount()); } + //#endif values2uart(); delay(50); diff --git a/pipboyIO/readme b/pipboyIO/readme index a9e348e..7ffdca7 100644 --- a/pipboyIO/readme +++ b/pipboyIO/readme @@ -1,4 +1,7 @@ sudo apt install xdotool export DISPLAY=:0 xdotool key Return -... \ No newline at end of file +... + +board esp32 wrover kit (all version) +disable bt+wifi \ No newline at end of file