From 50c02765c860ac3baa05b95ca39ac930857fe2b3 Mon Sep 17 00:00:00 2001 From: wapophis Date: Sun, 17 Mar 2024 20:49:15 +0100 Subject: [PATCH 01/25] Added I2C sending as serial --- src/ArqSerial.h | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/ArqSerial.h b/src/ArqSerial.h index 1523799..7ffece3 100644 --- a/src/ArqSerial.h +++ b/src/ArqSerial.h @@ -4,9 +4,9 @@ #ifndef StreamRead #define StreamRead Serial.read -#define StreamFlush Serial.flush -#define StreamWrite Serial.write -#define StreamPrint Serial.print +#define StreamFlush Serial1.flush +#define StreamWrite Serial1.write +#define StreamPrint Serial1.print #define StreamAvailable Serial.available #endif @@ -39,6 +39,7 @@ class ARQSerial do { if (idleFunction != 0) idleFunction(true); c = StreamRead(); + if (c >= 0) { #ifdef TESTFAIL testfailidx = (testfailidx + 1) % 5000; @@ -61,6 +62,7 @@ class ARQSerial while (StreamAvailable() > 0) { header = Arq_TimedRead(); //DebugPrintLn("hello1"); + currentCrc = 0; if (header == 0x01) { @@ -146,6 +148,7 @@ class ARQSerial StreamWrite(0x03); StreamWrite(packetId); StreamFlush(); + Serial.printf("\nSendAcq[start,packetId]: [0x03,%d,%d]",packetId); } void SendNAcq(uint8_t lastKnownValidPacket, byte reason) @@ -154,6 +157,7 @@ class ARQSerial StreamWrite(lastKnownValidPacket); StreamWrite(reason); StreamFlush(); + Serial.printf("\nSendNAcq[start,lastKnownValidPacket,reason]: [0x04,%d,%d]",lastKnownValidPacket,reason); } public: @@ -166,15 +170,33 @@ class ARQSerial StreamWrite(0x09); StreamWrite(packetType); StreamWrite(length); + Serial.printf("\nCustomPacketStart[start,packetType,length]: [0x09,%d,%d]",packetType,length); + } + + void I2CustomPacketStart(byte packetType, uint8_t length) { + Wire.write(0x09); + Wire.write(packetType); + Wire.write(length); + Serial.printf("\nI2CustomPacketStart[start,packetType,length]: [0x09,%d,%d]",packetType,length); } void CustomPacketSendByte(byte data) { StreamWrite(data); + Serial.printf("\nCustomPacketSendByte[data]: [%d]",data); + } + + void I2CustomPacketSendByte(byte data) { + Wire.write(data); + Serial.printf("\nI2CustomPacketSendByte[data]: [%d]",data); } + void CustomPacketEnd() { //Serial.write(0x00); } + void I2CustomPacketEnd() { + //Serial.write(0x00); + } int read() { unsigned long fsr_startMillis = millis(); @@ -184,6 +206,7 @@ class ARQSerial if (DataBuffer.size() > 0) { uint8_t res = 0; DataBuffer.pop(res); + Serial1.printf("\nread[data]: [%d]",res); return (int)res; } @@ -195,8 +218,10 @@ class ARQSerial } int Available() { + //Serial1.printf("\nChecking Available darta\n"); if (idleFunction != 0) idleFunction(false); if (DataBuffer.size() == 0) { + ProcessIncomingData(); } return DataBuffer.size(); @@ -206,6 +231,7 @@ class ARQSerial StreamWrite(0x08); StreamWrite(data); StreamFlush(); + Serial1.printf("\nWrite[start,data]: [0x08,%d]",data); } void Print(char data) From 901727704ec875259ba3b391b4bae937294418b6 Mon Sep 17 00:00:00 2001 From: wapophis Date: Sun, 17 Mar 2024 21:05:42 +0100 Subject: [PATCH 02/25] Added main byppass to serial function, As example only buttonStatusChangedEvents are resended to IC2 with the same serial protocol. --- src/main.cpp | 147 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 137 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 72fc438..f19f9ac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include // No longer have to define whether it's an ESP32 or ESP8266, just do an initial compilation and // VSCode will pick up the right environment from platformio.ini @@ -8,6 +9,14 @@ // Less secure if you plan to commit or share your files, but saves a bunch of memory. // If you hardcode credentials the device will only work in your network #define USE_HARDCODED_CREDENTIALS false +#define IC2_SERIAL_BYPASS true + +#if IC2_SERIAL_BYPASS + #define WIRE Wire + #define IC2_MASTER true + #define IC2_SLAVE false + #define IC2_ADDRESS 0x08 +#endif #if INCLUDE_WIFI #if USE_HARDCODED_CREDENTIALS @@ -34,7 +43,7 @@ FullLoopbackStream incomingStream; //#define INCLUDE_RGB_LEDS_NEOPIXELBUS // use this instead of INCLUDE_WS2812B //#define INCLUDE_WS2812B // consider using INCLUDE_RGB_LEDS_NEOPIXELBUS {"Name":"INCLUDE_WS2812B","Type":"autodefine","Condition":"[WS2812B_RGBLEDCOUNT]>0"} //#define INCLUDE_WS2812B_MATRIX // consider using INCLUDE_WS2812B_MATRIX {"Name":"INCLUDE_WS2812B_MATRIX","Type":"autodefine","Condition":"[WS2812B_MATRIX_ENABLED]>0"} -//#define INCLUDE_BUTTONS //{"Name":"INCLUDE_BUTTONS","Type":"autodefine","Condition":"[ENABLED_BUTTONS_COUNT]>0","IsInput":true} +#define INCLUDE_BUTTONS //{"Name":"INCLUDE_BUTTONS","Type":"autodefine","Condition":"[ENABLED_BUTTONS_COUNT]>0","IsInput":true} //#define INCLUDE_BUTTONMATRIX //{"Name":"INCLUDE_BUTTONMATRIX","Type":"autodefine","Condition":"[ENABLED_BUTTONMATRIX]>0","IsInput":true} //#define INCLUDE_TM1637 //{"Name":"INCLUDE_TM1637","Type":"autodefine","Condition":"[TM1637_ENABLEDMODULES]>0"} //#define INCLUDE_TM1638 //{"Name":"INCLUDE_TM1638","Type":"autodefine","Condition":"[TM1638_ENABLEDMODULES]>0"} @@ -543,10 +552,10 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, // ----------------------- ADDITIONAL BUTTONS --------------------------------------------------------------- // https://github.com/zegreatclan/SimHub/wiki/Arduino-Press-Buttons // ---------------------------------------------------------------------------------------------------------- -#define ENABLED_BUTTONS_COUNT 0 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} +#define ENABLED_BUTTONS_COUNT 1 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} #ifdef INCLUDE_BUTTONS -#define BUTTON_PIN_1 3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} +#define BUTTON_PIN_1 D3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} #define BUTTON_WIRINGMODE_1 0 //{"Name":"BUTTON_WIRINGMODE_1","Title":"1'st Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=1","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_1 0 //{"Name":"BUTTON_LOGICMODE_1","Title":"1'st Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=1","ListValues":"0,Normal;1,Reversed"} @@ -991,6 +1000,7 @@ unsigned long lastMatrixRefresh = 0; void idle(bool critical) { + //Serial.printf("\n [%d] Ejecutando la función idle %d",millis(),critical); #if INCLUDE_WIFI yield(); ECrowneWifi::flush(); @@ -1082,16 +1092,49 @@ void EncoderPositionChanged(int encoderId, int position, byte direction) { #endif } #endif +uint8_t endWireTransmission(bool stop){ + uint8_t error=Wire.endTransmission(stop); + if(error=0){ + Serial.printf("\n Correct wire close \n"); + } + if(error==1){ + Serial.printf("\n Wire buffer exausted \n"); + } + if(error==2){ + Serial.printf("\n received NACK on transmit of address.\n"); + } + if(error==3){ + Serial.printf("received NACK on transmit of data."); + } + if(error==4){ + Serial.printf(" other error."); + } + if(error==5){ + Serial.printf("timeout"); + } + Serial.flush(); + return error; +} void buttonStatusChanged(int buttonId, byte Status) { #ifdef INCLUDE_GAMEPAD Joystick.setButton(TM1638_ENABLEDMODULES * 8 + buttonId - 1, Status); Joystick.sendState(); #else - arqserial.CustomPacketStart(0x03, 2); - arqserial.CustomPacketSendByte(buttonId); - arqserial.CustomPacketSendByte(Status); - arqserial.CustomPacketEnd(); + #if IC2_SERIAL_BYPASS + Wire.beginTransmission(0x08); /* begin with device address 8 */ + arqserial.I2CustomPacketStart(0x03,2); + arqserial.I2CustomPacketSendByte(buttonId); + arqserial.I2CustomPacketSendByte(Status); + arqserial.I2CustomPacketEnd(); + endWireTransmission(true); + #else + arqserial.CustomPacketStart(0x03,2); + arqserial.CustomPacketSendByte(buttonId); + arqserial.CustomPacketSendByte(Status); + arqserial.CustomPacketEnd(); + #endif + #endif } @@ -1109,6 +1152,76 @@ void buttonMatrixStatusChanged(int buttonId, byte Status) { } #endif + +void resendToSerialFromMasterDevice(size_t howManyChars){ + // FlowSerialDebugPrintLn("Received data"); + //int recvBytes=0; + +// int buttonId=-1; + //byte buttonStatus=0; + + while (0 0) { @@ -1408,7 +1534,7 @@ void loop() { lastSerialActivity = millis(); // Read command loop_opt = FlowSerialTimedRead(); - + switch(loop_opt) { case '1': Command_Hello(); break; case '8': Command_SetBaudrate(); break; @@ -1450,3 +1576,4 @@ void loop() { Command_Shutdown(); } } + From b1ef41339fa2ccc27d198bfebe731844e740d69e Mon Sep 17 00:00:00 2001 From: wapophis Date: Wed, 20 Mar 2024 01:11:20 +0100 Subject: [PATCH 03/25] Updated Approach. Uses I2CTransport.h which uses the I2CSerialBridge (basic impl ) and a buffer. --- .../I2CSerialBridge/I2CSerialBridge.h | 56 ++++++ lib/EspSimHub/I2CSerialBridge/I2CTransport.h | 180 ++++++++++++++++++ src/ArqSerial.h | 32 ++-- src/main.cpp | 158 ++++++++------- 4 files changed, 341 insertions(+), 85 deletions(-) create mode 100644 lib/EspSimHub/I2CSerialBridge/I2CSerialBridge.h create mode 100644 lib/EspSimHub/I2CSerialBridge/I2CTransport.h diff --git a/lib/EspSimHub/I2CSerialBridge/I2CSerialBridge.h b/lib/EspSimHub/I2CSerialBridge/I2CSerialBridge.h new file mode 100644 index 0000000..b4ef1af --- /dev/null +++ b/lib/EspSimHub/I2CSerialBridge/I2CSerialBridge.h @@ -0,0 +1,56 @@ +#include +#include +class I2CSerialBridge{ + FullLoopbackStream *outgoingStream; + uint8_t address; + public: + + I2CSerialBridge(uint8_t address) { + this->address=address; + // CONFIG AS MASTER OR SLAVE WITH ADDRESS + } + + void setup(FullLoopbackStream *outgoingStream){ + this->outgoingStream = outgoingStream; + } + + void loop() { + // put your main code here, to run repeatedly + check_status(); + this->flush(); + } + + void flush() { + // if there is data available in the stream, it's meant + // to go from Serial port to the slave + size_t availableLength = this->outgoingStream->available(); + if (availableLength) + { +#if DEBUG_TCP_BRIDGE + Serial.printf("flushing with this much data: %d \n", availableLength); +#endif + + + // read the available data from the stream, and put in in the buffer + char sbuf[availableLength]; + this->outgoingStream->readBytes(sbuf, availableLength); + + // send data to client + Wire.beginTransmission(address); + size_t total = Wire.write(sbuf, availableLength); + Wire.endTransmission(); +#if DEBUG_TCP_BRIDGE + Serial.printf("\n ---> data sent to client %s: %d bytes \n", client->remoteIP().toString().c_str(), total); + Serial.printf("%d %d\n",sbuf[0], sbuf[1]); +#endif + } + } + + + private: + /** TODO CONTROL THAT TRANSPORT LAYER IS READY AND CONNECTED*/ + void check_status(){ + + } + +}; \ No newline at end of file diff --git a/lib/EspSimHub/I2CSerialBridge/I2CTransport.h b/lib/EspSimHub/I2CSerialBridge/I2CTransport.h new file mode 100644 index 0000000..20549ae --- /dev/null +++ b/lib/EspSimHub/I2CSerialBridge/I2CTransport.h @@ -0,0 +1,180 @@ +#include +#include +#include "I2CSerialBridge.h" + +#ifndef WIRE + # define WIRE Wire; +#endif + +#ifndef IC2_MASTER && IC2_SLAVE + #define IC2_MASTER +#endif + +#ifndef IC2_ADDRESS + #define IC2_ADDRESS 0x08 +#endif +#define StreamRead WIRE.read +// #if IC2_MASTER +// ic2SetupMaster(); +// #endif +// #if IC2_SLAVE +// ic2SetupSlave(); +// #endif + +I2CSerialBridge instance(0x08); + +class IC2Transport { + public: + static void setup(FullLoopbackStream *outgoingStream){ + instance.setup(outgoingStream); + } + + static void loop() { + instance.loop(); + } + static void flush() { + instance.flush(); + } + + // static size_t write(const uint8_t *buffer, size_t size){ + // Wire.beginTransmission(IC2_ADDRESS); + // Wire.write(buffer,size); + // return Wire.endTransmission(); + // } + // static size_t write(const char *buffer, size_t size){ + // Wire.beginTransmission(IC2_ADDRESS); + // Wire.write(buffer,size); + // return Wire.endTransmission(); + // } + // static size_t write(const char *str){ + // Wire.beginTransmission(IC2_ADDRESS); + // Wire.write(str); + // return Wire.endTransmission(); + // } + // static size_t write(uint8_t data){ + // Wire.beginTransmission(IC2_ADDRESS); + // Wire.write(data); + // return Wire.endTransmission(); + // } + +}; + + + +#define StreamAvailable WIRE.available +#define FlowSerialFlush WIRE.flush +#define StreamFlush WIRE.flush +#define StreamWrite outgoingStream.write +#define StreamPrint WIRE.print + +// size_t I2CWrite(u_int8_t data){ +// size_t oVal=0; +// Wire.beginTransmission(IC2_ADDRESS); +// oVal= WIRE.write(data); +// endWireTransmission(true); +// return oVal; +// } + +// size_t I2CWrite(const uint8_t *data, size_t size){ +// size_t oVal=0; +// Wire.beginTransmission(IC2_ADDRESS); +// oVal= WIRE.write(data,size); +// endWireTransmission(true); +// return oVal; +// } + + + +uint8_t endWireTransmission(bool stop){ + uint8_t error=Wire.endTransmission(stop); + if(error=0){ + Serial.printf("\n Correct wire close \n"); + } + if(error==1){ + Serial.printf("\n Wire buffer exausted \n"); + } + if(error==2){ + Serial.printf("\n received NACK on transmit of address.\n"); + } + if(error==3){ + Serial.printf("received NACK on transmit of data."); + } + if(error==4){ + Serial.printf(" other error."); + } + if(error==5){ + Serial.printf("timeout"); + } + Serial.flush(); + return error; + +} + +bool isSlaveAvailable(){ + Wire.beginTransmission(IC2_ADDRESS); + uint8_t error = endWireTransmission(true); + + if(error==0){ + Serial.printf("\n Slave device detected at address 8\n"); + return true; + } + return false; +} + +/** SETUP SERIAL BYPASS IC2 MASTER, USE WHEN THIS DEVICE COMMAND THE SENDING WORKFLOW*/ +void ic2SetupMaster(){ + WIRE.begin(); + while(!isSlaveAvailable()){ + Serial.printf("\n Slave device not available, retrying 1 sec later"); + delay(1000); + }; +} + + +void resendToSerialFromMasterDevice(size_t howManyChars){ + // FlowSerialDebugPrintLn("Received data"); + //int recvBytes=0; + +// int buttonId=-1; + //byte buttonStatus=0; + + while (0 #include -#include + // No longer have to define whether it's an ESP32 or ESP8266, just do an initial compilation and // VSCode will pick up the right environment from platformio.ini @@ -12,10 +12,14 @@ #define IC2_SERIAL_BYPASS true #if IC2_SERIAL_BYPASS - #define WIRE Wire + #define WIRE Wire #define IC2_MASTER true #define IC2_SLAVE false #define IC2_ADDRESS 0x08 + #include "I2CSerialBridge/I2CTransport.h" + + FullLoopbackStream outgoingStream; + #endif #if INCLUDE_WIFI @@ -116,6 +120,9 @@ FullLoopbackStream incomingStream; #include "SHDebouncer.h" #include "SHButton.h" + + + // ----------------------------------------------------- HW SETTINGS, PLEASE REVIEW ALL ------------------------------------------- #define ENABLE_MICRO_GAMEPAD 0 //{"Group":"GAMEPAD","Name":"ENABLE_MICRO_GAMEPAD","Title":"Enable arduino micro gamepad output for all the activated buttons/encoders","DefaultValue":"0","Type":"bool"} @@ -1005,6 +1012,10 @@ void idle(bool critical) { yield(); ECrowneWifi::flush(); #endif +#if IC2_SERIAL_BYPASS + yield(); + IC2Transport::flush(); +#endif #if(GAMEPAD_AXIS_01_ENABLED == 1) SHGAMEPADAXIS01.read(); @@ -1092,7 +1103,7 @@ void EncoderPositionChanged(int encoderId, int position, byte direction) { #endif } #endif -uint8_t endWireTransmission(bool stop){ +/*uint8_t endWireTransmission(bool stop){ uint8_t error=Wire.endTransmission(stop); if(error=0){ Serial.printf("\n Correct wire close \n"); @@ -1114,26 +1125,25 @@ uint8_t endWireTransmission(bool stop){ } Serial.flush(); return error; - -} +*/ void buttonStatusChanged(int buttonId, byte Status) { #ifdef INCLUDE_GAMEPAD Joystick.setButton(TM1638_ENABLEDMODULES * 8 + buttonId - 1, Status); Joystick.sendState(); #else - #if IC2_SERIAL_BYPASS - Wire.beginTransmission(0x08); /* begin with device address 8 */ - arqserial.I2CustomPacketStart(0x03,2); - arqserial.I2CustomPacketSendByte(buttonId); - arqserial.I2CustomPacketSendByte(Status); - arqserial.I2CustomPacketEnd(); - endWireTransmission(true); - #else + // #if IC2_SERIAL_BYPASS + // Wire.beginTransmission(0x08); /* begin with device address 8 */ + // arqserial.I2CustomPacketStart(0x03,2); + // arqserial.I2CustomPacketSendByte(buttonId); + // arqserial.I2CustomPacketSendByte(Status); + // arqserial.I2CustomPacketEnd(); + // endWireTransmission(true); + // #else arqserial.CustomPacketStart(0x03,2); arqserial.CustomPacketSendByte(buttonId); arqserial.CustomPacketSendByte(Status); arqserial.CustomPacketEnd(); - #endif +// #endif #endif } @@ -1153,69 +1163,73 @@ void buttonMatrixStatusChanged(int buttonId, byte Status) { #endif -void resendToSerialFromMasterDevice(size_t howManyChars){ - // FlowSerialDebugPrintLn("Received data"); - //int recvBytes=0; - -// int buttonId=-1; - //byte buttonStatus=0; - - while (0 Date: Wed, 20 Mar 2024 14:53:12 +0100 Subject: [PATCH 04/25] Enabling slave bypass to serial and refactoring the I2C Transport layer. --- lib/EspSimHub/I2CSerialBridge/I2CManager.h | 66 +++++++ .../I2CSerialBridge/I2CSeriaBypass.h | 35 ++++ .../I2CSerialBridge/I2CSerialBridge.h | 81 +++++++-- lib/EspSimHub/I2CSerialBridge/I2CTransport.h | 165 +----------------- .../I2CSerialBridge/IC2TransportMaster.h | 69 ++++++++ src/main.cpp | 15 +- 6 files changed, 251 insertions(+), 180 deletions(-) create mode 100644 lib/EspSimHub/I2CSerialBridge/I2CManager.h create mode 100644 lib/EspSimHub/I2CSerialBridge/I2CSeriaBypass.h create mode 100644 lib/EspSimHub/I2CSerialBridge/IC2TransportMaster.h diff --git a/lib/EspSimHub/I2CSerialBridge/I2CManager.h b/lib/EspSimHub/I2CSerialBridge/I2CManager.h new file mode 100644 index 0000000..9b4f604 --- /dev/null +++ b/lib/EspSimHub/I2CSerialBridge/I2CManager.h @@ -0,0 +1,66 @@ +#pragma once +#include "IC2TransportMaster.h" +#include "I2CSeriaBypass.h" + +/*** + * "This Manager is responsible for configuring the behavior based on the master-slave role for I2C communication." +*/ +class IC2TransportManager { + public: + static IC2TransportMaster tm; + static IC2TransportSlave ts; + + static void setup(FullLoopbackStream *outgoingStream){ + #if IC2_MASTER + #if IC2_SERIAL_BYPASS_DEBUG + Serial.begin(115200); + Serial.print("\nSetup as Master\n"); + Serial.flush(); + #endif + tm.setup(outgoingStream); + #endif + + #if IC2_SLAVE + Serial.print("Setup as Slave"); + Serial.flush(); + ts.setup(outgoingStream); + #endif + } + static void loop(){ + #ifdef IC2_MASTER + tm.loop(); + #endif + + #ifdef IC2_SLAVE + ts.loop(); + #endif + } + + static void flush(){ + #ifdef IC2_MASTER + tm.flush(); + #endif + + #ifdef IC2_SLAVE + ts.flush(); + #endif + } +}; + +#if IC2_MASTER +#define StreamAvailable WIRE.available +#define FlowSerialFlush WIRE.flush +#define StreamFlush WIRE.flush +#define StreamWrite outgoingStream.write +#define StreamPrint WIRE.print +/** SETUP SERIAL BYPASS IC2 SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ +#define FlowSerialBegin [](unsigned long baud) { Serial.printf("Hola mundo");} +#endif +#if IC_SLAVE +#define StreamRead Serial.read +#define StreamFlush Serial.flush +#define StreamWrite Serial.write +#define StreamPrint Serial.print +#define StreamAvailable Serial.available +#define FlowSerialBegin [](unsigned long baud) { Serial.begin(baud);} +#endif \ No newline at end of file diff --git a/lib/EspSimHub/I2CSerialBridge/I2CSeriaBypass.h b/lib/EspSimHub/I2CSerialBridge/I2CSeriaBypass.h new file mode 100644 index 0000000..419e64f --- /dev/null +++ b/lib/EspSimHub/I2CSerialBridge/I2CSeriaBypass.h @@ -0,0 +1,35 @@ +#pragma once +#include +#include "I2CTransport.h" + + + + + + +class IC2TransportSlave : public IC2Transport { + public: + void setup(FullLoopbackStream *outgoingStream){ + ic2SetupSlave(); + } + + void loop() { + + } + void flush() { + + } + + static void resendToSerialFromDevice(size_t howManyChars){ + while (0 #include class I2CSerialBridge{ @@ -26,26 +27,34 @@ class I2CSerialBridge{ size_t availableLength = this->outgoingStream->available(); if (availableLength) { -#if DEBUG_TCP_BRIDGE - Serial.printf("flushing with this much data: %d \n", availableLength); -#endif - - + #if DEBUG_TCP_BRIDGE + Serial.printf("flushing with this much data: %d \n", availableLength); + #endif // read the available data from the stream, and put in in the buffer char sbuf[availableLength]; this->outgoingStream->readBytes(sbuf, availableLength); // send data to client - Wire.beginTransmission(address); - size_t total = Wire.write(sbuf, availableLength); - Wire.endTransmission(); -#if DEBUG_TCP_BRIDGE - Serial.printf("\n ---> data sent to client %s: %d bytes \n", client->remoteIP().toString().c_str(), total); - Serial.printf("%d %d\n",sbuf[0], sbuf[1]); -#endif + WIRE.beginTransmission(address); + size_t total = WIRE.write(sbuf, availableLength); + WIRE.endTransmission(); + #if DEBUG_TCP_BRIDGE + Serial.printf("\n ---> data sent to client %s: %d bytes \n", client->remoteIP().toString().c_str(), total); + Serial.printf("%d %d\n",sbuf[0], sbuf[1]); + #endif } } - + + + /** SETUP SERIAL BYPASS IC2 MASTER, USE WHEN THIS DEVICE COMMAND THE SENDING WORKFLOW*/ + void ic2SetupMaster(){ + WIRE.begin(); + + while(!isSlaveAvailable()){ + Serial.printf("\n Slave device not available, retrying 1 sec later"); + delay(1000); + }; + } private: /** TODO CONTROL THAT TRANSPORT LAYER IS READY AND CONNECTED*/ @@ -53,4 +62,50 @@ class I2CSerialBridge{ } + + + +uint8_t endWireTransmission(bool stop){ + uint8_t error=WIRE.endTransmission(stop); + if(error=0){ + Serial.print("\n Correct wire close \n"); + } + if(error==1){ + Serial.print("\n Wire buffer exausted \n"); + } + if(error==2){ + Serial.print("\n received NACK on transmit of address.\n"); + } + if(error==3){ + Serial.print("received NACK on transmit of data."); + } + if(error==4){ + Serial.print(" other error."); + } + if(error==5){ + Serial.print("timeout"); + } + Serial.printf("\n Error code is %d",error); + Serial.flush(); + return error; + +} + +bool isSlaveAvailable(){ + #if IC2_SERIAL_BYPASS_DEBUG + Serial.print("\n Testing slave availability -> Beging transmission to "); + Serial.print(IC2_ADDRESS); + Serial.println("\n"); + #endif + WIRE.beginTransmission(IC2_ADDRESS); + uint8_t error = endWireTransmission(true); + + if(error==0){ + Serial.printf("\n Slave device detected at address 8\n"); + return true; + } + return false; +} + + }; \ No newline at end of file diff --git a/lib/EspSimHub/I2CSerialBridge/I2CTransport.h b/lib/EspSimHub/I2CSerialBridge/I2CTransport.h index 20549ae..b89e0e0 100644 --- a/lib/EspSimHub/I2CSerialBridge/I2CTransport.h +++ b/lib/EspSimHub/I2CSerialBridge/I2CTransport.h @@ -1,6 +1,7 @@ +#pragma once #include #include -#include "I2CSerialBridge.h" +#include #ifndef WIRE # define WIRE Wire; @@ -21,160 +22,10 @@ // ic2SetupSlave(); // #endif -I2CSerialBridge instance(0x08); - class IC2Transport { - public: - static void setup(FullLoopbackStream *outgoingStream){ - instance.setup(outgoingStream); - } - - static void loop() { - instance.loop(); - } - static void flush() { - instance.flush(); - } - - // static size_t write(const uint8_t *buffer, size_t size){ - // Wire.beginTransmission(IC2_ADDRESS); - // Wire.write(buffer,size); - // return Wire.endTransmission(); - // } - // static size_t write(const char *buffer, size_t size){ - // Wire.beginTransmission(IC2_ADDRESS); - // Wire.write(buffer,size); - // return Wire.endTransmission(); - // } - // static size_t write(const char *str){ - // Wire.beginTransmission(IC2_ADDRESS); - // Wire.write(str); - // return Wire.endTransmission(); - // } - // static size_t write(uint8_t data){ - // Wire.beginTransmission(IC2_ADDRESS); - // Wire.write(data); - // return Wire.endTransmission(); - // } - -}; - - - -#define StreamAvailable WIRE.available -#define FlowSerialFlush WIRE.flush -#define StreamFlush WIRE.flush -#define StreamWrite outgoingStream.write -#define StreamPrint WIRE.print - -// size_t I2CWrite(u_int8_t data){ -// size_t oVal=0; -// Wire.beginTransmission(IC2_ADDRESS); -// oVal= WIRE.write(data); -// endWireTransmission(true); -// return oVal; -// } - -// size_t I2CWrite(const uint8_t *data, size_t size){ -// size_t oVal=0; -// Wire.beginTransmission(IC2_ADDRESS); -// oVal= WIRE.write(data,size); -// endWireTransmission(true); -// return oVal; -// } - - - -uint8_t endWireTransmission(bool stop){ - uint8_t error=Wire.endTransmission(stop); - if(error=0){ - Serial.printf("\n Correct wire close \n"); - } - if(error==1){ - Serial.printf("\n Wire buffer exausted \n"); - } - if(error==2){ - Serial.printf("\n received NACK on transmit of address.\n"); - } - if(error==3){ - Serial.printf("received NACK on transmit of data."); - } - if(error==4){ - Serial.printf(" other error."); - } - if(error==5){ - Serial.printf("timeout"); - } - Serial.flush(); - return error; - -} - -bool isSlaveAvailable(){ - Wire.beginTransmission(IC2_ADDRESS); - uint8_t error = endWireTransmission(true); - - if(error==0){ - Serial.printf("\n Slave device detected at address 8\n"); - return true; - } - return false; -} - -/** SETUP SERIAL BYPASS IC2 MASTER, USE WHEN THIS DEVICE COMMAND THE SENDING WORKFLOW*/ -void ic2SetupMaster(){ - WIRE.begin(); - while(!isSlaveAvailable()){ - Serial.printf("\n Slave device not available, retrying 1 sec later"); - delay(1000); - }; -} - - -void resendToSerialFromMasterDevice(size_t howManyChars){ - // FlowSerialDebugPrintLn("Received data"); - //int recvBytes=0; - -// int buttonId=-1; - //byte buttonStatus=0; - - while (0 Date: Thu, 21 Mar 2024 02:33:46 +0100 Subject: [PATCH 05/25] Moved I2C FILES to libs. --- lib/{EspSimHub => }/I2CSerialBridge/I2CManager.h | 0 lib/{EspSimHub => }/I2CSerialBridge/I2CSeriaBypass.h | 0 lib/{EspSimHub => }/I2CSerialBridge/I2CSerialBridge.h | 0 lib/{EspSimHub => }/I2CSerialBridge/I2CTransport.h | 0 lib/{EspSimHub => }/I2CSerialBridge/IC2TransportMaster.h | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename lib/{EspSimHub => }/I2CSerialBridge/I2CManager.h (100%) rename lib/{EspSimHub => }/I2CSerialBridge/I2CSeriaBypass.h (100%) rename lib/{EspSimHub => }/I2CSerialBridge/I2CSerialBridge.h (100%) rename lib/{EspSimHub => }/I2CSerialBridge/I2CTransport.h (100%) rename lib/{EspSimHub => }/I2CSerialBridge/IC2TransportMaster.h (100%) diff --git a/lib/EspSimHub/I2CSerialBridge/I2CManager.h b/lib/I2CSerialBridge/I2CManager.h similarity index 100% rename from lib/EspSimHub/I2CSerialBridge/I2CManager.h rename to lib/I2CSerialBridge/I2CManager.h diff --git a/lib/EspSimHub/I2CSerialBridge/I2CSeriaBypass.h b/lib/I2CSerialBridge/I2CSeriaBypass.h similarity index 100% rename from lib/EspSimHub/I2CSerialBridge/I2CSeriaBypass.h rename to lib/I2CSerialBridge/I2CSeriaBypass.h diff --git a/lib/EspSimHub/I2CSerialBridge/I2CSerialBridge.h b/lib/I2CSerialBridge/I2CSerialBridge.h similarity index 100% rename from lib/EspSimHub/I2CSerialBridge/I2CSerialBridge.h rename to lib/I2CSerialBridge/I2CSerialBridge.h diff --git a/lib/EspSimHub/I2CSerialBridge/I2CTransport.h b/lib/I2CSerialBridge/I2CTransport.h similarity index 100% rename from lib/EspSimHub/I2CSerialBridge/I2CTransport.h rename to lib/I2CSerialBridge/I2CTransport.h diff --git a/lib/EspSimHub/I2CSerialBridge/IC2TransportMaster.h b/lib/I2CSerialBridge/IC2TransportMaster.h similarity index 100% rename from lib/EspSimHub/I2CSerialBridge/IC2TransportMaster.h rename to lib/I2CSerialBridge/IC2TransportMaster.h From 99a49f85db6113b34ac223103b5511ce82257cab Mon Sep 17 00:00:00 2001 From: wapophis Date: Thu, 21 Mar 2024 02:35:02 +0100 Subject: [PATCH 06/25] Added serial interceptor, later will change class naming. This is the impl on the slave, working with arduino AVR impl. --- .../SimHubProtocolDecoder.cpp | 0 .../SimHubProtocolDecoder.h | 112 ++++++++++++++++++ src/main.cpp | 2 +- 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 lib/SimHubProtocolDecoder/SimHubProtocolDecoder.cpp create mode 100644 lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h diff --git a/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.cpp b/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.cpp new file mode 100644 index 0000000..e69de29 diff --git a/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h b/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h new file mode 100644 index 0000000..1220eb0 --- /dev/null +++ b/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h @@ -0,0 +1,112 @@ +#include + + +typedef void(*SHButtonChangedEventCallBack) (int, byte); //function pointer prototype +typedef void(*SHRotaryEncoderPositionChangedEventCallBack) (int, int, byte); //function pointer prototype +/*** + * | PACKET HEADER | SIZE | DSCRIPTION | + * | 0x01 | 3 | ENCODER SIZE 3 + * | 0X02 | 2 | ENCODER SIZE 2 + * | 0X03 | 2 | BUTTON STATE SIZE 2 + * | 0X04 | 3 | TM1638 BUTTONS STATE SIZE 3 + * +*/ +byte packetsHeaders[]={0x01,0x02,0x03,0x04}; +byte packetsPayloadLength[]={3,2,2,3}; + +class EventCallBackManager{ + SHButtonChangedEventCallBack shButtonChangedCallback; + SHRotaryEncoderPositionChangedEventCallBack SHRotaryEncoderPositionChangedCallback; + + public: + void setButtonCallBack(SHButtonChangedEventCallBack callback){ + shButtonChangedCallback=callback; + } + SHButtonChangedEventCallBack getButtonCallback(){ + return shButtonChangedCallback; + } + + void setEncoderPositionChangedCallback(SHRotaryEncoderPositionChangedEventCallBack callback){ + SHRotaryEncoderPositionChangedCallback=callback; + } + SHRotaryEncoderPositionChangedEventCallBack getEncoderPositionChangedCallback(){ + return SHRotaryEncoderPositionChangedCallback; + } +}; + + +static void decodeBuffer(EventCallBackManager *callbacker,Stream *stream){ + byte packetType=0x0; + int size=-1; + #if IC2_SERIAL_BYPASS_DEBUG + Serial.print("\n Disponible en buffer "); + Serial.print(stream->available()); + Serial.print("\n"); + Serial.flush(); + #endif + if(stream->available()){ + packetType=stream->read(); + #if IC2_SERIAL_BYPASS_DEBUG + Serial.print("\n packetType "); + Serial.print(packetType); + Serial.print("\n"); + Serial.flush(); + #endif + } + if(packetType==0x09){ + packetType=stream->read(); + + #if IC2_SERIAL_BYPASS_DEBUG + Serial.print("\n packetType "); + Serial.print(packetType); + Serial.print("\n"); + Serial.flush(); + #endif + + size=stream->read(); + + #if IC2_SERIAL_BYPASS_DEBUG + Serial.print("\n Payload Size is "); + Serial.print(size); + Serial.print("\n"); + Serial.flush(); + #endif + + + switch (packetType){ + case 0x01: + // callbacker->getEncoderPositionChangedCallback()(stream->read(),stream->read(),stream->read()); + break; + case 0x02: + //callbacker->getEncoderPositionChangedCallback()(stream->read(),stream->read(),stream->read()); + break; + case 0x03: + int buttonId; + buttonId=stream->read(); + byte status; + status=stream->read(); + #if IC2_SERIAL_BYPASS_DEBUG + Serial.print("\n BUtton state changed "); + Serial.println(buttonId); + Serial.println(status); + Serial.flush(); + #endif + callbacker->getButtonCallback()(buttonId,status); + break; + case 0x04: + // TODO: FIXME + //callbacker->getButtonCallback()((stream->read())*8+stream->read(),stream->read()); + break; + default: + break; + } + + + while (0 available()){ + Serial.write(stream->read()); + } + stream->flush(); + + } + +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 92b0244..ffd221a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,7 @@ #define IC2_SLAVE false #define IC2_ADDRESS 0x08 #define IC2_SERIAL_BYPASS_DEBUG true - #include "I2CSerialBridge/I2CManager.h" + #include FullLoopbackStream outgoingStream; From 94bcfc4d8ebd324af684091efb2e99b8002160d6 Mon Sep 17 00:00:00 2001 From: wapophis Date: Thu, 21 Mar 2024 10:22:52 +0100 Subject: [PATCH 07/25] Generalize to Streams. --- lib/I2CSerialBridge/I2CSeriaBypass.h | 6 ++++-- lib/I2CSerialBridge/I2CTransport.h | 2 +- lib/I2CSerialBridge/IC2TransportMaster.h | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/I2CSerialBridge/I2CSeriaBypass.h b/lib/I2CSerialBridge/I2CSeriaBypass.h index 419e64f..d0965e3 100644 --- a/lib/I2CSerialBridge/I2CSeriaBypass.h +++ b/lib/I2CSerialBridge/I2CSeriaBypass.h @@ -8,8 +8,10 @@ class IC2TransportSlave : public IC2Transport { + static Stream *outgoingStream; public: - void setup(FullLoopbackStream *outgoingStream){ + void setup(Stream *outgoingStream){ + this->outgoingStream=outgoingStream; ic2SetupSlave(); } @@ -23,7 +25,7 @@ class IC2TransportSlave : public IC2Transport { static void resendToSerialFromDevice(size_t howManyChars){ while (0 write(c); } }; diff --git a/lib/I2CSerialBridge/I2CTransport.h b/lib/I2CSerialBridge/I2CTransport.h index b89e0e0..f7a75d1 100644 --- a/lib/I2CSerialBridge/I2CTransport.h +++ b/lib/I2CSerialBridge/I2CTransport.h @@ -27,5 +27,5 @@ class IC2Transport { IC2Transport(){} virtual void loop() = 0 ; virtual void flush() = 0; - virtual void setup(FullLoopbackStream *) = 0; + virtual void setup(Stream *) = 0; }; \ No newline at end of file diff --git a/lib/I2CSerialBridge/IC2TransportMaster.h b/lib/I2CSerialBridge/IC2TransportMaster.h index c9bbe09..f25ceb0 100644 --- a/lib/I2CSerialBridge/IC2TransportMaster.h +++ b/lib/I2CSerialBridge/IC2TransportMaster.h @@ -10,9 +10,9 @@ class IC2TransportMaster : public IC2Transport { public: IC2TransportMaster(){} - void setup(FullLoopbackStream *outgoingStream){ + void setup(Stream *outgoingStream){ ic2SetupSerialBypass(); - instance.setup(outgoingStream); + instance.setup((FullLoopbackStream*) outgoingStream); } void loop() { instance.loop(); From c49feb3fcb774430004fefd97b67f697598768d3 Mon Sep 17 00:00:00 2001 From: wapophis Date: Thu, 21 Mar 2024 10:35:26 +0100 Subject: [PATCH 08/25] Renamed I2C Classess and files --- lib/I2CSerialBridge/I2CManager.h | 8 ++++---- lib/I2CSerialBridge/I2CSerialBridge.h | 5 +++++ lib/I2CSerialBridge/I2CTransport.h | 4 ++-- .../{IC2TransportMaster.h => I2CTransportMaster.h} | 8 ++++++-- .../{I2CSeriaBypass.h => I2CTransportSlave.h} | 9 ++++----- 5 files changed, 21 insertions(+), 13 deletions(-) rename lib/I2CSerialBridge/{IC2TransportMaster.h => I2CTransportMaster.h} (90%) rename lib/I2CSerialBridge/{I2CSeriaBypass.h => I2CTransportSlave.h} (78%) diff --git a/lib/I2CSerialBridge/I2CManager.h b/lib/I2CSerialBridge/I2CManager.h index 9b4f604..3bf83c4 100644 --- a/lib/I2CSerialBridge/I2CManager.h +++ b/lib/I2CSerialBridge/I2CManager.h @@ -1,14 +1,14 @@ #pragma once -#include "IC2TransportMaster.h" -#include "I2CSeriaBypass.h" +#include "I2CTransportMaster.h" +#include "I2CTransportSlave.h" /*** * "This Manager is responsible for configuring the behavior based on the master-slave role for I2C communication." */ class IC2TransportManager { public: - static IC2TransportMaster tm; - static IC2TransportSlave ts; + static I2CTransportMaster tm; + static I2CTransportSlave ts; static void setup(FullLoopbackStream *outgoingStream){ #if IC2_MASTER diff --git a/lib/I2CSerialBridge/I2CSerialBridge.h b/lib/I2CSerialBridge/I2CSerialBridge.h index ad2c8aa..0363b7b 100644 --- a/lib/I2CSerialBridge/I2CSerialBridge.h +++ b/lib/I2CSerialBridge/I2CSerialBridge.h @@ -1,6 +1,11 @@ #include #include + +#ifndef WIRE +#error WIRE must be settled to have a correct custom wire config in your MASTER config. +#endif + class I2CSerialBridge{ FullLoopbackStream *outgoingStream; uint8_t address; diff --git a/lib/I2CSerialBridge/I2CTransport.h b/lib/I2CSerialBridge/I2CTransport.h index f7a75d1..4237129 100644 --- a/lib/I2CSerialBridge/I2CTransport.h +++ b/lib/I2CSerialBridge/I2CTransport.h @@ -22,9 +22,9 @@ // ic2SetupSlave(); // #endif -class IC2Transport { +class I2CTransport { public: - IC2Transport(){} + I2CTransport(){} virtual void loop() = 0 ; virtual void flush() = 0; virtual void setup(Stream *) = 0; diff --git a/lib/I2CSerialBridge/IC2TransportMaster.h b/lib/I2CSerialBridge/I2CTransportMaster.h similarity index 90% rename from lib/I2CSerialBridge/IC2TransportMaster.h rename to lib/I2CSerialBridge/I2CTransportMaster.h index f25ceb0..af5321e 100644 --- a/lib/I2CSerialBridge/IC2TransportMaster.h +++ b/lib/I2CSerialBridge/I2CTransportMaster.h @@ -3,13 +3,17 @@ #include "I2CTransport.h" #include "I2CSerialBridge.h" +#ifndef WIRE +#error WIRE must be settled to have a correct custom wire config in your MASTER config. +#endif + #if IC2_MASTER I2CSerialBridge instance(IC2_ADDRESS); -class IC2TransportMaster : public IC2Transport { +class I2CTransportMaster : public I2CTransport { public: - IC2TransportMaster(){} + I2CTransportMaster(){} void setup(Stream *outgoingStream){ ic2SetupSerialBypass(); instance.setup((FullLoopbackStream*) outgoingStream); diff --git a/lib/I2CSerialBridge/I2CSeriaBypass.h b/lib/I2CSerialBridge/I2CTransportSlave.h similarity index 78% rename from lib/I2CSerialBridge/I2CSeriaBypass.h rename to lib/I2CSerialBridge/I2CTransportSlave.h index d0965e3..4b09c14 100644 --- a/lib/I2CSerialBridge/I2CSeriaBypass.h +++ b/lib/I2CSerialBridge/I2CTransportSlave.h @@ -2,12 +2,11 @@ #include #include "I2CTransport.h" +#ifndef WIRE +#error WIRE must be settled to have a correct custom wire config in your SLAVE config. +#endif - - - - -class IC2TransportSlave : public IC2Transport { +class I2CTransportSlave : public I2CTransport { static Stream *outgoingStream; public: void setup(Stream *outgoingStream){ From 609e752897bf2c2faa1cf140241bab68bf45f43f Mon Sep 17 00:00:00 2001 From: wapophis Date: Thu, 21 Mar 2024 10:59:04 +0100 Subject: [PATCH 09/25] Slave impl not tested yet. The idea behind is that de StreamWrite macro will be overriden later. The default behaviour about stream, is read all the stream an send to StreamWrite(buffer,buffersize) method, seems that stream.write(buf,size) is stream compliant and must be implemented in all the stream transport layer that maybe used to comunicate with SIMHUB. --- lib/I2CSerialBridge/I2CManager.h | 16 ++++++++-------- lib/I2CSerialBridge/I2CTransportSlave.h | 10 ++++++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/I2CSerialBridge/I2CManager.h b/lib/I2CSerialBridge/I2CManager.h index 3bf83c4..aa1fb10 100644 --- a/lib/I2CSerialBridge/I2CManager.h +++ b/lib/I2CSerialBridge/I2CManager.h @@ -56,11 +56,11 @@ class IC2TransportManager { /** SETUP SERIAL BYPASS IC2 SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ #define FlowSerialBegin [](unsigned long baud) { Serial.printf("Hola mundo");} #endif -#if IC_SLAVE -#define StreamRead Serial.read -#define StreamFlush Serial.flush -#define StreamWrite Serial.write -#define StreamPrint Serial.print -#define StreamAvailable Serial.available -#define FlowSerialBegin [](unsigned long baud) { Serial.begin(baud);} -#endif \ No newline at end of file +// #if IC_SLAVE +// #define StreamRead Serial.read +// #define StreamFlush Serial.flush +// #define StreamWrite Serial.write +// #define StreamPrint Serial.print +// #define StreamAvailable Serial.available +// #define FlowSerialBegin [](unsigned long baud) { Serial.begin(baud);} +// #endif \ No newline at end of file diff --git a/lib/I2CSerialBridge/I2CTransportSlave.h b/lib/I2CSerialBridge/I2CTransportSlave.h index 4b09c14..d5b4a36 100644 --- a/lib/I2CSerialBridge/I2CTransportSlave.h +++ b/lib/I2CSerialBridge/I2CTransportSlave.h @@ -2,6 +2,8 @@ #include #include "I2CTransport.h" +#define StreamWrite + #ifndef WIRE #error WIRE must be settled to have a correct custom wire config in your SLAVE config. #endif @@ -22,10 +24,14 @@ class I2CTransportSlave : public I2CTransport { } static void resendToSerialFromDevice(size_t howManyChars){ + byte bffer[Wire.available()]; + int bfferIdx=0; while (0 write(c); + char c = WIRE.read(); /* receive byte as a character */ + bffer[bfferIdx]=c; + bfferIdx++; } + StreamWrite(&bffer,sizeof(bffer)); }; void ic2SetupSlave(){ From df21309a4a36229cee476ac44246492cb891556f Mon Sep 17 00:00:00 2001 From: wapophis Date: Thu, 21 Mar 2024 12:55:51 +0100 Subject: [PATCH 10/25] Added new logic to send via serial de axis changed state with a callback --- src/SHGamepadAxis.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/SHGamepadAxis.h b/src/SHGamepadAxis.h index ee7989c..789a477 100644 --- a/src/SHGamepadAxis.h +++ b/src/SHGamepadAxis.h @@ -4,9 +4,13 @@ #include #include + +typedef void(*SHAxisChanged) (int, int); + + class SHGamepadAxis { private: - + SHAxisChanged shAxisChangedCallback; int lastAxisValue = -1; int axisPin = -1; int axisIdx = -1; @@ -42,7 +46,7 @@ class SHGamepadAxis { case 2: joystick->setBrake(value); break; - default: + default: break; } @@ -51,6 +55,11 @@ class SHGamepadAxis { public: + SHGamepadAxis(byte axisPin, int axisIdx, int minimumInputValue, int maximumInputValue, int samplingRate, double exponentialFactor = 1,SHAxisChanged callback){ + shAxisChangedCallback=callback; + SHGamepadAxis axisPin, axisIdx, minimumInputValue, maximumInputValue, samplingRate, exponentialFactor) + } + SHGamepadAxis(byte axisPin, int axisIdx, int minimumInputValue, int maximumInputValue, int samplingRate, double exponentialFactor = 1) { this->axisIdx = axisIdx; this->axisPin = axisPin; @@ -68,7 +77,7 @@ class SHGamepadAxis { bool read() { int pot = analogReadXXbit(axisPin, samplingRate); - if (lastAxisValue != pot) { + if (lastAxisValue != pot) { lastAxisValue = pot; int mapped = map(pot, minimumInputValue, maximumInputValue, 0, 1024); From 926f500f3b78dcd9c2a88ef5c65571d46d37d552 Mon Sep 17 00:00:00 2001 From: wapophis Date: Thu, 21 Mar 2024 12:57:50 +0100 Subject: [PATCH 11/25] Active shAxisCallBack execution --- src/SHGamepadAxis.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/SHGamepadAxis.h b/src/SHGamepadAxis.h index 789a477..a154567 100644 --- a/src/SHGamepadAxis.h +++ b/src/SHGamepadAxis.h @@ -10,7 +10,7 @@ typedef void(*SHAxisChanged) (int, int); class SHGamepadAxis { private: - SHAxisChanged shAxisChangedCallback; + SHAxisChanged shAxisChangedCallback=null; int lastAxisValue = -1; int axisPin = -1; int axisIdx = -1; @@ -89,6 +89,9 @@ class SHGamepadAxis { mapped2 = mapped2 * 1024; setAxis(axisIdx, mapped2); + if(this->shAxisChangedCallback!=null){ + shAxisChangedCallback(axisIdx,mapped2); + } } } }; From e12d19003ce1d89fd22fdb97f1f7ad726682bbae Mon Sep 17 00:00:00 2001 From: wapophis Date: Thu, 21 Mar 2024 15:05:22 +0100 Subject: [PATCH 12/25] Analog Joystick support sending via customPacket 0x13 --- lib/I2CSerialBridge/I2CTransportSlave.h | 4 + .../SimHubProtocolDecoder.h | 2 + src/main.cpp | 93 +++++-------------- 3 files changed, 28 insertions(+), 71 deletions(-) diff --git a/lib/I2CSerialBridge/I2CTransportSlave.h b/lib/I2CSerialBridge/I2CTransportSlave.h index d5b4a36..defe5c2 100644 --- a/lib/I2CSerialBridge/I2CTransportSlave.h +++ b/lib/I2CSerialBridge/I2CTransportSlave.h @@ -36,6 +36,10 @@ class I2CTransportSlave : public I2CTransport { void ic2SetupSlave(){ WIRE.begin(IC2_ADDRESS); + /** + * BECAUSE THERE IS NO JOYSTICK SUPPORT IN THE ESP IMPL DEFAULT IMPL FOR ESP IS THIS IMPL TO RESEND ALL THE FLOW + * TO THE TRANSPORT STREAM WHICH IS CONFIGURED FOR THE DEVICE. + */ WIRE.onReceive(resendToSerialFromDevice); /* register receive event */ } }; diff --git a/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h b/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h index 1220eb0..b88d37c 100644 --- a/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h +++ b/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h @@ -53,6 +53,8 @@ static void decodeBuffer(EventCallBackManager *callbacker,Stream *stream){ Serial.flush(); #endif } + + // CUSTOM PACKETS if(packetType==0x09){ packetType=stream->read(); diff --git a/src/main.cpp b/src/main.cpp index ffd221a..08d21ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -542,15 +542,15 @@ SHPWMPin shCONSPIN(CONS_PIN, 40); #define GAMEPAD_AXIS_03_EXPONENTIALFACTOR 1 //{"Name":"GAMEPAD_AXIS_03_EXPONENTIALFACTOR","Title":"Brake axis exponential correction","DefaultValue":"1","Type":"double","Condition":"GAMEPAD_AXIS_03_ENABLED>0","dMin":0.1,"dMax":1.9} #if(GAMEPAD_AXIS_01_ENABLED == 1) -SHGamepadAxis SHGAMEPADAXIS01(GAMEPAD_AXIS_01_PIN, 0, GAMEPAD_AXIS_01_MINVALUE, GAMEPAD_AXIS_01_MAXVALUE, GAMEPAD_AXIS_01_SAMPLING, GAMEPAD_AXIS_01_EXPONENTIALFACTOR); +SHGamepadAxis SHGAMEPADAXIS01(GAMEPAD_AXIS_01_PIN, 0, GAMEPAD_AXIS_01_MINVALUE, GAMEPAD_AXIS_01_MAXVALUE, GAMEPAD_AXIS_01_SAMPLING, GAMEPAD_AXIS_01_EXPONENTIALFACTOR,axisStatusChanged); #endif #if(GAMEPAD_AXIS_02_ENABLED == 1) -SHGamepadAxis SHGAMEPADAXIS02(GAMEPAD_AXIS_02_PIN, 1, GAMEPAD_AXIS_02_MINVALUE, GAMEPAD_AXIS_02_MAXVALUE, GAMEPAD_AXIS_02_SAMPLING, GAMEPAD_AXIS_02_EXPONENTIALFACTOR); +SHGamepadAxis SHGAMEPADAXIS02(GAMEPAD_AXIS_02_PIN, 1, GAMEPAD_AXIS_02_MINVALUE, GAMEPAD_AXIS_02_MAXVALUE, GAMEPAD_AXIS_02_SAMPLING, GAMEPAD_AXIS_02_EXPONENTIALFACTOR,axisStatusChanged); #endif #if(GAMEPAD_AXIS_03_ENABLED == 1) -SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, GAMEPAD_AXIS_03_MAXVALUE, GAMEPAD_AXIS_03_SAMPLING, GAMEPAD_AXIS_03_EXPONENTIALFACTOR); +SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, GAMEPAD_AXIS_03_MAXVALUE, GAMEPAD_AXIS_03_SAMPLING, GAMEPAD_AXIS_03_EXPONENTIALFACTOR,axisStatusChanged); #endif #endif // INCLUDE_GAMEPAD @@ -1149,6 +1149,17 @@ void buttonStatusChanged(int buttonId, byte Status) { #endif } +void axisStatusChanged(int axisId,int mappedValue){ +// #if GAMEPAD_AXIS_01_ENABLED || GAMEPAD_AXIS_02_ENABLED ||GAMEPAD_AXIS_03_ENABLED + #if IC2_SERIAL_BYPASS + arqserial.CustomPacketStart(0x13,2); + arqserial.CustomPacketSendByte(axisId); + arqserial.CustomPacketSendByte(mappedValue); + arqserial.CustomPacketEnd(); + #endif +// #endif +} + #ifdef INCLUDE_BUTTONMATRIX void buttonMatrixStatusChanged(int buttonId, byte Status) { #ifdef INCLUDE_GAMEPAD @@ -1164,74 +1175,6 @@ void buttonMatrixStatusChanged(int buttonId, byte Status) { #endif -// #if IC2_SERIAL_BYPASS - - -// void resendToSerialFromMasterDevice(size_t howManyChars){ -// // FlowSerialDebugPrintLn("Received data"); -// //int recvBytes=0; - -// // int buttonId=-1; -// //byte buttonStatus=0; - -// while (0 1024){ + aAxisCounter=0; + } IC2TransportManager::loop(); #endif @@ -1538,6 +1487,8 @@ void loop() { #ifdef INCLUDE_GAMEPAD UpdateGamepadState(); #endif + + shCustomProtocol.loop(); //delay(1000); //Serial.printf("\nI'm alive\n"); From ca8d76f8c17c9545aaf656a97d160b9eb26cc6fe Mon Sep 17 00:00:00 2001 From: wapophis Date: Fri, 22 Mar 2024 09:44:46 +0100 Subject: [PATCH 13/25] Analog Axis implementation and analog axis simulation --- lib/AnalogAxisSimulator/AnalogAxis.cpp | 54 +++++++++++++++++++++++++ lib/AnalogAxisSimulator/AnalogAxis.h | 25 ++++++++++++ src/ArqSerial.h | 12 ++++-- src/SHGamepadAxis.h | 54 +++++++++++++++++-------- src/main.cpp | 55 +++++++++++++++++--------- 5 files changed, 161 insertions(+), 39 deletions(-) create mode 100644 lib/AnalogAxisSimulator/AnalogAxis.cpp create mode 100644 lib/AnalogAxisSimulator/AnalogAxis.h diff --git a/lib/AnalogAxisSimulator/AnalogAxis.cpp b/lib/AnalogAxisSimulator/AnalogAxis.cpp new file mode 100644 index 0000000..181744f --- /dev/null +++ b/lib/AnalogAxisSimulator/AnalogAxis.cpp @@ -0,0 +1,54 @@ +#include "AnalogAxis.h" + +int pot = -1; +bool AnalogAxisSimulator::read(int samplingRate){ + + + if(millis()-lastGeneratedReading>300){ + pot=analogReadXXbit(samplingRate); + lastGeneratedReading=millis(); + } + + if (lastAxisValue != pot) { + Serial.println("\nAnalogAxisSimulator::setAnalogReading"); + Serial.println(pot); + lastAxisValue = pot; + + int mapped = map(pot, minimumInputValue, maximumInputValue, 0, 1024); + Serial.print("\nMapped:"); + Serial.print(mapped); + float mapped2 = min((float)1, max((float)0,(float) ((float)mapped / (float)1024))); + Serial.print("\nMapped2:"); + Serial.print(mapped2); + + if (exponentialFactor != 1) { + mapped2 = pow(mapped2, 1.0 / (float)exponentialFactor); + } + + mapped2 = mapped2 * 1024; + Serial.print("\nMapped2 Powed:"); + Serial.print(mapped2); + if(this->shAxisChangedCallback!=nullptr){ + Serial.println("\nCallback defined"); + shAxisChangedCallback(axisIdx,mapped2); + } + } + return false; + +} + + float AnalogAxisSimulator::analogReadXXbit(byte bits_of_precision) + { + uint8_t n = bits_of_precision - 10; + unsigned long oversample_num = 1 << (2 * n); + uint8_t divisor = 1 << n; + unsigned long reading_sum = 0; + unsigned long inner_sum = 0; + for (unsigned long j = 0; j < oversample_num; j++) + { + inner_sum +=random(-1024,1024); + } + unsigned int reading = (inner_sum + (unsigned long)divisor / 2UL) >> n; + reading_sum += reading; + return (float)reading_sum; + } \ No newline at end of file diff --git a/lib/AnalogAxisSimulator/AnalogAxis.h b/lib/AnalogAxisSimulator/AnalogAxis.h new file mode 100644 index 0000000..034d3ce --- /dev/null +++ b/lib/AnalogAxisSimulator/AnalogAxis.h @@ -0,0 +1,25 @@ +#include +typedef void(*SHAxisChanged) (int, int); + +class AnalogAxisSimulator{ + int axisIdx=1; + int lastAxisValue = -1; + int minimumInputValue=0; + int maximumInputValue=1024; + int samplingRate=10; + float exponentialFactor=1; + unsigned long lastGeneratedReading=millis(); + SHAxisChanged shAxisChangedCallback=nullptr; + public: + + AnalogAxisSimulator(int axisIdx){ + this->axisIdx=axisIdx; + } + bool read(int samplingRate); + + void setCallBack(SHAxisChanged callback){ + shAxisChangedCallback=callback; + } + + float analogReadXXbit(byte bits_of_precision); +}; \ No newline at end of file diff --git a/src/ArqSerial.h b/src/ArqSerial.h index 11b18ab..5d6fffa 100644 --- a/src/ArqSerial.h +++ b/src/ArqSerial.h @@ -148,7 +148,7 @@ class ARQSerial StreamWrite(0x03); StreamWrite(packetId); StreamFlush(); - Serial.printf("\nSendAcq[start,packetId]: [0x03,%d,%d]",packetId); + //Serial.printf("\nSendAcq[start,packetId]: [0x03,%d,%d]",packetId); } void SendNAcq(uint8_t lastKnownValidPacket, byte reason) @@ -157,7 +157,7 @@ class ARQSerial StreamWrite(lastKnownValidPacket); StreamWrite(reason); StreamFlush(); - Serial.printf("\nSendNAcq[start,lastKnownValidPacket,reason]: [0x04,%d,%d]",lastKnownValidPacket,reason); + //Serial.printf("\nSendNAcq[start,lastKnownValidPacket,reason]: [0x04,%d,%d]",lastKnownValidPacket,reason); } public: @@ -170,7 +170,10 @@ class ARQSerial StreamWrite(0x09); StreamWrite(packetType); StreamWrite(length); - Serial.printf("\nCustomPacketStart[start,packetType,length]: [0x09,%d,%d]",packetType,length); + // Serial.write(0x09); + // Serial.write(packetType); + // Serial.write(length); + //Serial.printf("\nCustomPacketStart[start,packetType,length]: [0x09,%d,%d]",packetType,length); } // void I2CustomPacketStart(byte packetType, uint8_t length) { @@ -182,7 +185,8 @@ class ARQSerial void CustomPacketSendByte(byte data) { StreamWrite(data); - Serial.printf("\nCustomPacketSendByte[data]: [%d]",data); + Serial.write(data); + //Serial.printf("\nCustomPacketSendByte[data]: [%d]",data); } // void I2CustomPacketSendByte(byte data) { diff --git a/src/SHGamepadAxis.h b/src/SHGamepadAxis.h index a154567..de92947 100644 --- a/src/SHGamepadAxis.h +++ b/src/SHGamepadAxis.h @@ -2,7 +2,7 @@ #define __SHGAMEPADAXIS_H__ #include -#include +/*#include */ typedef void(*SHAxisChanged) (int, int); @@ -10,14 +10,14 @@ typedef void(*SHAxisChanged) (int, int); class SHGamepadAxis { private: - SHAxisChanged shAxisChangedCallback=null; + SHAxisChanged shAxisChangedCallback=nullptr; int lastAxisValue = -1; int axisPin = -1; int axisIdx = -1; int minimumInputValue; int maximumInputValue; int samplingRate; - Joystick_* joystick; + //Joystick_* joystick; float exponentialFactor; float analogReadXXbit(uint8_t analogPin, uint8_t bits_of_precision) { @@ -40,26 +40,21 @@ class SHGamepadAxis { switch (axisIdx) { case 0: - joystick->setThrottle(value); break; + // joystick->setThrottle(value); break; case 1: - joystick->setAccelerator(value); break; + //joystick->setAccelerator(value); break; case 2: - joystick->setBrake(value); break; + //joystick->setBrake(value); break; default: break; } - joystick->sendState(); + //joystick->sendState(); } public: - SHGamepadAxis(byte axisPin, int axisIdx, int minimumInputValue, int maximumInputValue, int samplingRate, double exponentialFactor = 1,SHAxisChanged callback){ - shAxisChangedCallback=callback; - SHGamepadAxis axisPin, axisIdx, minimumInputValue, maximumInputValue, samplingRate, exponentialFactor) - } - SHGamepadAxis(byte axisPin, int axisIdx, int minimumInputValue, int maximumInputValue, int samplingRate, double exponentialFactor = 1) { this->axisIdx = axisIdx; this->axisPin = axisPin; @@ -69,11 +64,15 @@ class SHGamepadAxis { this->exponentialFactor = exponentialFactor; } - void SetJoystick(Joystick_* joystick) { - this->joystick = joystick; - read(); + void setCallBack(SHAxisChanged callback){ + this->shAxisChangedCallback=callback; } + // void SetJoystick(Joystick_* joystick) { + // this->joystick = joystick; + // read(); + // } + bool read() { int pot = analogReadXXbit(axisPin, samplingRate); @@ -81,7 +80,27 @@ class SHGamepadAxis { lastAxisValue = pot; int mapped = map(pot, minimumInputValue, maximumInputValue, 0, 1024); - float mapped2 = min(1, max(0, (float)mapped / (float)1024)); + float mapped2 = min(1, max(0, (mapped / 1024))); + + if (exponentialFactor != 1) { + mapped2 = pow(mapped2, 1.0 / (float)exponentialFactor); + } + + mapped2 = mapped2 * 1024; + setAxis(axisIdx, mapped2); + if(this->shAxisChangedCallback!=nullptr){ + shAxisChangedCallback(axisIdx,mapped2); + } + } + } + + bool setAxisAnalogReading(int pot) { + + if (lastAxisValue != pot) { + lastAxisValue = pot; + + int mapped = map(pot, minimumInputValue, maximumInputValue, 0, 1024); + float mapped2 = min(1, max(0, (mapped / 1024))); if (exponentialFactor != 1) { mapped2 = pow(mapped2, 1.0 / (float)exponentialFactor); @@ -89,11 +108,12 @@ class SHGamepadAxis { mapped2 = mapped2 * 1024; setAxis(axisIdx, mapped2); - if(this->shAxisChangedCallback!=null){ + if(this->shAxisChangedCallback!=nullptr){ shAxisChangedCallback(axisIdx,mapped2); } } } + }; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 08d21ed..735eab8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,9 +18,10 @@ #define IC2_ADDRESS 0x08 #define IC2_SERIAL_BYPASS_DEBUG true #include + #include FullLoopbackStream outgoingStream; - + AnalogAxisSimulator axis1(1); #endif #if INCLUDE_WIFI @@ -1014,6 +1015,8 @@ void idle(bool critical) { ECrowneWifi::flush(); #endif #if IC2_SERIAL_BYPASS + axis1.read(10); + yield(); IC2TransportManager::flush(); #endif @@ -1127,6 +1130,30 @@ void EncoderPositionChanged(int encoderId, int position, byte direction) { Serial.flush(); return error; */ + + +/** + * + * +*/ +void axisStatusChanged(int axisId,int mappedValue){ + #if IC2_SERIAL_BYPASS + if(IC2_SERIAL_BYPASS_DEBUG){ + Serial.print("AxisStatusChanged"); + } + + arqserial.CustomPacketStart(0x13,3); + arqserial.CustomPacketSendByte(axisId); + arqserial.CustomPacketSendByte(highByte(mappedValue)); + arqserial.CustomPacketSendByte(lowByte(mappedValue)); + arqserial.CustomPacketEnd(); + #endif + } + +/** + * + * + * */ void buttonStatusChanged(int buttonId, byte Status) { #ifdef INCLUDE_GAMEPAD Joystick.setButton(TM1638_ENABLEDMODULES * 8 + buttonId - 1, Status); @@ -1149,16 +1176,7 @@ void buttonStatusChanged(int buttonId, byte Status) { #endif } -void axisStatusChanged(int axisId,int mappedValue){ -// #if GAMEPAD_AXIS_01_ENABLED || GAMEPAD_AXIS_02_ENABLED ||GAMEPAD_AXIS_03_ENABLED - #if IC2_SERIAL_BYPASS - arqserial.CustomPacketStart(0x13,2); - arqserial.CustomPacketSendByte(axisId); - arqserial.CustomPacketSendByte(mappedValue); - arqserial.CustomPacketEnd(); - #endif -// #endif -} + #ifdef INCLUDE_BUTTONMATRIX void buttonMatrixStatusChanged(int buttonId, byte Status) { @@ -1176,6 +1194,7 @@ void buttonMatrixStatusChanged(int buttonId, byte Status) { + /***** * * SETUP DEVICES ENABLED @@ -1192,7 +1211,10 @@ void setup() ECrowneWifi::setup(&outgoingStream, &incomingStream); #endif #if IC2_SERIAL_BYPASS + IC2TransportManager::setup(&outgoingStream); + axis1.setCallBack(axisStatusChanged); + #endif //#ifdef INCLUDE_TEMPGAUGE // shTEMPPIN.SetValue((int)80); @@ -1440,26 +1462,23 @@ void UpdateGamepadEncodersState(bool sendState) { #endif #endif + + char loop_opt; char xactionc; unsigned long lastSerialActivity = 0; - /**** * * MAIN LOOP */ -int aAxisCounter=0; void loop() { #if INCLUDE_WIFI ECrowneWifi::loop(); #endif #if IC2_SERIAL_BYPASS - aAxisCounter++; - axisStatusChanged(1,aAxisCounter); - if(aAxisCounter>1024){ - aAxisCounter=0; - } + + IC2TransportManager::loop(); #endif From 71ef04f868d1e69931cc78fc8a4fa032eb67b10f Mon Sep 17 00:00:00 2001 From: wapophis Date: Sun, 31 Mar 2024 23:54:06 +0200 Subject: [PATCH 14/25] Settled high button count --- src/main.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 735eab8..4e2be3e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -561,30 +561,30 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, // ----------------------- ADDITIONAL BUTTONS --------------------------------------------------------------- // https://github.com/zegreatclan/SimHub/wiki/Arduino-Press-Buttons // ---------------------------------------------------------------------------------------------------------- -#define ENABLED_BUTTONS_COUNT 1 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} +#define ENABLED_BUTTONS_COUNT 8 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} #ifdef INCLUDE_BUTTONS #define BUTTON_PIN_1 D3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} #define BUTTON_WIRINGMODE_1 0 //{"Name":"BUTTON_WIRINGMODE_1","Title":"1'st Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=1","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_1 0 //{"Name":"BUTTON_LOGICMODE_1","Title":"1'st Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=1","ListValues":"0,Normal;1,Reversed"} -#define BUTTON_PIN_2 3 //{"Name":"BUTTON_PIN_2","Title":"2'nd Additional button digital pin","DefaultValue":"3","Type":"pin;Button 2","Condition":"ENABLED_BUTTONS_COUNT>=2"} +#define BUTTON_PIN_2 D4 //{"Name":"BUTTON_PIN_2","Title":"2'nd Additional button digital pin","DefaultValue":"3","Type":"pin;Button 2","Condition":"ENABLED_BUTTONS_COUNT>=2"} #define BUTTON_WIRINGMODE_2 0 //{"Name":"BUTTON_WIRINGMODE_2","Title":"2'nd Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=2","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_2 0 //{"Name":"BUTTON_LOGICMODE_2","Title":"2'nd Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=2","ListValues":"0,Normal;1,Reversed"} -#define BUTTON_PIN_3 3 //{"Name":"BUTTON_PIN_3","Title":"3'rd Additional button digital pin","DefaultValue":"3","Type":"pin;Button 3","Condition":"ENABLED_BUTTONS_COUNT>=3"} +#define BUTTON_PIN_3 D5 //{"Name":"BUTTON_PIN_3","Title":"3'rd Additional button digital pin","DefaultValue":"3","Type":"pin;Button 3","Condition":"ENABLED_BUTTONS_COUNT>=3"} #define BUTTON_WIRINGMODE_3 0 //{"Name":"BUTTON_WIRINGMODE_3","Title":"3'rd Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=3","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_3 0 //{"Name":"BUTTON_LOGICMODE_3","Title":"3'rd Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=3","ListValues":"0,Normal;1,Reversed"} -#define BUTTON_PIN_4 3 //{"Name":"BUTTON_PIN_4","Title":"4'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 4","Condition":"ENABLED_BUTTONS_COUNT>=4"} +#define BUTTON_PIN_4 D6 //{"Name":"BUTTON_PIN_4","Title":"4'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 4","Condition":"ENABLED_BUTTONS_COUNT>=4"} #define BUTTON_WIRINGMODE_4 0 //{"Name":"BUTTON_WIRINGMODE_4","Title":"4'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=4","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_4 0 //{"Name":"BUTTON_LOGICMODE_4","Title":"4'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=4","ListValues":"0,Normal;1,Reversed"} -#define BUTTON_PIN_5 3 //{"Name":"BUTTON_PIN_5","Title":"5'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 5","Condition":"ENABLED_BUTTONS_COUNT>=5"} +#define BUTTON_PIN_5 D7 //{"Name":"BUTTON_PIN_5","Title":"5'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 5","Condition":"ENABLED_BUTTONS_COUNT>=5"} #define BUTTON_WIRINGMODE_5 0 //{"Name":"BUTTON_WIRINGMODE_5","Title":"5'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_5 0 //{"Name":"BUTTON_LOGICMODE_5","Title":"5'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Normal;1,Reversed"} -#define BUTTON_PIN_6 3 //{"Name":"BUTTON_PIN_6","Title":"6'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 6","Condition":"ENABLED_BUTTONS_COUNT>=6"} +#define BUTTON_PIN_6 D8 //{"Name":"BUTTON_PIN_6","Title":"6'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 6","Condition":"ENABLED_BUTTONS_COUNT>=6"} #define BUTTON_WIRINGMODE_6 0 //{"Name":"BUTTON_WIRINGMODE_6","Title":"6'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_6 0 //{"Name":"BUTTON_LOGICMODE_6","Title":"6'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Normal;1,Reversed"} @@ -592,7 +592,7 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, #define BUTTON_WIRINGMODE_7 0 //{"Name":"BUTTON_WIRINGMODE_7","Title":"7'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=7","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_7 0 //{"Name":"BUTTON_LOGICMODE_7","Title":"7'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=7","ListValues":"0,Normal;1,Reversed"} -#define BUTTON_PIN_8 3 //{"Name":"BUTTON_PIN_8","Title":"8'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 8","Condition":"ENABLED_BUTTONS_COUNT>=8"} +#define BUTTON_PIN_8 1 //{"Name":"BUTTON_PIN_8","Title":"8'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 8","Condition":"ENABLED_BUTTONS_COUNT>=8"} #define BUTTON_WIRINGMODE_8 0 //{"Name":"BUTTON_WIRINGMODE_8","Title":"8'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=8","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_8 0 //{"Name":"BUTTON_LOGICMODE_8","Title":"8'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=8","ListValues":"0,Normal;1,Reversed"} From e4e7f75a94d439b4143c3173edfe500fb7e0b341 Mon Sep 17 00:00:00 2001 From: wapophis Date: Mon, 1 Apr 2024 00:23:05 +0200 Subject: [PATCH 15/25] Added buttons types, 0 is a physycall button, 0 is a serialed button --- .vscode/settings.json | 36 ++++++++++++++ lib/I2CSerialBridge/I2CManager.h | 20 ++++---- lib/I2CSerialBridge/I2CSerialBridge.h | 10 ++-- lib/I2CSerialBridge/I2CTransportMaster.h | 26 +++++----- src/main.cpp | 62 ++++++++++++++++-------- 5 files changed, 105 insertions(+), 49 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ff59f8e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,36 @@ +{ + "files.associations": { + "array": "cpp", + "string": "cpp", + "string_view": "cpp", + "ranges": "cpp", + "deque": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "initializer_list": "cpp", + "regex": "cpp", + "*.tcc": "cpp", + "optional": "cpp", + "istream": "cpp", + "ostream": "cpp", + "system_error": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "bitset": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "typeinfo": "cpp", + "random": "cpp", + "algorithm": "cpp", + "iterator": "cpp", + "map": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "set": "cpp", + "cstddef": "cpp" + } +} \ No newline at end of file diff --git a/lib/I2CSerialBridge/I2CManager.h b/lib/I2CSerialBridge/I2CManager.h index aa1fb10..1ad5344 100644 --- a/lib/I2CSerialBridge/I2CManager.h +++ b/lib/I2CSerialBridge/I2CManager.h @@ -5,14 +5,14 @@ /*** * "This Manager is responsible for configuring the behavior based on the master-slave role for I2C communication." */ -class IC2TransportManager { +class I2CTransportManager { public: static I2CTransportMaster tm; static I2CTransportSlave ts; static void setup(FullLoopbackStream *outgoingStream){ - #if IC2_MASTER - #if IC2_SERIAL_BYPASS_DEBUG + #if I2C_MASTER + #if I2C_SERIAL_BYPASS_DEBUG Serial.begin(115200); Serial.print("\nSetup as Master\n"); Serial.flush(); @@ -20,40 +20,40 @@ class IC2TransportManager { tm.setup(outgoingStream); #endif - #if IC2_SLAVE + #if I2C_BYPASS_SLAVE Serial.print("Setup as Slave"); Serial.flush(); ts.setup(outgoingStream); #endif } static void loop(){ - #ifdef IC2_MASTER + #ifdef I2C_MASTER tm.loop(); #endif - #ifdef IC2_SLAVE + #ifdef I2C_BYPASS_SLAVE ts.loop(); #endif } static void flush(){ - #ifdef IC2_MASTER + #ifdef I2C_MASTER tm.flush(); #endif - #ifdef IC2_SLAVE + #ifdef I2C_BYPASS_SLAVE ts.flush(); #endif } }; -#if IC2_MASTER +#if I2C_MASTER #define StreamAvailable WIRE.available #define FlowSerialFlush WIRE.flush #define StreamFlush WIRE.flush #define StreamWrite outgoingStream.write #define StreamPrint WIRE.print -/** SETUP SERIAL BYPASS IC2 SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ +/** SETUP SERIAL BYPASS I2C SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ #define FlowSerialBegin [](unsigned long baud) { Serial.printf("Hola mundo");} #endif // #if IC_SLAVE diff --git a/lib/I2CSerialBridge/I2CSerialBridge.h b/lib/I2CSerialBridge/I2CSerialBridge.h index 0363b7b..5bb1aed 100644 --- a/lib/I2CSerialBridge/I2CSerialBridge.h +++ b/lib/I2CSerialBridge/I2CSerialBridge.h @@ -51,8 +51,8 @@ class I2CSerialBridge{ } - /** SETUP SERIAL BYPASS IC2 MASTER, USE WHEN THIS DEVICE COMMAND THE SENDING WORKFLOW*/ - void ic2SetupMaster(){ + /** SETUP SERIAL BYPASS I2C MASTER, USE WHEN THIS DEVICE COMMAND THE SENDING WORKFLOW*/ + void i2cSetupMaster(){ WIRE.begin(); while(!isSlaveAvailable()){ @@ -97,12 +97,12 @@ uint8_t endWireTransmission(bool stop){ } bool isSlaveAvailable(){ - #if IC2_SERIAL_BYPASS_DEBUG + #if I2C_SERIAL_BYPASS_DEBUG Serial.print("\n Testing slave availability -> Beging transmission to "); - Serial.print(IC2_ADDRESS); + Serial.print(I2C_ADDRESS); Serial.println("\n"); #endif - WIRE.beginTransmission(IC2_ADDRESS); + WIRE.beginTransmission(I2C_ADDRESS); uint8_t error = endWireTransmission(true); if(error==0){ diff --git a/lib/I2CSerialBridge/I2CTransportMaster.h b/lib/I2CSerialBridge/I2CTransportMaster.h index af5321e..87eb8c8 100644 --- a/lib/I2CSerialBridge/I2CTransportMaster.h +++ b/lib/I2CSerialBridge/I2CTransportMaster.h @@ -7,15 +7,15 @@ #error WIRE must be settled to have a correct custom wire config in your MASTER config. #endif -#if IC2_MASTER -I2CSerialBridge instance(IC2_ADDRESS); +#if I2C_MASTER +I2CSerialBridge instance(I2C_ADDRESS); class I2CTransportMaster : public I2CTransport { public: I2CTransportMaster(){} void setup(Stream *outgoingStream){ - ic2SetupSerialBypass(); + i2cSetupSerialBypass(); instance.setup((FullLoopbackStream*) outgoingStream); } void loop() { @@ -26,35 +26,35 @@ class I2CTransportMaster : public I2CTransport { } - void ic2SetupSerialBypass(){ - #if IC2_SERIAL_BYPASS_DEBUG + void i2cSetupSerialBypass(){ + #if I2C_SERIAL_BYPASS_DEBUG Serial.print("\n Setting up the master slave connection with slave at "); - Serial.print(IC2_ADDRESS); + Serial.print(I2C_ADDRESS); Serial.println("\n"); #endif - #if IC2_MASTER - instance.ic2SetupMaster(); + #if I2C_MASTER + instance.i2cSetupMaster(); #endif } // static size_t write(const uint8_t *buffer, size_t size){ - // Wire.beginTransmission(IC2_ADDRESS); + // Wire.beginTransmission(I2C_ADDRESS); // Wire.write(buffer,size); // return Wire.endTransmission(); // } // static size_t write(const char *buffer, size_t size){ - // Wire.beginTransmission(IC2_ADDRESS); + // Wire.beginTransmission(I2C_ADDRESS); // Wire.write(buffer,size); // return Wire.endTransmission(); // } // static size_t write(const char *str){ - // Wire.beginTransmission(IC2_ADDRESS); + // Wire.beginTransmission(I2C_ADDRESS); // Wire.write(str); // return Wire.endTransmission(); // } // static size_t write(uint8_t data){ - // Wire.beginTransmission(IC2_ADDRESS); + // Wire.beginTransmission(I2C_ADDRESS); // Wire.write(data); // return Wire.endTransmission(); // } @@ -68,6 +68,6 @@ class I2CTransportMaster : public I2CTransport { // #define StreamFlush WIRE.flush // #define StreamWrite outgoingStream.write // #define StreamPrint WIRE.print -// /** SETUP SERIAL BYPASS IC2 SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ +// /** SETUP SERIAL BYPASS I2C SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ // #define FlowSerialBegin [](unsigned long baud) { Serial.printf("Hola mundo");} #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4e2be3e..671b503 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,19 +9,19 @@ // Less secure if you plan to commit or share your files, but saves a bunch of memory. // If you hardcode credentials the device will only work in your network #define USE_HARDCODED_CREDENTIALS false -#define IC2_SERIAL_BYPASS true +#define I2C_SERIAL_BYPASS true -#if IC2_SERIAL_BYPASS +#if I2C_SERIAL_BYPASS #define WIRE Wire - #define IC2_MASTER true - #define IC2_SLAVE false - #define IC2_ADDRESS 0x08 - #define IC2_SERIAL_BYPASS_DEBUG true + #define I2C_MASTER true + #define I2C_BYPASS_SLAVE false + #define I2C_ADDRESS 0x08 + #define I2C_SERIAL_BYPASS_DEBUG true #include #include FullLoopbackStream outgoingStream; - AnalogAxisSimulator axis1(1); + //AnalogAxisSimulator axis1(1); #endif #if INCLUDE_WIFI @@ -564,58 +564,72 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, #define ENABLED_BUTTONS_COUNT 8 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} #ifdef INCLUDE_BUTTONS -#define BUTTON_PIN_1 D3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} +#define BUTTON_PIN_1 D3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} #define BUTTON_WIRINGMODE_1 0 //{"Name":"BUTTON_WIRINGMODE_1","Title":"1'st Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=1","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_1 0 //{"Name":"BUTTON_LOGICMODE_1","Title":"1'st Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=1","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_1 0 //{"Name":"BUTTON_TYPE_1","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=1","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_2 D4 //{"Name":"BUTTON_PIN_2","Title":"2'nd Additional button digital pin","DefaultValue":"3","Type":"pin;Button 2","Condition":"ENABLED_BUTTONS_COUNT>=2"} #define BUTTON_WIRINGMODE_2 0 //{"Name":"BUTTON_WIRINGMODE_2","Title":"2'nd Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=2","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_2 0 //{"Name":"BUTTON_LOGICMODE_2","Title":"2'nd Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=2","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_2 0 //{"Name":"BUTTON_TYPE_2","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=2","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_3 D5 //{"Name":"BUTTON_PIN_3","Title":"3'rd Additional button digital pin","DefaultValue":"3","Type":"pin;Button 3","Condition":"ENABLED_BUTTONS_COUNT>=3"} #define BUTTON_WIRINGMODE_3 0 //{"Name":"BUTTON_WIRINGMODE_3","Title":"3'rd Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=3","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_3 0 //{"Name":"BUTTON_LOGICMODE_3","Title":"3'rd Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=3","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_3 0 //{"Name":"BUTTON_TYPE_3","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=3","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_4 D6 //{"Name":"BUTTON_PIN_4","Title":"4'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 4","Condition":"ENABLED_BUTTONS_COUNT>=4"} #define BUTTON_WIRINGMODE_4 0 //{"Name":"BUTTON_WIRINGMODE_4","Title":"4'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=4","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_4 0 //{"Name":"BUTTON_LOGICMODE_4","Title":"4'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=4","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_4 0 //{"Name":"BUTTON_TYPE_4","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=4","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_5 D7 //{"Name":"BUTTON_PIN_5","Title":"5'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 5","Condition":"ENABLED_BUTTONS_COUNT>=5"} #define BUTTON_WIRINGMODE_5 0 //{"Name":"BUTTON_WIRINGMODE_5","Title":"5'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_5 0 //{"Name":"BUTTON_LOGICMODE_5","Title":"5'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_5 0 //{"Name":"BUTTON_TYPE_5","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_6 D8 //{"Name":"BUTTON_PIN_6","Title":"6'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 6","Condition":"ENABLED_BUTTONS_COUNT>=6"} #define BUTTON_WIRINGMODE_6 0 //{"Name":"BUTTON_WIRINGMODE_6","Title":"6'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_6 0 //{"Name":"BUTTON_LOGICMODE_6","Title":"6'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_6 0 //{"Name":"BUTTON_TYPE_6","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_7 3 //{"Name":"BUTTON_PIN_7","Title":"7'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 7","Condition":"ENABLED_BUTTONS_COUNT>=7"} #define BUTTON_WIRINGMODE_7 0 //{"Name":"BUTTON_WIRINGMODE_7","Title":"7'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=7","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_7 0 //{"Name":"BUTTON_LOGICMODE_7","Title":"7'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=7","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_7 0 //{"Name":"BUTTON_TYPE_7","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=7","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_8 1 //{"Name":"BUTTON_PIN_8","Title":"8'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 8","Condition":"ENABLED_BUTTONS_COUNT>=8"} #define BUTTON_WIRINGMODE_8 0 //{"Name":"BUTTON_WIRINGMODE_8","Title":"8'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=8","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_8 0 //{"Name":"BUTTON_LOGICMODE_8","Title":"8'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=8","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_8 0 //{"Name":"BUTTON_TYPE_8","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=8","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_9 3 //{"Name":"BUTTON_PIN_9","Title":"9'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 9","Condition":"ENABLED_BUTTONS_COUNT>=9"} #define BUTTON_WIRINGMODE_9 0 //{"Name":"BUTTON_WIRINGMODE_9","Title":"9'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=9","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_9 0 //{"Name":"BUTTON_LOGICMODE_9","Title":"9'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=9","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_9 0 //{"Name":"BUTTON_TYPE_9","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=9","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_10 3 //{"Name":"BUTTON_PIN_10","Title":"10'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 10","Condition":"ENABLED_BUTTONS_COUNT>=10"} #define BUTTON_WIRINGMODE_10 0 //{"Name":"BUTTON_WIRINGMODE_10","Title":"10'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=10","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_10 0 //{"Name":"BUTTON_LOGICMODE_10","Title":"10'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=10","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_10 0 //{"Name":"BUTTON_TYPE_10","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=10","ListValues":"0,Physically Connected;1,Serialized"} #define BUTTON_PIN_11 3 //{"Name":"BUTTON_PIN_11","Title":"11'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 11","Condition":"ENABLED_BUTTONS_COUNT>=11"} #define BUTTON_WIRINGMODE_11 0 //{"Name":"BUTTON_WIRINGMODE_11","Title":"11'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=11","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_11 0 //{"Name":"BUTTON_LOGICMODE_11","Title":"11'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=11","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_11 0 //{"Name":"BUTTON_TYPE_11","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=11","ListValues":"0,Physically Connected;1,Serialized"} -#define BUTTON_PIN_12 3 //{"Name":"BUTTON_PIN_12","Title":"12'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 12","Condition":"ENABLED_BUTTONS_COUNT>=12"} +#define BUTTON_PIN_12 42 //{"Name":"BUTTON_PIN_12","Title":"12'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 12","Condition":"ENABLED_BUTTONS_COUNT>=12"} #define BUTTON_WIRINGMODE_12 0 //{"Name":"BUTTON_WIRINGMODE_12","Title":"12'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=12","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_12 0 //{"Name":"BUTTON_LOGICMODE_12","Title":"12'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=12","ListValues":"0,Normal;1,Reversed"} +#define BUTTON_TYPE_12 0 //{"Name":"BUTTON_TYPE_12","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=12","ListValues":"0,Physically Connected;1,Serialized"} int BUTTON_PINS[] = { BUTTON_PIN_1, BUTTON_PIN_2, BUTTON_PIN_3, BUTTON_PIN_4, BUTTON_PIN_5, BUTTON_PIN_6, BUTTON_PIN_7, BUTTON_PIN_8, BUTTON_PIN_9,BUTTON_PIN_10,BUTTON_PIN_11,BUTTON_PIN_12 }; int BUTTON_WIRING_MODES[] = { BUTTON_WIRINGMODE_1, BUTTON_WIRINGMODE_2, BUTTON_WIRINGMODE_3, BUTTON_WIRINGMODE_4, BUTTON_WIRINGMODE_5, BUTTON_WIRINGMODE_6, BUTTON_WIRINGMODE_7, BUTTON_WIRINGMODE_8, BUTTON_WIRINGMODE_9,BUTTON_WIRINGMODE_10,BUTTON_WIRINGMODE_11,BUTTON_WIRINGMODE_12 }; int BUTTON_LOGIC_MODES[] = { BUTTON_LOGICMODE_1, BUTTON_LOGICMODE_2, BUTTON_LOGICMODE_3, BUTTON_LOGICMODE_4, BUTTON_LOGICMODE_5, BUTTON_LOGICMODE_6, BUTTON_LOGICMODE_7, BUTTON_LOGICMODE_8, BUTTON_LOGICMODE_9, BUTTON_LOGICMODE_10, BUTTON_LOGICMODE_11, BUTTON_LOGICMODE_12 }; +int BUTTON_TYPE[]={BUTTON_TYPE_1,BUTTON_TYPE_2,BUTTON_TYPE_3,BUTTON_TYPE_4,BUTTON_TYPE_5,BUTTON_TYPE_6,BUTTON_TYPE_7,BUTTON_TYPE_8,BUTTON_TYPE_9,BUTTON_TYPE_10,BUTTON_TYPE_11,BUTTON_TYPE_12}; + SHButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12; SHButton* BUTTONS[] = { &button1, &button2, &button3, &button4, &button5, &button6, &button7, &button8 , &button9, &button10, &button11, &button12 }; @@ -1014,11 +1028,11 @@ void idle(bool critical) { yield(); ECrowneWifi::flush(); #endif -#if IC2_SERIAL_BYPASS - axis1.read(10); +#if I2C_SERIAL_BYPASS + //axis1.read(10); yield(); - IC2TransportManager::flush(); + I2CTransportManager::flush(); #endif #if(GAMEPAD_AXIS_01_ENABLED == 1) @@ -1137,8 +1151,8 @@ void EncoderPositionChanged(int encoderId, int position, byte direction) { * */ void axisStatusChanged(int axisId,int mappedValue){ - #if IC2_SERIAL_BYPASS - if(IC2_SERIAL_BYPASS_DEBUG){ + #if I2C_SERIAL_BYPASS + if(I2C_SERIAL_BYPASS_DEBUG){ Serial.print("AxisStatusChanged"); } @@ -1159,7 +1173,7 @@ void buttonStatusChanged(int buttonId, byte Status) { Joystick.setButton(TM1638_ENABLEDMODULES * 8 + buttonId - 1, Status); Joystick.sendState(); #else - // #if IC2_SERIAL_BYPASS + // #if I2C_SERIAL_BYPASS // Wire.beginTransmission(0x08); /* begin with device address 8 */ // arqserial.I2CustomPacketStart(0x03,2); // arqserial.I2CustomPacketSendByte(buttonId); @@ -1210,10 +1224,10 @@ void setup() #if INCLUDE_WIFI ECrowneWifi::setup(&outgoingStream, &incomingStream); #endif -#if IC2_SERIAL_BYPASS +#if I2C_SERIAL_BYPASS - IC2TransportManager::setup(&outgoingStream); - axis1.setCallBack(axisStatusChanged); + I2CTransportManager::setup(&outgoingStream); + //axis1.setCallBack(axisStatusChanged); #endif //#ifdef INCLUDE_TEMPGAUGE @@ -1342,7 +1356,13 @@ void setup() #ifdef INCLUDE_BUTTONS // EXTERNAL BUTTONS INIT for (int btnIdx = 0; btnIdx < ENABLED_BUTTONS_COUNT; btnIdx++) { - BUTTONS[btnIdx]->begin(btnIdx + 1, BUTTON_PINS[btnIdx], buttonStatusChanged, BUTTON_WIRING_MODES[btnIdx], BUTTON_LOGIC_MODES[btnIdx]); + #if I2C_SERIAL_BYPASS + if(BUTTON_TYPE[btnIdx]==0 && I2C_BYPASS_SLAVE){ + BUTTONS[btnIdx]->begin(btnIdx + 1, BUTTON_PINS[btnIdx], buttonStatusChanged, BUTTON_WIRING_MODES[btnIdx], BUTTON_LOGIC_MODES[btnIdx]); + } + #else + BUTTONS[btnIdx]->begin(btnIdx + 1, BUTTON_PINS[btnIdx], buttonStatusChanged, BUTTON_WIRING_MODES[btnIdx], BUTTON_LOGIC_MODES[btnIdx]); + #endif } #endif @@ -1476,10 +1496,10 @@ void loop() { ECrowneWifi::loop(); #endif -#if IC2_SERIAL_BYPASS +#if I2C_SERIAL_BYPASS - IC2TransportManager::loop(); + I2CTransportManager::loop(); #endif #ifdef INCLUDE_SHAKEITL298N From 3da22d3a9fc76f07c08b5c04d512597181782eee Mon Sep 17 00:00:00 2001 From: wapophis Date: Mon, 1 Apr 2024 19:30:32 +0200 Subject: [PATCH 16/25] Fixed build, sending at least two buttons. --- lib/I2CSerialBridge/I2CSerialBridge.h | 24 ++++++++++++++++++------ platformio.ini | 26 ++++++++++++++++++++++++-- src/main.cpp | 16 ++++++++++------ 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/lib/I2CSerialBridge/I2CSerialBridge.h b/lib/I2CSerialBridge/I2CSerialBridge.h index 5bb1aed..737809e 100644 --- a/lib/I2CSerialBridge/I2CSerialBridge.h +++ b/lib/I2CSerialBridge/I2CSerialBridge.h @@ -21,6 +21,10 @@ class I2CSerialBridge{ } void loop() { + #if I2C_SERIAL_BYPASS_DEBUG + // Serial.println("\n I2CSerialBridge - loop"); + // Serial.flush(); + #endif // put your main code here, to run repeatedly check_status(); this->flush(); @@ -32,9 +36,12 @@ class I2CSerialBridge{ size_t availableLength = this->outgoingStream->available(); if (availableLength) { - #if DEBUG_TCP_BRIDGE + #if I2C_SERIAL_BYPASS_DEBUG + Serial.println("\n I2CSerialBridge - flush"); Serial.printf("flushing with this much data: %d \n", availableLength); #endif + + // read the available data from the stream, and put in in the buffer char sbuf[availableLength]; this->outgoingStream->readBytes(sbuf, availableLength); @@ -43,9 +50,11 @@ class I2CSerialBridge{ WIRE.beginTransmission(address); size_t total = WIRE.write(sbuf, availableLength); WIRE.endTransmission(); - #if DEBUG_TCP_BRIDGE - Serial.printf("\n ---> data sent to client %s: %d bytes \n", client->remoteIP().toString().c_str(), total); - Serial.printf("%d %d\n",sbuf[0], sbuf[1]); + + #if I2C_SERIAL_BYPASS_DEBUG + // Serial.println("\n I2CSerialBridge - flush"); +// Serial.printf("\n ---> data sent to client %s: %d bytes \n", I2C_ADDRESS, total); + // Serial.printf("%d %d\n",sbuf[0], sbuf[1]); #endif } } @@ -64,7 +73,10 @@ class I2CSerialBridge{ private: /** TODO CONTROL THAT TRANSPORT LAYER IS READY AND CONNECTED*/ void check_status(){ - + #if I2C_SERIAL_BYPASS_DEBUG + // Serial.println("\n I2CSerialBridge - check_status"); + // Serial.flush(); + #endif } @@ -72,7 +84,7 @@ class I2CSerialBridge{ uint8_t endWireTransmission(bool stop){ uint8_t error=WIRE.endTransmission(stop); - if(error=0){ + if(error==0){ Serial.print("\n Correct wire close \n"); } if(error==1){ diff --git a/platformio.ini b/platformio.ini index e3e0dfb..4be5017 100644 --- a/platformio.ini +++ b/platformio.ini @@ -57,10 +57,32 @@ build_flags = -w -DESP8266=true monitor_speed = 115200 ;upload_port = COM4 -;build_type = debug # set this to debug only for debugging, as it's slower. -monitor_filters = esp8266_exception_decoder +build_type = debug # set this to debug only for debugging, as it's slower. +monitor_filters = esp8266_exception_decoder, default, debug build_unflags = -fno-rtti # comment this out if you need more ram, but you'll need to make some type assumptions +monitor_rts = 0 +monitor_dtr = 0 +[env:esp8266debug] +platform = espressif8266 +# this should be set to your flavor of esp8266, for instance d1_mini +# BOARD_LIST: https://docs.platformio.org/en/latest/boards/index.html#espressif-8266 +board = nodemcuv2 +framework = arduino +lib_deps = + ${common.lib_deps} + fastled/FastLED@3.4 #newer versions don't seem to work with esp8266 in my tests + # add any libraries that are specific for the es8266 + +build_flags = + -w -DESP8266=true +monitor_speed = 115200 +;upload_port = COM4 +;build_type = debug # set this to debug only for debugging, as it's slower. +monitor_filters = esp8266_exception_decoder, default, debug +build_unflags = -fno-rtti # comment this out if you need more ram, but you'll need to make some type assumptions +monitor_rts = 0 +monitor_dtr = 0 ############################################# # ESP32 diff --git a/src/main.cpp b/src/main.cpp index 671b503..a1b17a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -561,7 +561,7 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, // ----------------------- ADDITIONAL BUTTONS --------------------------------------------------------------- // https://github.com/zegreatclan/SimHub/wiki/Arduino-Press-Buttons // ---------------------------------------------------------------------------------------------------------- -#define ENABLED_BUTTONS_COUNT 8 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} +#define ENABLED_BUTTONS_COUNT 2 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} #ifdef INCLUDE_BUTTONS #define BUTTON_PIN_1 D3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} @@ -1058,7 +1058,13 @@ void idle(bool critical) { bool changed = false; #ifdef INCLUDE_BUTTONS for (int btnIdx = 0; btnIdx < ENABLED_BUTTONS_COUNT; btnIdx++) { - BUTTONS[btnIdx]->read(); + #if I2C_SERIAL_BYPASS + if(BUTTON_TYPE[btnIdx]==0){ + BUTTONS[btnIdx]->read(); + } + #else + BUTTONS[btnIdx]->read(); + #endif } #endif #ifdef INCLUDE_TM1638 @@ -1225,7 +1231,7 @@ void setup() ECrowneWifi::setup(&outgoingStream, &incomingStream); #endif #if I2C_SERIAL_BYPASS - + I2CTransportManager::setup(&outgoingStream); //axis1.setCallBack(axisStatusChanged); @@ -1357,7 +1363,7 @@ void setup() // EXTERNAL BUTTONS INIT for (int btnIdx = 0; btnIdx < ENABLED_BUTTONS_COUNT; btnIdx++) { #if I2C_SERIAL_BYPASS - if(BUTTON_TYPE[btnIdx]==0 && I2C_BYPASS_SLAVE){ + if(BUTTON_TYPE[btnIdx]==0){ BUTTONS[btnIdx]->begin(btnIdx + 1, BUTTON_PINS[btnIdx], buttonStatusChanged, BUTTON_WIRING_MODES[btnIdx], BUTTON_LOGIC_MODES[btnIdx]); } #else @@ -1497,8 +1503,6 @@ void loop() { #endif #if I2C_SERIAL_BYPASS - - I2CTransportManager::loop(); #endif From 6cca58441a69aec2f33a3b4bfeab0f4ce6f13b71 Mon Sep 17 00:00:00 2001 From: wapophis Date: Tue, 2 Apr 2024 06:25:23 +0200 Subject: [PATCH 17/25] wip testing --- .vscode/settings.json | 3 ++- platformio.ini | 4 ++-- src/main.cpp | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index ff59f8e..17659d6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -32,5 +32,6 @@ "numeric": "cpp", "set": "cpp", "cstddef": "cpp" - } + }, + "cmake.configureOnOpen": false } \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 4be5017..0429cd8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -57,7 +57,7 @@ build_flags = -w -DESP8266=true monitor_speed = 115200 ;upload_port = COM4 -build_type = debug # set this to debug only for debugging, as it's slower. +;build_type = debug # set this to debug only for debugging, as it's slower. monitor_filters = esp8266_exception_decoder, default, debug build_unflags = -fno-rtti # comment this out if you need more ram, but you'll need to make some type assumptions monitor_rts = 0 @@ -78,7 +78,7 @@ build_flags = -w -DESP8266=true monitor_speed = 115200 ;upload_port = COM4 -;build_type = debug # set this to debug only for debugging, as it's slower. +build_type = debug # set this to debug only for debugging, as it's slower. monitor_filters = esp8266_exception_decoder, default, debug build_unflags = -fno-rtti # comment this out if you need more ram, but you'll need to make some type assumptions monitor_rts = 0 diff --git a/src/main.cpp b/src/main.cpp index a1b17a4..d56faa8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -561,7 +561,7 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, // ----------------------- ADDITIONAL BUTTONS --------------------------------------------------------------- // https://github.com/zegreatclan/SimHub/wiki/Arduino-Press-Buttons // ---------------------------------------------------------------------------------------------------------- -#define ENABLED_BUTTONS_COUNT 2 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} +#define ENABLED_BUTTONS_COUNT 5 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} #ifdef INCLUDE_BUTTONS #define BUTTON_PIN_1 D3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} @@ -589,7 +589,7 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, #define BUTTON_LOGICMODE_5 0 //{"Name":"BUTTON_LOGICMODE_5","Title":"5'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Normal;1,Reversed"} #define BUTTON_TYPE_5 0 //{"Name":"BUTTON_TYPE_5","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Physically Connected;1,Serialized"} -#define BUTTON_PIN_6 D8 //{"Name":"BUTTON_PIN_6","Title":"6'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 6","Condition":"ENABLED_BUTTONS_COUNT>=6"} +#define BUTTON_PIN_6 D0 //{"Name":"BUTTON_PIN_6","Title":"6'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 6","Condition":"ENABLED_BUTTONS_COUNT>=6"} #define BUTTON_WIRINGMODE_6 0 //{"Name":"BUTTON_WIRINGMODE_6","Title":"6'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_6 0 //{"Name":"BUTTON_LOGICMODE_6","Title":"6'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Normal;1,Reversed"} #define BUTTON_TYPE_6 0 //{"Name":"BUTTON_TYPE_6","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Physically Connected;1,Serialized"} @@ -1231,7 +1231,8 @@ void setup() ECrowneWifi::setup(&outgoingStream, &incomingStream); #endif #if I2C_SERIAL_BYPASS - + Serial.begin(115200); + Serial.println("MAIN - SETUP - I2C_SERIAL_BYPASS"); I2CTransportManager::setup(&outgoingStream); //axis1.setCallBack(axisStatusChanged); From e423a8ce2a896792de54504522ad371445783644 Mon Sep 17 00:00:00 2001 From: Wapophis Date: Tue, 9 Apr 2024 23:20:18 +0200 Subject: [PATCH 18/25] UPDATED BUTONSTATUS_CHANGED --- lib/I2CSerialBridge/I2CTransport.h | 2 +- src/main.cpp | 17 ++++------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/lib/I2CSerialBridge/I2CTransport.h b/lib/I2CSerialBridge/I2CTransport.h index 4237129..ed0cc37 100644 --- a/lib/I2CSerialBridge/I2CTransport.h +++ b/lib/I2CSerialBridge/I2CTransport.h @@ -1,6 +1,6 @@ #pragma once #include -#include + #include #ifndef WIRE diff --git a/src/main.cpp b/src/main.cpp index d56faa8..a340409 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -561,7 +561,7 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, // ----------------------- ADDITIONAL BUTTONS --------------------------------------------------------------- // https://github.com/zegreatclan/SimHub/wiki/Arduino-Press-Buttons // ---------------------------------------------------------------------------------------------------------- -#define ENABLED_BUTTONS_COUNT 5 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} +#define ENABLED_BUTTONS_COUNT 6 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} #ifdef INCLUDE_BUTTONS #define BUTTON_PIN_1 D3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} @@ -589,7 +589,7 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, #define BUTTON_LOGICMODE_5 0 //{"Name":"BUTTON_LOGICMODE_5","Title":"5'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Normal;1,Reversed"} #define BUTTON_TYPE_5 0 //{"Name":"BUTTON_TYPE_5","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=5","ListValues":"0,Physically Connected;1,Serialized"} -#define BUTTON_PIN_6 D0 //{"Name":"BUTTON_PIN_6","Title":"6'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 6","Condition":"ENABLED_BUTTONS_COUNT>=6"} +#define BUTTON_PIN_6 3 //{"Name":"BUTTON_PIN_6","Title":"6'th Additional button digital pin","DefaultValue":"3","Type":"pin;Button 6","Condition":"ENABLED_BUTTONS_COUNT>=6"} #define BUTTON_WIRINGMODE_6 0 //{"Name":"BUTTON_WIRINGMODE_6","Title":"6'th Additional button wiring","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Pin to GND;1,VCC to pin"} #define BUTTON_LOGICMODE_6 0 //{"Name":"BUTTON_LOGICMODE_6","Title":"6'th Additional button logic","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Normal;1,Reversed"} #define BUTTON_TYPE_6 0 //{"Name":"BUTTON_TYPE_6","Title":"Is virtual button","DefaultValue":"0","Type":"list","Condition":"ENABLED_BUTTONS_COUNT>=6","ListValues":"0,Physically Connected;1,Serialized"} @@ -1175,24 +1175,14 @@ void axisStatusChanged(int axisId,int mappedValue){ * * */ void buttonStatusChanged(int buttonId, byte Status) { -#ifdef INCLUDE_GAMEPAD +#ifdef INCLUDE_GAMEPAD && !I2C_BYPASS_MASTER Joystick.setButton(TM1638_ENABLEDMODULES * 8 + buttonId - 1, Status); Joystick.sendState(); #else - // #if I2C_SERIAL_BYPASS - // Wire.beginTransmission(0x08); /* begin with device address 8 */ - // arqserial.I2CustomPacketStart(0x03,2); - // arqserial.I2CustomPacketSendByte(buttonId); - // arqserial.I2CustomPacketSendByte(Status); - // arqserial.I2CustomPacketEnd(); - // endWireTransmission(true); - // #else arqserial.CustomPacketStart(0x03,2); arqserial.CustomPacketSendByte(buttonId); arqserial.CustomPacketSendByte(Status); arqserial.CustomPacketEnd(); -// #endif - #endif } @@ -1230,6 +1220,7 @@ void setup() #if INCLUDE_WIFI ECrowneWifi::setup(&outgoingStream, &incomingStream); #endif + #if I2C_SERIAL_BYPASS Serial.begin(115200); Serial.println("MAIN - SETUP - I2C_SERIAL_BYPASS"); From bd23f1614cd1ba36c9b73360856a07b214684e39 Mon Sep 17 00:00:00 2001 From: Wapophis Date: Tue, 9 Apr 2024 23:21:49 +0200 Subject: [PATCH 19/25] BUTTONMATRIXSTATUDCHANGED UPDATED TO SEND VIA ARQSERIAL WHEN I2C_BYPASS_MASTER IS ENABLED --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index a340409..f8d6580 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1190,7 +1190,7 @@ void buttonStatusChanged(int buttonId, byte Status) { #ifdef INCLUDE_BUTTONMATRIX void buttonMatrixStatusChanged(int buttonId, byte Status) { -#ifdef INCLUDE_GAMEPAD +#ifdef INCLUDE_GAMEPAD && !I2C_BYPASS_MASTER Joystick.setButton(TM1638_ENABLEDMODULES * 8 + ENABLED_BUTTONS_COUNT + buttonId - 1, Status); Joystick.sendState(); #else From d96bb73ec0d0f30fc45ee03a2ed07b0631b9d50f Mon Sep 17 00:00:00 2001 From: Wapophis Date: Sun, 14 Apr 2024 14:05:15 +0200 Subject: [PATCH 20/25] synced with AVR-SIMHUB --- lib/I2CSerialBridge/I2CManager.h | 30 +++++++++++++----------- lib/I2CSerialBridge/I2CSerialBridge.h | 20 ++++++++++------ lib/I2CSerialBridge/I2CTransport.h | 22 ++++++++--------- lib/I2CSerialBridge/I2CTransportMaster.h | 4 ++-- src/ArqSerial.h | 4 ++-- src/main.cpp | 4 ++-- 6 files changed, 46 insertions(+), 38 deletions(-) diff --git a/lib/I2CSerialBridge/I2CManager.h b/lib/I2CSerialBridge/I2CManager.h index 1ad5344..912786f 100644 --- a/lib/I2CSerialBridge/I2CManager.h +++ b/lib/I2CSerialBridge/I2CManager.h @@ -7,11 +7,13 @@ */ class I2CTransportManager { public: + #if I2C_BYPASS_MASTER static I2CTransportMaster tm; + #endif static I2CTransportSlave ts; static void setup(FullLoopbackStream *outgoingStream){ - #if I2C_MASTER + #if I2C_BYPASS_MASTER #if I2C_SERIAL_BYPASS_DEBUG Serial.begin(115200); Serial.print("\nSetup as Master\n"); @@ -27,27 +29,27 @@ class I2CTransportManager { #endif } static void loop(){ - #ifdef I2C_MASTER + #if I2C_BYPASS_MASTER tm.loop(); #endif - #ifdef I2C_BYPASS_SLAVE + #if I2C_BYPASS_SLAVE ts.loop(); #endif } static void flush(){ - #ifdef I2C_MASTER + #if I2C_BYPASS_MASTER tm.flush(); #endif - #ifdef I2C_BYPASS_SLAVE + #if I2C_BYPASS_SLAVE ts.flush(); #endif } }; -#if I2C_MASTER +#if I2C_BYPASS_MASTER #define StreamAvailable WIRE.available #define FlowSerialFlush WIRE.flush #define StreamFlush WIRE.flush @@ -56,11 +58,11 @@ class I2CTransportManager { /** SETUP SERIAL BYPASS I2C SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ #define FlowSerialBegin [](unsigned long baud) { Serial.printf("Hola mundo");} #endif -// #if IC_SLAVE -// #define StreamRead Serial.read -// #define StreamFlush Serial.flush -// #define StreamWrite Serial.write -// #define StreamPrint Serial.print -// #define StreamAvailable Serial.available -// #define FlowSerialBegin [](unsigned long baud) { Serial.begin(baud);} -// #endif \ No newline at end of file + #if I2C_BYPASS_SLAVE + #define StreamRead Serial.read + #define StreamFlush Serial.flush + #define StreamWrite Serial.write + #define StreamPrint Serial.print + #define StreamAvailable Serial.available + #define FlowSerialBegin [](unsigned long baud) { Serial.begin(baud);} + #endif diff --git a/lib/I2CSerialBridge/I2CSerialBridge.h b/lib/I2CSerialBridge/I2CSerialBridge.h index 737809e..dd297b4 100644 --- a/lib/I2CSerialBridge/I2CSerialBridge.h +++ b/lib/I2CSerialBridge/I2CSerialBridge.h @@ -22,8 +22,8 @@ class I2CSerialBridge{ void loop() { #if I2C_SERIAL_BYPASS_DEBUG - // Serial.println("\n I2CSerialBridge - loop"); - // Serial.flush(); + // Serial.print("\n I2CSerialBridge - loop"); + // Serial.flush(); #endif // put your main code here, to run repeatedly check_status(); @@ -38,7 +38,8 @@ class I2CSerialBridge{ { #if I2C_SERIAL_BYPASS_DEBUG Serial.println("\n I2CSerialBridge - flush"); - Serial.printf("flushing with this much data: %d \n", availableLength); + Serial.print("\nflushing with this much data:"); + Serial.print(availableLength); #endif @@ -63,9 +64,10 @@ class I2CSerialBridge{ /** SETUP SERIAL BYPASS I2C MASTER, USE WHEN THIS DEVICE COMMAND THE SENDING WORKFLOW*/ void i2cSetupMaster(){ WIRE.begin(); + Wire.setWireTimeout(300); while(!isSlaveAvailable()){ - Serial.printf("\n Slave device not available, retrying 1 sec later"); + Serial.print("\n Slave device not available, retrying 1 sec later"); delay(1000); }; } @@ -102,7 +104,9 @@ uint8_t endWireTransmission(bool stop){ if(error==5){ Serial.print("timeout"); } - Serial.printf("\n Error code is %d",error); + Serial.print("\n Error code is"); + Serial.print(error); + Serial.println(); Serial.flush(); return error; @@ -118,11 +122,13 @@ bool isSlaveAvailable(){ uint8_t error = endWireTransmission(true); if(error==0){ - Serial.printf("\n Slave device detected at address 8\n"); + Serial.print("\n Slave device detected at address "); + Serial.print(I2C_ADDRESS); + Serial.print("\n"); return true; } return false; } -}; \ No newline at end of file +}; diff --git a/lib/I2CSerialBridge/I2CTransport.h b/lib/I2CSerialBridge/I2CTransport.h index ed0cc37..425226e 100644 --- a/lib/I2CSerialBridge/I2CTransport.h +++ b/lib/I2CSerialBridge/I2CTransport.h @@ -3,19 +3,19 @@ #include -#ifndef WIRE - # define WIRE Wire; -#endif +// #ifndef WIRE +// # define WIRE Wire; +// #endif -#ifndef IC2_MASTER && IC2_SLAVE - #define IC2_MASTER -#endif +// #if I2C_BYPASS_MASTER && IC2_SLAVE +// #define I2C_BYPASS_MASTER +// #endif -#ifndef IC2_ADDRESS - #define IC2_ADDRESS 0x08 -#endif -#define StreamRead WIRE.read -// #if IC2_MASTER +// #ifndef IC2_ADDRESS +// #define IC2_ADDRESS 0x08 +// #endif +// #define StreamRead WIRE.read +// #if I2C_BYPASS_MASTER // ic2SetupMaster(); // #endif // #if IC2_SLAVE diff --git a/lib/I2CSerialBridge/I2CTransportMaster.h b/lib/I2CSerialBridge/I2CTransportMaster.h index 87eb8c8..5d93ad5 100644 --- a/lib/I2CSerialBridge/I2CTransportMaster.h +++ b/lib/I2CSerialBridge/I2CTransportMaster.h @@ -7,7 +7,7 @@ #error WIRE must be settled to have a correct custom wire config in your MASTER config. #endif -#if I2C_MASTER +#if I2C_BYPASS_MASTER I2CSerialBridge instance(I2C_ADDRESS); class I2CTransportMaster : public I2CTransport { @@ -32,7 +32,7 @@ class I2CTransportMaster : public I2CTransport { Serial.print(I2C_ADDRESS); Serial.println("\n"); #endif - #if I2C_MASTER + #if I2C_BYPASS_MASTER instance.i2cSetupMaster(); #endif diff --git a/src/ArqSerial.h b/src/ArqSerial.h index 5d6fffa..1681f6a 100644 --- a/src/ArqSerial.h +++ b/src/ArqSerial.h @@ -210,7 +210,7 @@ class ARQSerial if (DataBuffer.size() > 0) { uint8_t res = 0; DataBuffer.pop(res); - Serial1.printf("\nread[data]: [%d]",res); + //Serial1.print("\nread[data]: [%d]",res); return (int)res; } @@ -235,7 +235,7 @@ class ARQSerial StreamWrite(0x08); StreamWrite(data); StreamFlush(); - Serial1.printf("\nWrite[start,data]: [0x08,%d]",data); + //Serial1.printf("\nWrite[start,data]: [0x08,%d]",data); } void Print(char data) diff --git a/src/main.cpp b/src/main.cpp index f8d6580..3a8d8bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1406,8 +1406,8 @@ void setup() shCustomProtocol.setup(); arqserial.setIdleFunction(idle); - Serial.begin(115200); - delay(200); + // Serial.begin(115200); + // delay(200); #if(GAMEPAD_AXIS_01_ENABLED == 1) SHGAMEPADAXIS01.SetJoystick(&Joystick); From be94ac8b974dd8b487cc0e99b88db88778a49d35 Mon Sep 17 00:00:00 2001 From: Wapophis Date: Fri, 19 Apr 2024 08:40:28 +0200 Subject: [PATCH 21/25] MINOR FIXES --- lib/I2CSerialBridge/I2CSerialBridge.h | 2 +- lib/I2CSerialBridge/I2CTransportSlave.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/I2CSerialBridge/I2CSerialBridge.h b/lib/I2CSerialBridge/I2CSerialBridge.h index dd297b4..f33de58 100644 --- a/lib/I2CSerialBridge/I2CSerialBridge.h +++ b/lib/I2CSerialBridge/I2CSerialBridge.h @@ -64,7 +64,7 @@ class I2CSerialBridge{ /** SETUP SERIAL BYPASS I2C MASTER, USE WHEN THIS DEVICE COMMAND THE SENDING WORKFLOW*/ void i2cSetupMaster(){ WIRE.begin(); - Wire.setWireTimeout(300); + WIRE.setTimeout(300); while(!isSlaveAvailable()){ Serial.print("\n Slave device not available, retrying 1 sec later"); diff --git a/lib/I2CSerialBridge/I2CTransportSlave.h b/lib/I2CSerialBridge/I2CTransportSlave.h index defe5c2..e98f748 100644 --- a/lib/I2CSerialBridge/I2CTransportSlave.h +++ b/lib/I2CSerialBridge/I2CTransportSlave.h @@ -35,7 +35,7 @@ class I2CTransportSlave : public I2CTransport { }; void ic2SetupSlave(){ - WIRE.begin(IC2_ADDRESS); + WIRE.begin(I2C_ADDRESS); /** * BECAUSE THERE IS NO JOYSTICK SUPPORT IN THE ESP IMPL DEFAULT IMPL FOR ESP IS THIS IMPL TO RESEND ALL THE FLOW * TO THE TRANSPORT STREAM WHICH IS CONFIGURED FOR THE DEVICE. From d4386eaca81b84ee215231702929142a602eb308 Mon Sep 17 00:00:00 2001 From: wapophis Date: Fri, 19 Apr 2024 20:46:42 +0200 Subject: [PATCH 22/25] updated protocol decoder from avr-simhub --- .../SimHubProtocolDecoder.h | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h b/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h index b88d37c..eb2ab07 100644 --- a/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h +++ b/lib/SimHubProtocolDecoder/SimHubProtocolDecoder.h @@ -38,6 +38,12 @@ class EventCallBackManager{ static void decodeBuffer(EventCallBackManager *callbacker,Stream *stream){ byte packetType=0x0; int size=-1; + //TODO: REMOVE IS FOR TESTING + // while(stream->available()){ + // Serial.write(stream->read()); + // } + // return; + #define IC2_SERIAL_BYPASS_DEBUG false #if IC2_SERIAL_BYPASS_DEBUG Serial.print("\n Disponible en buffer "); Serial.print(stream->available()); @@ -47,7 +53,7 @@ static void decodeBuffer(EventCallBackManager *callbacker,Stream *stream){ if(stream->available()){ packetType=stream->read(); #if IC2_SERIAL_BYPASS_DEBUG - Serial.print("\n packetType "); + Serial.print("\n Initial packetType "); Serial.print(packetType); Serial.print("\n"); Serial.flush(); @@ -57,9 +63,8 @@ static void decodeBuffer(EventCallBackManager *callbacker,Stream *stream){ // CUSTOM PACKETS if(packetType==0x09){ packetType=stream->read(); - #if IC2_SERIAL_BYPASS_DEBUG - Serial.print("\n packetType "); + Serial.print("\n Accepted packetType "); Serial.print(packetType); Serial.print("\n"); Serial.flush(); @@ -74,35 +79,32 @@ static void decodeBuffer(EventCallBackManager *callbacker,Stream *stream){ Serial.flush(); #endif + if(packetType==0x01){ + int encoderId=stream->read(); + byte direction=stream->read(); + int position=stream->read(); + callbacker->getEncoderPositionChangedCallback()(encoderId,position,direction); + } - switch (packetType){ - case 0x01: - // callbacker->getEncoderPositionChangedCallback()(stream->read(),stream->read(),stream->read()); - break; - case 0x02: - //callbacker->getEncoderPositionChangedCallback()(stream->read(),stream->read(),stream->read()); - break; - case 0x03: - int buttonId; - buttonId=stream->read(); - byte status; - status=stream->read(); - #if IC2_SERIAL_BYPASS_DEBUG - Serial.print("\n BUtton state changed "); - Serial.println(buttonId); - Serial.println(status); - Serial.flush(); - #endif - callbacker->getButtonCallback()(buttonId,status); - break; - case 0x04: - // TODO: FIXME - //callbacker->getButtonCallback()((stream->read())*8+stream->read(),stream->read()); - break; - default: - break; - } + if(packetType==0x02){ + int encoderId=stream->read(); + byte direction=stream->read(); + callbacker->getEncoderPositionChangedCallback()(encoderId,direction,0xD7); // 0XD7 Identify a button status changed + } + if(packetType==0x03){ + int buttonId; + buttonId=stream->read(); + byte status; + status=stream->read(); + #if IC2_SERIAL_BYPASS_DEBUG + Serial.print("\n BUtton state changed "); + Serial.println(buttonId); + Serial.println(status); + Serial.flush(); + #endif + callbacker->getButtonCallback()(buttonId,status); + } while (0 available()){ Serial.write(stream->read()); From 899fa4ba28e7bd4b95dda40c2806960b58b4cab2 Mon Sep 17 00:00:00 2001 From: wapophis Date: Fri, 19 Apr 2024 21:39:02 +0200 Subject: [PATCH 23/25] rebased I2CSerialBrdige module and ADDED SHVIRTUALROTARY --- lib/I2CSerialBridge/I2CManager.h | 15 ++-- lib/I2CSerialBridge/I2CSerialBridge.h | 35 +++++--- lib/I2CSerialBridge/I2CTransport.h | 19 ---- lib/I2CSerialBridge/I2CTransportMaster.h | 33 ------- lib/I2CSerialBridge/I2CTransportSlave.h | 4 + .../SHRotaryEncodersContext.h | 78 +++++++++++++++++ .../SHVirtualRotaryEncoder.h | 87 +++++++++++++++++++ 7 files changed, 202 insertions(+), 69 deletions(-) create mode 100644 lib/SHRotaryEncondersContext/SHRotaryEncodersContext.h create mode 100644 lib/SHRotaryEncondersContext/SHVirtualRotaryEncoder.h diff --git a/lib/I2CSerialBridge/I2CManager.h b/lib/I2CSerialBridge/I2CManager.h index 912786f..b94e646 100644 --- a/lib/I2CSerialBridge/I2CManager.h +++ b/lib/I2CSerialBridge/I2CManager.h @@ -15,7 +15,6 @@ class I2CTransportManager { static void setup(FullLoopbackStream *outgoingStream){ #if I2C_BYPASS_MASTER #if I2C_SERIAL_BYPASS_DEBUG - Serial.begin(115200); Serial.print("\nSetup as Master\n"); Serial.flush(); #endif @@ -50,13 +49,15 @@ class I2CTransportManager { }; #if I2C_BYPASS_MASTER -#define StreamAvailable WIRE.available -#define FlowSerialFlush WIRE.flush -#define StreamFlush WIRE.flush +#define StreamRead outgoingStream.read +#define StreamAvailable outgoingStream.available +#define FlowSerialFlush Serial.flush +#define StreamFlush I2CTransportManager::flush #define StreamWrite outgoingStream.write -#define StreamPrint WIRE.print +//#define StreamWrite WIRE.write +#define StreamPrint outgoingStream.print /** SETUP SERIAL BYPASS I2C SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ -#define FlowSerialBegin [](unsigned long baud) { Serial.printf("Hola mundo");} +#define FlowSerialBegin [](unsigned long baud) { Serial.print("Hola mundo");} #endif #if I2C_BYPASS_SLAVE #define StreamRead Serial.read @@ -65,4 +66,4 @@ class I2CTransportManager { #define StreamPrint Serial.print #define StreamAvailable Serial.available #define FlowSerialBegin [](unsigned long baud) { Serial.begin(baud);} - #endif + #endif \ No newline at end of file diff --git a/lib/I2CSerialBridge/I2CSerialBridge.h b/lib/I2CSerialBridge/I2CSerialBridge.h index f33de58..6279f4d 100644 --- a/lib/I2CSerialBridge/I2CSerialBridge.h +++ b/lib/I2CSerialBridge/I2CSerialBridge.h @@ -6,7 +6,7 @@ #error WIRE must be settled to have a correct custom wire config in your MASTER config. #endif -class I2CSerialBridge{ +class I2CSerialBridge{ FullLoopbackStream *outgoingStream; uint8_t address; public: @@ -51,6 +51,13 @@ class I2CSerialBridge{ WIRE.beginTransmission(address); size_t total = WIRE.write(sbuf, availableLength); WIRE.endTransmission(); + //endWireTransmission(false); + // DEBUG OUTPUT STREAM + for (int i=0;i -// #ifndef WIRE -// # define WIRE Wire; -// #endif - -// #if I2C_BYPASS_MASTER && IC2_SLAVE -// #define I2C_BYPASS_MASTER -// #endif - -// #ifndef IC2_ADDRESS -// #define IC2_ADDRESS 0x08 -// #endif -// #define StreamRead WIRE.read -// #if I2C_BYPASS_MASTER -// ic2SetupMaster(); -// #endif -// #if IC2_SLAVE -// ic2SetupSlave(); -// #endif - class I2CTransport { public: I2CTransport(){} diff --git a/lib/I2CSerialBridge/I2CTransportMaster.h b/lib/I2CSerialBridge/I2CTransportMaster.h index 5d93ad5..ef727d8 100644 --- a/lib/I2CSerialBridge/I2CTransportMaster.h +++ b/lib/I2CSerialBridge/I2CTransportMaster.h @@ -37,37 +37,4 @@ class I2CTransportMaster : public I2CTransport { #endif } - - // static size_t write(const uint8_t *buffer, size_t size){ - // Wire.beginTransmission(I2C_ADDRESS); - // Wire.write(buffer,size); - // return Wire.endTransmission(); - // } - // static size_t write(const char *buffer, size_t size){ - // Wire.beginTransmission(I2C_ADDRESS); - // Wire.write(buffer,size); - // return Wire.endTransmission(); - // } - // static size_t write(const char *str){ - // Wire.beginTransmission(I2C_ADDRESS); - // Wire.write(str); - // return Wire.endTransmission(); - // } - // static size_t write(uint8_t data){ - // Wire.beginTransmission(I2C_ADDRESS); - // Wire.write(data); - // return Wire.endTransmission(); - // } - -}; - - - -// #define StreamAvailable WIRE.available -// #define FlowSerialFlush WIRE.flush -// #define StreamFlush WIRE.flush -// #define StreamWrite outgoingStream.write -// #define StreamPrint WIRE.print -// /** SETUP SERIAL BYPASS I2C SLAVE, USE WHEN THIS DEVICE IS CONNECTED TO SIMHUB*/ -// #define FlowSerialBegin [](unsigned long baud) { Serial.printf("Hola mundo");} #endif \ No newline at end of file diff --git a/lib/I2CSerialBridge/I2CTransportSlave.h b/lib/I2CSerialBridge/I2CTransportSlave.h index e98f748..3a1661e 100644 --- a/lib/I2CSerialBridge/I2CTransportSlave.h +++ b/lib/I2CSerialBridge/I2CTransportSlave.h @@ -8,6 +8,10 @@ #error WIRE must be settled to have a correct custom wire config in your SLAVE config. #endif +#ifndef I2C_ADDRESS +#error I2C_ADDRESS must be settled to have a correct custom wire config in your SLAVE config. +#endif + class I2CTransportSlave : public I2CTransport { static Stream *outgoingStream; public: diff --git a/lib/SHRotaryEncondersContext/SHRotaryEncodersContext.h b/lib/SHRotaryEncondersContext/SHRotaryEncodersContext.h new file mode 100644 index 0000000..74fedb8 --- /dev/null +++ b/lib/SHRotaryEncondersContext/SHRotaryEncodersContext.h @@ -0,0 +1,78 @@ +#pragma once +#include +#include + +class SHRotaryEncoderContext { +private: + byte direction[8]; + int position[8]; + unsigned long positionLastChanged[8]; + bool buttonPressed[8]; + + +public: + void init(){ + + } + + void updateContext(int encoderId, int pos, byte dir){ + // char sbuf[150]; + // sprintf(sbuf,"\nSHRotaryEncoderContext::updateContext(%d,%d,%d);\n",encoderId,pos,dir); + // Serial.print(sbuf); + if(dir==0xD7){ + this->buttonPressed[encoderId-1]=pos == 1? 0 : 1; + Serial.print(this->buttonPressed[encoderId-1]); + }else{ + this->direction[encoderId-1]=dir; + this->position[encoderId-1]=pos; + this->positionLastChanged[encoderId-1]=millis(); + } + + // sprintf(sbuf,"\nSHRotaryEncoderContext::updateContext::direction:%d",this->direction[encoderId-1]); + // Serial.print(sbuf); + // sprintf(sbuf,"\nSHRotaryEncoderContext::updateContext::position:%d",this->position[encoderId-1]); + // Serial.print(sbuf); + // sprintf(sbuf,"\nSHRotaryEncoderContext::updateContext::positionLastChanged:%lu",this->positionLastChanged[encoderId-1]); + // Serial.print(sbuf); + // Serial.println(""); + + } + + int getPosition(int encoderId){ + return this->position[encoderId-1]; + } + + byte getDirection(int encoderId){ + return this->direction[encoderId-1]; + } + + unsigned long getPositionLastChanged(int encoderId){ + return this->positionLastChanged[encoderId-1]; + } + + bool getButtonState(int encoderId){ + return this->buttonPressed[encoderId-1]; + } + + void logDirection(){ + char sbuf[150]; + for(int i=0;i<8;i++){ + sprintf(sbuf+strlen(sbuf)," %d,",this->direction[i]); + } + // Serial.print("\nSHRotaryEncoderContext::direction: "); + Serial.print(sbuf); + Serial.println(""); + + } + + void logPosition(){ + char sbuf[150]; + for(int i=0;i<8;i++){ + sprintf(sbuf+strlen(sbuf)," %d,",this->position[i]); + } + // Serial.print("\nSHRotaryEncoderContext::position: "); + Serial.print(sbuf); + Serial.println(""); + } + +}; \ No newline at end of file diff --git a/lib/SHRotaryEncondersContext/SHVirtualRotaryEncoder.h b/lib/SHRotaryEncondersContext/SHVirtualRotaryEncoder.h new file mode 100644 index 0000000..d422015 --- /dev/null +++ b/lib/SHRotaryEncondersContext/SHVirtualRotaryEncoder.h @@ -0,0 +1,87 @@ +#pragma once +#ifndef __SHVIRTUALROTARYENCODER_H__ +#define __SHVIRTUALROTARYENCODER_H__ + +#include +#include +#include +#include + +class SHVirtualRotaryEncoder : public SHRotaryEncoder { +private: + SHRotaryEncoderContext* context; + +public: + + SHVirtualRotaryEncoder(SHRotaryEncoderContext* context){ + this->context=context; + } + + void begin(uint8_t outputAPin, uint8_t outputBPin, int buttonPin, bool reverse, bool enablePullup, byte encoderid, bool half, SHRotaryEncoderPositionChanged changedcallback) override{ + this->id=encoderid; + this->inputLastState = 0; + this->buttonLastState = enablePullup? HIGH: LOW; + this->halfSteps=half; + this->positionChangedCallback=changedcallback; + context->updateContext(id,0,255); + } + + uint8_t getDirection(uint8_t delay, unsigned long referenceTime) override{ + // Serial.print(" DirecionLastCHange:"); + // Serial.print(this->context->getDirection(this->id)); + // Serial.print(" | "); + // Serial.print(" ReferenceTime:"); + // Serial.print(referenceTime); + // Serial.print(" | "); + // Serial.print(" getPositionLastChanged:"); + // Serial.print(context->getPositionLastChanged(this->id)); + // Serial.print(" | "); + // Serial.print(" Diff:"); + // Serial.print(referenceTime - context->getPositionLastChanged(this->id)); + // Serial.print(" | \n"); + + if (directionLastChange != 255 && (referenceTime - context->getPositionLastChanged(this->id)) < delay) { + // this->context->updateContext(this->id,counter,directionLastChange); + return directionLastChange; + } + // this->context->updateContext(this->id,counter,255); + return 255; + } + + uint8_t getPressed() override{ + return !buttonLastState; + } + + void read() override{ + + this->direction=this->context->getDirection(this->id); + this->counter=this->context->getPosition(this->id); + + // Serial.print(" SHVirtualRotaryEncoder::read:direction:"); + // Serial.print(this->context->getDirection(this->id)); + // Serial.print(" \n"); + + if (direction == 0) { + counter++; + positionChangedCallback(id, counter, direction); + //positionLastChanged = millis(); + directionLastChange = 0; + } + else if (direction == 1) { + counter--; + positionChangedCallback(id, counter, direction); + //positionLastChanged = millis(); + directionLastChange = 1; + } + + this->buttonLastState=context->getButtonState(id); + + //SHRotaryEncoder::read(); + } + + + +}; + + +#endif \ No newline at end of file From 6ae38be7cfba8cdf6599873601e3c002959e750a Mon Sep 17 00:00:00 2001 From: wapophis Date: Fri, 19 Apr 2024 23:46:40 +0200 Subject: [PATCH 24/25] rebase SHRotaryEncoder from AVR-SIMHUB --- src/SHRotaryEncoder.h | 14 +-- src/main.cpp | 220 ++++++++++++++++++++++++++++++++---------- 2 files changed, 179 insertions(+), 55 deletions(-) diff --git a/src/SHRotaryEncoder.h b/src/SHRotaryEncoder.h index 8da4ce6..af2456b 100644 --- a/src/SHRotaryEncoder.h +++ b/src/SHRotaryEncoder.h @@ -50,7 +50,7 @@ static const unsigned char halfStepsTable[][4] = typedef void(*SHRotaryEncoderPositionChanged) (int, int, byte); class SHRotaryEncoder { -private: +protected: FastDigitalPin outputA; // CLK FastDigitalPin outputB; // DT @@ -74,7 +74,7 @@ class SHRotaryEncoder { public: - void begin(uint8_t outputAPin, uint8_t outputBPin, int buttonPin, bool reverse, bool enablePullup, byte encoderid, bool half, SHRotaryEncoderPositionChanged changedcallback) { +virtual void begin(uint8_t outputAPin, uint8_t outputBPin, int buttonPin, bool reverse, bool enablePullup, byte encoderid, bool half, SHRotaryEncoderPositionChanged changedcallback) { halfSteps = half; buttonDebouncer.begin(50); outputA.begin((!reverse) ? outputAPin : outputBPin); @@ -93,19 +93,19 @@ class SHRotaryEncoder { buttonLastState = button.digitalRead(); positionChangedCallback = changedcallback; } - - uint8_t getDirection(uint8_t delay, unsigned long referenceTime) { + +virtual uint8_t getDirection(uint8_t delay, unsigned long referenceTime) { if (directionLastChange != 255 && (referenceTime - positionLastChanged) < delay) { return directionLastChange; } return 255; } - uint8_t getPressed() { +virtual uint8_t getPressed() { return button.isValid() && !buttonLastState; } - void read() { +virtual void read() { if (!halfSteps) inputLastState = fullStepsTable[inputLastState & 0xf][(outputB.digitalRead() << 1) | outputA.digitalRead()]; else { @@ -119,12 +119,14 @@ class SHRotaryEncoder { positionChangedCallback(id, counter, 0); positionLastChanged = millis(); directionLastChange = 0; + //Serial.print("DIR_CCW"); } else if (direction == DIR_CW) { counter--; positionChangedCallback(id, counter, 1); positionLastChanged = millis(); directionLastChange = 1; + //Serial.print("DIR_CW"); } if (button.isValid()) { diff --git a/src/main.cpp b/src/main.cpp index 3a8d8bc..bca131d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,7 +81,7 @@ FullLoopbackStream incomingStream; //#define INCLUDE_SHAKEITMOTOMONSTER //{"Name":"INCLUDE_SHAKEITMOTOMONSTER","Type":"autodefine","Condition":"[MOTOMONSTER_ENABLED]>0"} //#define INCLUDE_SHAKEIDUALVNH5019 //{"Name":"INCLUDE_SHAKEIDUALVNH5019","Type":"autodefine","Condition":"[DUALVNH5019_ENABLED]>0"} //#define INCLUDE_SHAKEITPWM //{"Name":"INCLUDE_SHAKEITPWM","Type":"autodefine","Condition":"[SHAKEITPWM_ENABLED_MOTORS]>0"} -//#define INCLUDE_ENCODERS //{"Name":"INCLUDE_ENCODERS","Type":"autodefine","Condition":"[ENABLED_ENCODERS_COUNT]>0","IsInput":true} +#define INCLUDE_ENCODERS //{"Name":"INCLUDE_ENCODERS","Type":"autodefine","Condition":"[ENABLED_ENCODERS_COUNT]>0","IsInput":true} //#define INCLUDE_DM163_MATRIX //{"Name":"INCLUDE_DM163_MATRIX","Type":"autodefine","Condition":"[DM163_MATRIX_ENABLED]>0"} //#define INCLUDE_SUNFOUNDERSH104P_MATRIX //{"Name":"INCLUDE_SUNFOUNDERSH104P_MATRIX","Type":"autodefine","Condition":"[SUNFOUNDERSH104P_MATRIX_ENABLED]>0"} @@ -645,64 +645,88 @@ SHDebouncer ButtonsDebouncer(10); #ifdef INCLUDE_ENCODERS #include "SHRotaryEncoder.h" -#define ENCODER1_CLK_PIN 7 //{"Name":"ENCODER1_CLK_PIN","Title":"Encoder 1 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 1 CLK","Condition":"ENABLED_ENCODERS_COUNT>0"} -#define ENCODER1_DT_PIN 8 //{"Name":"ENCODER1_DT_PIN","Title":"Encoder 1 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 1 DT","Condition":"ENABLED_ENCODERS_COUNT>0"} -#define ENCODER1_BUTTON_PIN 9 //{"Name":"ENCODER1_BUTTON_PIN","Title":"Encoder 1 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 1 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>0","Min":-1} -#define ENCODER1_ENABLE_PULLUP 0 //{"Name":"ENCODER1_ENABLE_PULLUP","Title":"Encoder 1 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>0"} +#if I2C_SERIAL_BYPASS && I2C_BYPASS_SLAVE + #include + SHRotaryEncoderContext virtualEncoderContext; + void VirtualEncoderPositionChanged(int encoderId,int position, byte direction); +#endif + +#define ENCODER1_CLK_PIN 8 //{"Name":"ENCODER1_CLK_PIN","Title":"Encoder 1 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 1 CLK","Condition":"ENABLED_ENCODERS_COUNT>0"} +#define ENCODER1_DT_PIN 9 //{"Name":"ENCODER1_DT_PIN","Title":"Encoder 1 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 1 DT","Condition":"ENABLED_ENCODERS_COUNT>0"} +#define ENCODER1_BUTTON_PIN 5 //{"Name":"ENCODER1_BUTTON_PIN","Title":"Encoder 1 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 1 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>0","Min":-1} +#define ENCODER1_BUTTON_TYPE 0 //{"Name":"ENCODER1_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>0","ListValues":"0,Physically Connected;1,Serialized"} +#define ENCODER1_ENABLE_PULLUP 1 //{"Name":"ENCODER1_ENABLE_PULLUP","Title":"Encoder 1 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>0"} #define ENCODER1_REVERSE_DIRECTION 0 //{"Name":"ENCODER1_REVERSE_DIRECTION","Title":"Encoder 1 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>0"} #define ENCODER1_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER1_ENABLE_HALFSTEPS","Title":"Encoder 1 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=1","ListValues":"0,Full steps;1,Half steps"} +#define ENCODER1_TYPE 1 //{"Name":"ENCODER1_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>0","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER2_CLK_PIN 11 //{"Name":"ENCODER2_CLK_PIN","Title":"Encoder 2 output A (CLK) pin","DefaultValue":"11","Type":"pin;Encoder 2 CLK","Condition":"ENABLED_ENCODERS_COUNT>1"} #define ENCODER2_DT_PIN 12 //{"Name":"ENCODER2_DT_PIN","Title":"Encoder 2 output B (DT) pin","DefaultValue":"12","Type":"pin;Encoder 2 DT","Condition":"ENABLED_ENCODERS_COUNT>1"} #define ENCODER2_BUTTON_PIN 13 //{"Name":"ENCODER2_BUTTON_PIN","Title":"Encoder 2 button (SW) pin","DefaultValue":"13","Type":"pin;Encoder 2 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>1","Min":-1} +#define ENCODER2_BUTTON_TYPE 0 //{"Name":"ENCODER2_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>1","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER2_ENABLE_PULLUP 0 //{"Name":"ENCODER2_ENABLE_PULLUP","Title":"Encoder 2 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>1"} #define ENCODER2_REVERSE_DIRECTION 0 //{"Name":"ENCODER2_REVERSE_DIRECTION","Title":"Encoder 2 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>1"} #define ENCODER2_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER2_ENABLE_HALFSTEPS","Title":"Encoder 2 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=2","ListValues":"0,Full steps;1,Half steps"} +#define ENCODER2_TYPE 0 //{"Name":"ENCODER1_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>1","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER3_CLK_PIN 7 //{"Name":"ENCODER3_CLK_PIN","Title":"Encoder 3 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 3 CLK","Condition":"ENABLED_ENCODERS_COUNT>2"} #define ENCODER3_DT_PIN 8 //{"Name":"ENCODER3_DT_PIN","Title":"Encoder 3 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 3 DT","Condition":"ENABLED_ENCODERS_COUNT>2"} #define ENCODER3_BUTTON_PIN 9 //{"Name":"ENCODER3_BUTTON_PIN","Title":"Encoder 3 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 3 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>2","Min":-1} +#define ENCODER3_BUTTON_TYPE 0 //{"Name":"ENCODER3_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=3","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER3_ENABLE_PULLUP 0 //{"Name":"ENCODER3_ENABLE_PULLUP","Title":"Encoder 3 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>2"} #define ENCODER3_REVERSE_DIRECTION 0 //{"Name":"ENCODER3_REVERSE_DIRECTION","Title":"Encoder 3 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>2"} #define ENCODER3_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER3_ENABLE_HALFSTEPS","Title":"Encoder 3 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=3","ListValues":"0,Full steps;1,Half steps"} +#define ENCODER3_TYPE 0 //{"Name":"ENCODER3_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=3","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER4_CLK_PIN 7 //{"Name":"ENCODER4_CLK_PIN","Title":"Encoder 4 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 4 CLK","Condition":"ENABLED_ENCODERS_COUNT>3"} #define ENCODER4_DT_PIN 8 //{"Name":"ENCODER4_DT_PIN","Title":"Encoder 4 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 4 DT","Condition":"ENABLED_ENCODERS_COUNT>3"} #define ENCODER4_BUTTON_PIN 9 //{"Name":"ENCODER4_BUTTON_PIN","Title":"Encoder 4 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 4 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>3","Min":-1} +#define ENCODER4_BUTTON_TYPE 0 //{"Name":"ENCODER4_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>3","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER4_ENABLE_PULLUP 0 //{"Name":"ENCODER4_ENABLE_PULLUP","Title":"Encoder 4 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>3"} #define ENCODER4_REVERSE_DIRECTION 0 //{"Name":"ENCODER4_REVERSE_DIRECTION","Title":"Encoder 4 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>3"} #define ENCODER4_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER4_ENABLE_HALFSTEPS","Title":"Encoder 4 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=4","ListValues":"0,Full steps;1,Half steps"} +#define ENCODER4_TYPE 0 //{"Name":"ENCODER4_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=4","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER5_CLK_PIN 7 //{"Name":"ENCODER5_CLK_PIN","Title":"Encoder 5 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 5 CLK","Condition":"ENABLED_ENCODERS_COUNT>4"} #define ENCODER5_DT_PIN 8 //{"Name":"ENCODER5_DT_PIN","Title":"Encoder 5 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 5 DT","Condition":"ENABLED_ENCODERS_COUNT>4"} #define ENCODER5_BUTTON_PIN 9 //{"Name":"ENCODER5_BUTTON_PIN","Title":"Encoder 5 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 5 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>4","Min":-1} +#define ENCODER5_BUTTON_TYPE 0 //{"Name":"ENCODER5_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=4","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER5_ENABLE_PULLUP 0 //{"Name":"ENCODER5_ENABLE_PULLUP","Title":"Encoder 5 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>4"} #define ENCODER5_REVERSE_DIRECTION 0 //{"Name":"ENCODER5_REVERSE_DIRECTION","Title":"Encoder 5 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>4"} #define ENCODER5_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER5_ENABLE_HALFSTEPS","Title":"Encoder 5 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=5","ListValues":"0,Full steps;1,Half steps"} +#define ENCODER5_TYPE 0 //{"Name":"ENCODER5_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=5","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER6_CLK_PIN 7 //{"Name":"ENCODER6_CLK_PIN","Title":"Encoder 6 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 6 CLK","Condition":"ENABLED_ENCODERS_COUNT>5"} #define ENCODER6_DT_PIN 8 //{"Name":"ENCODER6_DT_PIN","Title":"Encoder 6 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 6 DT","Condition":"ENABLED_ENCODERS_COUNT>5"} #define ENCODER6_BUTTON_PIN 9 //{"Name":"ENCODER6_BUTTON_PIN","Title":"Encoder 6 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 6 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>5","Min":-1} +#define ENCODER6_BUTTON_TYPE 0 //{"Name":"ENCODER6_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=5","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER6_ENABLE_PULLUP 0 //{"Name":"ENCODER6_ENABLE_PULLUP","Title":"Encoder 6 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>5"} #define ENCODER6_REVERSE_DIRECTION 0 //{"Name":"ENCODER6_REVERSE_DIRECTION","Title":"Encoder 6 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>5"} #define ENCODER6_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER6_ENABLE_HALFSTEPS","Title":"Encoder 6 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=6","ListValues":"0,Full steps;1,Half steps"} +#define ENCODER6_TYPE 0 //{"Name":"ENCODER6_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=6","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER7_CLK_PIN 7 //{"Name":"ENCODER7_CLK_PIN","Title":"Encoder 7 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 7 CLK","Condition":"ENABLED_ENCODERS_COUNT>6"} #define ENCODER7_DT_PIN 8 //{"Name":"ENCODER7_DT_PIN","Title":"Encoder 7 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 7 DT","Condition":"ENABLED_ENCODERS_COUNT>6"} #define ENCODER7_BUTTON_PIN 9 //{"Name":"ENCODER7_BUTTON_PIN","Title":"Encoder 7 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 7 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>6","Min":-1} +#define ENCODER7_BUTTON_TYPE 0 //{"Name":"ENCODER7_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=7","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER7_ENABLE_PULLUP 0 //{"Name":"ENCODER7_ENABLE_PULLUP","Title":"Encoder 7 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>6"} #define ENCODER7_REVERSE_DIRECTION 0 //{"Name":"ENCODER7_REVERSE_DIRECTION","Title":"Encoder 7 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>6"} #define ENCODER7_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER7_ENABLE_HALFSTEPS","Title":"Encoder 7 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=7","ListValues":"0,Full steps;1,Half steps"} +#define ENCODER7_TYPE 0 //{"Name":"ENCODER7_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=7","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER8_CLK_PIN 7 //{"Name":"ENCODER8_CLK_PIN","Title":"Encoder 8 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 8 CLK","Condition":"ENABLED_ENCODERS_COUNT>7"} #define ENCODER8_DT_PIN 8 //{"Name":"ENCODER8_DT_PIN","Title":"Encoder 8 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 8 DT","Condition":"ENABLED_ENCODERS_COUNT>7"} #define ENCODER8_BUTTON_PIN 9 //{"Name":"ENCODER8_BUTTON_PIN","Title":"Encoder 8 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 8 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>7","Min":-1} +#define ENCODER8_BUTTON_TYPE 0 //{"Name":"ENCODER8_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>7","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER8_ENABLE_PULLUP 0 //{"Name":"ENCODER8_ENABLE_PULLUP","Title":"Encoder 8 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>7"} #define ENCODER8_REVERSE_DIRECTION 0 //{"Name":"ENCODER8_REVERSE_DIRECTION","Title":"Encoder 8 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>7"} #define ENCODER8_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER8_ENABLE_HALFSTEPS","Title":"Encoder 8 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=8","ListValues":"0,Full steps;1,Half steps"} +#define ENCODER8_TYPE 0 //{"Name":"ENCODER8_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>7","ListValues":"0,Physically Connected;1,Serialized"} -SHRotaryEncoder encoder1, encoder2, encoder3, encoder4, encoder5, encoder6, encoder7, encoder8; -SHRotaryEncoder* SHRotaryEncoders[] = { &encoder1, &encoder2, &encoder3, &encoder4, &encoder5, &encoder6, &encoder7, &encoder8 }; +int ENCODER_TYPE[]={ENCODER1_TYPE,ENCODER2_TYPE,ENCODER3_TYPE,ENCODER4_TYPE,ENCODER5_TYPE,ENCODER6_TYPE,ENCODER7_TYPE,ENCODER8_TYPE}; +//SHRotaryEncoder encoder1, encoder2, encoder3, encoder4, encoder5, encoder6, encoder7, encoder8; +//SHRotaryEncoder* SHRotaryEncoders[] = { &encoder1, &encoder2, &encoder3, &encoder4, &encoder5, &encoder6, &encoder7, &encoder8 }; +SHRotaryEncoder* SHRotaryEncoders[8]; #endif // ----------------------- ROTARY ENCODERS ------------------------------------------------------------------ @@ -1107,8 +1131,19 @@ void idle(bool critical) { } #ifdef INCLUDE_ENCODERS +void UpdateGamepadEncodersState(bool sendState); + +#if I2C_SERIAL_BYPASS && I2C_BYPASS_SLAVE +void VirtualEncoderPositionChanged(int encoderId,int position, byte direction) { + virtualEncoderContext.updateContext(encoderId,position,direction); +} +#endif + + void EncoderPositionChanged(int encoderId, int position, byte direction) { -#ifdef INCLUDE_GAMEPAD + +#if INCLUDE_GAMEPAD || ( INCLUDE_GAMEPAD && !I2C_BYPASS_MASTER && I2C_BYPASS_SLAVE && I2C_SERIAL_BYPASS) + UpdateGamepadEncodersState(true); #else if (direction < 2) { @@ -1117,39 +1152,18 @@ void EncoderPositionChanged(int encoderId, int position, byte direction) { arqserial.CustomPacketSendByte(direction); arqserial.CustomPacketSendByte(position); arqserial.CustomPacketEnd(); + } - else { + else { // BUTTON CLICK positionChangedCallback(id, counter, buttonState == HIGH ? 2 : 3); arqserial.CustomPacketStart(0x02, 2); arqserial.CustomPacketSendByte(encoderId); arqserial.CustomPacketSendByte(direction - 2); arqserial.CustomPacketEnd(); } + #endif } #endif -/*uint8_t endWireTransmission(bool stop){ - uint8_t error=Wire.endTransmission(stop); - if(error=0){ - Serial.printf("\n Correct wire close \n"); - } - if(error==1){ - Serial.printf("\n Wire buffer exausted \n"); - } - if(error==2){ - Serial.printf("\n received NACK on transmit of address.\n"); - } - if(error==3){ - Serial.printf("received NACK on transmit of data."); - } - if(error==4){ - Serial.printf(" other error."); - } - if(error==5){ - Serial.printf("timeout"); - } - Serial.flush(); - return error; -*/ /** @@ -1203,7 +1217,28 @@ void buttonMatrixStatusChanged(int buttonId, byte Status) { #endif + #if I2C_BYPASS_SLAVE + #include "SimHubProtocolDecoder.h" + + /*** ATTACH BEHAVIOURS WHEN IS IN SLAVE MODE*/ + EventCallBackManager callbacker; + + + + void receiveSerialProtocolViaI2c(int howMany){ + #if I2C_SERIAL_BYPASS_DEBUG + Serial.print("Received data via I2C with"); + Serial.print(howMany); + Serial.print(" Bytes"); + Serial.flush(); + #endif + decodeBuffer(&callbacker,&Wire); + } + + #endif + +void InitEncoders() ; /***** * @@ -1237,6 +1272,43 @@ void setup() #endif FlowSerialBegin(19200); + while (!Serial1) ; // https://forum.arduino.cc/t/cant-view-serial-print-from-setup/167916 + + +#if I2C_BYPASS_SLAVE + Serial.println("MAIN - SETUP - I2C_SERIAL_BYPASS AS SLAVE"); + #ifdef INCLUDE_BUTTONS + callbacker.setButtonCallBack(buttonStatusChanged); + #endif + +// callbacker.setAnalogAxisChangedEventCallback(analogAxisChangedEventCallback); + Wire.begin(I2C_BYPASS_SLAVE_ADRESS); /* join i2c bus with address 8 */ + Wire.setWireTimeout(1000); + Wire.onReceive(receiveSerialProtocolViaI2c); + + // TODO: IN ENCODERS BRANCH + #ifdef INCLUDE_ENCODERS + callbacker.setEncoderPositionChangedCallback(VirtualEncoderPositionChanged); + #endif + + #endif + +#if I2C_BYPASS_MASTER + #if I2C_SERIAL_BYPASS_DEBUG + Serial.println("MAIN - SETUP - I2C_SERIAL_BYPASS"); + #endif + I2CTransportManager::setup(&outgoingStream); + + /// TEST TRANSPORT + // Serial.println("Lanzando prueba del canal I2C"); + // StreamWrite("t"); + // StreamWrite("e"); + // StreamWrite("s"); + // StreamWrite("t"); + //// + //axis1.setCallBack(axisStatusChanged); +#endif + #ifdef INCLUDE_GAMEPAD Joystick.begin(false); @@ -1406,8 +1478,6 @@ void setup() shCustomProtocol.setup(); arqserial.setIdleFunction(idle); - // Serial.begin(115200); - // delay(200); #if(GAMEPAD_AXIS_01_ENABLED == 1) SHGAMEPADAXIS01.SetJoystick(&Joystick); @@ -1427,15 +1497,48 @@ void setup() } #ifdef INCLUDE_ENCODERS +#include void InitEncoders() { - if (ENABLED_ENCODERS_COUNT > 0) encoder1.begin(ENCODER1_CLK_PIN, ENCODER1_DT_PIN, ENCODER1_BUTTON_PIN, ENCODER1_REVERSE_DIRECTION, ENCODER1_ENABLE_PULLUP, 1, ENCODER1_ENABLE_HALFSTEPS, EncoderPositionChanged); - if (ENABLED_ENCODERS_COUNT > 1) encoder2.begin(ENCODER2_CLK_PIN, ENCODER2_DT_PIN, ENCODER2_BUTTON_PIN, ENCODER2_REVERSE_DIRECTION, ENCODER2_ENABLE_PULLUP, 2, ENCODER2_ENABLE_HALFSTEPS, EncoderPositionChanged); - if (ENABLED_ENCODERS_COUNT > 2) encoder3.begin(ENCODER3_CLK_PIN, ENCODER3_DT_PIN, ENCODER3_BUTTON_PIN, ENCODER3_REVERSE_DIRECTION, ENCODER3_ENABLE_PULLUP, 3, ENCODER3_ENABLE_HALFSTEPS, EncoderPositionChanged); - if (ENABLED_ENCODERS_COUNT > 3) encoder4.begin(ENCODER4_CLK_PIN, ENCODER4_DT_PIN, ENCODER4_BUTTON_PIN, ENCODER4_REVERSE_DIRECTION, ENCODER4_ENABLE_PULLUP, 4, ENCODER4_ENABLE_HALFSTEPS, EncoderPositionChanged); - if (ENABLED_ENCODERS_COUNT > 4) encoder5.begin(ENCODER5_CLK_PIN, ENCODER5_DT_PIN, ENCODER5_BUTTON_PIN, ENCODER5_REVERSE_DIRECTION, ENCODER5_ENABLE_PULLUP, 5, ENCODER5_ENABLE_HALFSTEPS, EncoderPositionChanged); - if (ENABLED_ENCODERS_COUNT > 5) encoder6.begin(ENCODER6_CLK_PIN, ENCODER6_DT_PIN, ENCODER6_BUTTON_PIN, ENCODER6_REVERSE_DIRECTION, ENCODER6_ENABLE_PULLUP, 6, ENCODER6_ENABLE_HALFSTEPS, EncoderPositionChanged); - if (ENABLED_ENCODERS_COUNT > 6) encoder7.begin(ENCODER7_CLK_PIN, ENCODER7_DT_PIN, ENCODER7_BUTTON_PIN, ENCODER7_REVERSE_DIRECTION, ENCODER7_ENABLE_PULLUP, 7, ENCODER7_ENABLE_HALFSTEPS, EncoderPositionChanged); - if (ENABLED_ENCODERS_COUNT > 7) encoder8.begin(ENCODER8_CLK_PIN, ENCODER8_DT_PIN, ENCODER8_BUTTON_PIN, ENCODER8_REVERSE_DIRECTION, ENCODER8_ENABLE_PULLUP, 8, ENCODER8_ENABLE_HALFSTEPS, EncoderPositionChanged); + for(int i=0;ibegin(ENCODER1_CLK_PIN, ENCODER1_DT_PIN, ENCODER1_BUTTON_PIN, ENCODER1_REVERSE_DIRECTION, ENCODER1_ENABLE_PULLUP, 1, ENCODER1_ENABLE_HALFSTEPS, EncoderPositionChanged); + break; + case 1: + SHRotaryEncoders[1]->begin(ENCODER2_CLK_PIN, ENCODER2_DT_PIN, ENCODER2_BUTTON_PIN, ENCODER2_REVERSE_DIRECTION, ENCODER2_ENABLE_PULLUP, 2, ENCODER2_ENABLE_HALFSTEPS, EncoderPositionChanged); + break; + case 2: + SHRotaryEncoders[2]->begin(ENCODER3_CLK_PIN, ENCODER3_DT_PIN, ENCODER3_BUTTON_PIN, ENCODER3_REVERSE_DIRECTION, ENCODER3_ENABLE_PULLUP, 3, ENCODER3_ENABLE_HALFSTEPS, EncoderPositionChanged); + break; + case 3: + SHRotaryEncoders[3]->begin(ENCODER4_CLK_PIN, ENCODER4_DT_PIN, ENCODER4_BUTTON_PIN, ENCODER4_REVERSE_DIRECTION, ENCODER4_ENABLE_PULLUP, 4, ENCODER4_ENABLE_HALFSTEPS, EncoderPositionChanged); + break; + case 4: + SHRotaryEncoders[4]->begin(ENCODER5_CLK_PIN, ENCODER5_DT_PIN, ENCODER5_BUTTON_PIN, ENCODER5_REVERSE_DIRECTION, ENCODER5_ENABLE_PULLUP, 5, ENCODER5_ENABLE_HALFSTEPS, EncoderPositionChanged); + break; + case 5: + SHRotaryEncoders[5]->begin(ENCODER6_CLK_PIN, ENCODER6_DT_PIN, ENCODER6_BUTTON_PIN, ENCODER6_REVERSE_DIRECTION, ENCODER6_ENABLE_PULLUP, 6, ENCODER6_ENABLE_HALFSTEPS, EncoderPositionChanged); + break; + case 6: + SHRotaryEncoders[6]->begin(ENCODER7_CLK_PIN, ENCODER7_DT_PIN, ENCODER7_BUTTON_PIN, ENCODER7_REVERSE_DIRECTION, ENCODER7_ENABLE_PULLUP, 7, ENCODER7_ENABLE_HALFSTEPS, EncoderPositionChanged); + break; + case 7: + SHRotaryEncoders[7]->begin(ENCODER8_CLK_PIN, ENCODER8_DT_PIN, ENCODER8_BUTTON_PIN, ENCODER8_REVERSE_DIRECTION, ENCODER8_ENABLE_PULLUP, 8, ENCODER8_ENABLE_HALFSTEPS, EncoderPositionChanged); + break; + + default: + break; + } + + } + } #endif @@ -1455,29 +1558,48 @@ void UpdateGamepadState() { #endif #ifdef INCLUDE_ENCODERS +// Serial.print("DisplayClientV2:UpdateGamepadState ->"); UpdateGamepadEncodersState(false); #endif - Joystick.sendState(); } + + + #ifdef INCLUDE_ENCODERS + void UpdateGamepadEncodersState(bool sendState) { int btnidx = TM1638_ENABLEDMODULES * 8 + ENABLED_BUTTONS_COUNT + ENABLED_BUTTONMATRIX * (BMATRIX_COLS * BMATRIX_ROWS); unsigned long refTime = millis(); - for (int i = 0; i < ENABLED_ENCODERS_COUNT; i++) { - uint8_t dir = SHRotaryEncoders[i]->getDirection(MICRO_GAMEPAD_ENCODERPRESSTIME, refTime); - Joystick.setButton(btnidx, dir == 0); - Joystick.setButton(btnidx + 1, dir == 1); - Joystick.setButton(btnidx + 2, SHRotaryEncoders[i]->getPressed()); - btnidx += 3; + + for (int i = 0; i < ENABLED_ENCODERS_COUNT; i++) { + //delay(300); + // Serial.print(" UpdateGamepadEncodersState :: "); + // Serial.print(sendState); + // Serial.print("::ENCODER - "); + // Serial.print(i); + // Serial.print(" -> "); + //Serial.print(SHRotaryEncoders[i]->getPressed()); + //if(ENCODER_TYPE[i]==0){ + uint8_t dir = SHRotaryEncoders[i]->getDirection(MICRO_GAMEPAD_ENCODERPRESSTIME, refTime); + // Serial.print(dir); + // Serial.print(","); + Joystick.setButton(btnidx, dir == 0); + Joystick.setButton(btnidx + 1, dir == 1); + Joystick.setButton(btnidx + 2, SHRotaryEncoders[i]->getPressed()); + btnidx += 3; + + //} + } if (sendState) Joystick.sendState(); } #endif + #endif From 468d57acebeb01cbedc58923fb4f8e62f1a321e1 Mon Sep 17 00:00:00 2001 From: wapophis Date: Sat, 20 Apr 2024 16:21:56 +0200 Subject: [PATCH 25/25] working in master mode encoders via i2c --- lib/I2CSerialBridge/I2CTransportMaster.h | 1 + platformio.ini | 4 +- src/main.cpp | 49 +++++++++++++----------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/lib/I2CSerialBridge/I2CTransportMaster.h b/lib/I2CSerialBridge/I2CTransportMaster.h index ef727d8..776b10a 100644 --- a/lib/I2CSerialBridge/I2CTransportMaster.h +++ b/lib/I2CSerialBridge/I2CTransportMaster.h @@ -37,4 +37,5 @@ class I2CTransportMaster : public I2CTransport { #endif } +}; #endif \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 0429cd8..e08f3ea 100644 --- a/platformio.ini +++ b/platformio.ini @@ -77,9 +77,9 @@ lib_deps = build_flags = -w -DESP8266=true monitor_speed = 115200 -;upload_port = COM4 +upload_port = COM3 build_type = debug # set this to debug only for debugging, as it's slower. -monitor_filters = esp8266_exception_decoder, default, debug +monitor_filters = esp8266_exception_decoder, default build_unflags = -fno-rtti # comment this out if you need more ram, but you'll need to make some type assumptions monitor_rts = 0 monitor_dtr = 0 diff --git a/src/main.cpp b/src/main.cpp index bca131d..4a3da48 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,7 +13,7 @@ #if I2C_SERIAL_BYPASS #define WIRE Wire - #define I2C_MASTER true + #define I2C_BYPASS_MASTER true #define I2C_BYPASS_SLAVE false #define I2C_ADDRESS 0x08 #define I2C_SERIAL_BYPASS_DEBUG true @@ -561,7 +561,7 @@ SHGamepadAxis SHGAMEPADAXIS03(GAMEPAD_AXIS_03_PIN, 2, GAMEPAD_AXIS_03_MINVALUE, // ----------------------- ADDITIONAL BUTTONS --------------------------------------------------------------- // https://github.com/zegreatclan/SimHub/wiki/Arduino-Press-Buttons // ---------------------------------------------------------------------------------------------------------- -#define ENABLED_BUTTONS_COUNT 6 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} +#define ENABLED_BUTTONS_COUNT 2 //{"Group":"Additional Buttons","Name":"ENABLED_BUTTONS_COUNT","Title":"Additional buttons (directly connected to the arduino, 12 max) buttons count","DefaultValue":"0","Type":"int","Max":12} #ifdef INCLUDE_BUTTONS #define BUTTON_PIN_1 D3 //{"Name":"BUTTON_PIN_1","Title":"1'st Additional button digital pin","DefaultValue":"3","Type":"pin;Button 1","Condition":"ENABLED_BUTTONS_COUNT>=1"} @@ -641,7 +641,7 @@ SHDebouncer ButtonsDebouncer(10); // https://www.dx.com/p/ky-040-rotary-encoder-module-brick-sensor-development-for-arduino-avr-pic-420429#.W9BCM0sza0Q // Rotary encoders with pull-up resistors on the 3 outputs // ---------------------------------------------------------------------------------------------------------- -#define ENABLED_ENCODERS_COUNT 0 //{"Group":"Rotary Encoders","Name":"ENABLED_ENCODERS_COUNT","Title":"Rotary encoders enabled","DefaultValue":"0","Type":"int","Max":8} +#define ENABLED_ENCODERS_COUNT 1 //{"Group":"Rotary Encoders","Name":"ENABLED_ENCODERS_COUNT","Title":"Rotary encoders enabled","DefaultValue":"0","Type":"int","Max":8} #ifdef INCLUDE_ENCODERS #include "SHRotaryEncoder.h" @@ -651,36 +651,36 @@ SHDebouncer ButtonsDebouncer(10); void VirtualEncoderPositionChanged(int encoderId,int position, byte direction); #endif -#define ENCODER1_CLK_PIN 8 //{"Name":"ENCODER1_CLK_PIN","Title":"Encoder 1 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 1 CLK","Condition":"ENABLED_ENCODERS_COUNT>0"} -#define ENCODER1_DT_PIN 9 //{"Name":"ENCODER1_DT_PIN","Title":"Encoder 1 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 1 DT","Condition":"ENABLED_ENCODERS_COUNT>0"} -#define ENCODER1_BUTTON_PIN 5 //{"Name":"ENCODER1_BUTTON_PIN","Title":"Encoder 1 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 1 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>0","Min":-1} +#define ENCODER1_CLK_PIN D5 //{"Name":"ENCODER1_CLK_PIN","Title":"Encoder 1 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 1 CLK","Condition":"ENABLED_ENCODERS_COUNT>0"} +#define ENCODER1_DT_PIN D6 //{"Name":"ENCODER1_DT_PIN","Title":"Encoder 1 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 1 DT","Condition":"ENABLED_ENCODERS_COUNT>0"} +#define ENCODER1_BUTTON_PIN D7 //{"Name":"ENCODER1_BUTTON_PIN","Title":"Encoder 1 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 1 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>0","Min":-1} #define ENCODER1_BUTTON_TYPE 0 //{"Name":"ENCODER1_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>0","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER1_ENABLE_PULLUP 1 //{"Name":"ENCODER1_ENABLE_PULLUP","Title":"Encoder 1 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>0"} #define ENCODER1_REVERSE_DIRECTION 0 //{"Name":"ENCODER1_REVERSE_DIRECTION","Title":"Encoder 1 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>0"} #define ENCODER1_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER1_ENABLE_HALFSTEPS","Title":"Encoder 1 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=1","ListValues":"0,Full steps;1,Half steps"} -#define ENCODER1_TYPE 1 //{"Name":"ENCODER1_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>0","ListValues":"0,Physically Connected;1,Serialized"} +#define ENCODER1_TYPE 0 //{"Name":"ENCODER1_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>0","ListValues":"0,Physically Connected;1,Serialized"} -#define ENCODER2_CLK_PIN 11 //{"Name":"ENCODER2_CLK_PIN","Title":"Encoder 2 output A (CLK) pin","DefaultValue":"11","Type":"pin;Encoder 2 CLK","Condition":"ENABLED_ENCODERS_COUNT>1"} -#define ENCODER2_DT_PIN 12 //{"Name":"ENCODER2_DT_PIN","Title":"Encoder 2 output B (DT) pin","DefaultValue":"12","Type":"pin;Encoder 2 DT","Condition":"ENABLED_ENCODERS_COUNT>1"} -#define ENCODER2_BUTTON_PIN 13 //{"Name":"ENCODER2_BUTTON_PIN","Title":"Encoder 2 button (SW) pin","DefaultValue":"13","Type":"pin;Encoder 2 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>1","Min":-1} +#define ENCODER2_CLK_PIN 33 //{"Name":"ENCODER2_CLK_PIN","Title":"Encoder 2 output A (CLK) pin","DefaultValue":"11","Type":"pin;Encoder 2 CLK","Condition":"ENABLED_ENCODERS_COUNT>1"} +#define ENCODER2_DT_PIN 34 //{"Name":"ENCODER2_DT_PIN","Title":"Encoder 2 output B (DT) pin","DefaultValue":"12","Type":"pin;Encoder 2 DT","Condition":"ENABLED_ENCODERS_COUNT>1"} +#define ENCODER2_BUTTON_PIN 35 //{"Name":"ENCODER2_BUTTON_PIN","Title":"Encoder 2 button (SW) pin","DefaultValue":"13","Type":"pin;Encoder 2 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>1","Min":-1} #define ENCODER2_BUTTON_TYPE 0 //{"Name":"ENCODER2_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>1","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER2_ENABLE_PULLUP 0 //{"Name":"ENCODER2_ENABLE_PULLUP","Title":"Encoder 2 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>1"} #define ENCODER2_REVERSE_DIRECTION 0 //{"Name":"ENCODER2_REVERSE_DIRECTION","Title":"Encoder 2 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>1"} #define ENCODER2_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER2_ENABLE_HALFSTEPS","Title":"Encoder 2 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=2","ListValues":"0,Full steps;1,Half steps"} #define ENCODER2_TYPE 0 //{"Name":"ENCODER1_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>1","ListValues":"0,Physically Connected;1,Serialized"} -#define ENCODER3_CLK_PIN 7 //{"Name":"ENCODER3_CLK_PIN","Title":"Encoder 3 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 3 CLK","Condition":"ENABLED_ENCODERS_COUNT>2"} -#define ENCODER3_DT_PIN 8 //{"Name":"ENCODER3_DT_PIN","Title":"Encoder 3 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 3 DT","Condition":"ENABLED_ENCODERS_COUNT>2"} -#define ENCODER3_BUTTON_PIN 9 //{"Name":"ENCODER3_BUTTON_PIN","Title":"Encoder 3 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 3 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>2","Min":-1} +#define ENCODER3_CLK_PIN D5 //{"Name":"ENCODER3_CLK_PIN","Title":"Encoder 3 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 3 CLK","Condition":"ENABLED_ENCODERS_COUNT>2"} +#define ENCODER3_DT_PIN D6 //{"Name":"ENCODER3_DT_PIN","Title":"Encoder 3 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 3 DT","Condition":"ENABLED_ENCODERS_COUNT>2"} +#define ENCODER3_BUTTON_PIN D7 //{"Name":"ENCODER3_BUTTON_PIN","Title":"Encoder 3 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 3 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>2","Min":-1} #define ENCODER3_BUTTON_TYPE 0 //{"Name":"ENCODER3_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=3","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER3_ENABLE_PULLUP 0 //{"Name":"ENCODER3_ENABLE_PULLUP","Title":"Encoder 3 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>2"} #define ENCODER3_REVERSE_DIRECTION 0 //{"Name":"ENCODER3_REVERSE_DIRECTION","Title":"Encoder 3 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>2"} #define ENCODER3_ENABLE_HALFSTEPS 0 //{"Name":"ENCODER3_ENABLE_HALFSTEPS","Title":"Encoder 3 steps mode","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=3","ListValues":"0,Full steps;1,Half steps"} #define ENCODER3_TYPE 0 //{"Name":"ENCODER3_TYPE","Title":"Is virtual encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>=3","ListValues":"0,Physically Connected;1,Serialized"} -#define ENCODER4_CLK_PIN 7 //{"Name":"ENCODER4_CLK_PIN","Title":"Encoder 4 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 4 CLK","Condition":"ENABLED_ENCODERS_COUNT>3"} -#define ENCODER4_DT_PIN 8 //{"Name":"ENCODER4_DT_PIN","Title":"Encoder 4 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 4 DT","Condition":"ENABLED_ENCODERS_COUNT>3"} -#define ENCODER4_BUTTON_PIN 9 //{"Name":"ENCODER4_BUTTON_PIN","Title":"Encoder 4 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 4 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>3","Min":-1} +#define ENCODER4_CLK_PIN D5 //{"Name":"ENCODER4_CLK_PIN","Title":"Encoder 4 output A (CLK) pin","DefaultValue":"7","Type":"pin;Encoder 4 CLK","Condition":"ENABLED_ENCODERS_COUNT>3"} +#define ENCODER4_DT_PIN D6 //{"Name":"ENCODER4_DT_PIN","Title":"Encoder 4 output B (DT) pin","DefaultValue":"8","Type":"pin;Encoder 4 DT","Condition":"ENABLED_ENCODERS_COUNT>3"} +#define ENCODER4_BUTTON_PIN D7 //{"Name":"ENCODER4_BUTTON_PIN","Title":"Encoder 4 button (SW) pin","DefaultValue":"9","Type":"pin;Encoder 4 SWITCH","Condition":"ENABLED_ENCODERS_COUNT>3","Min":-1} #define ENCODER4_BUTTON_TYPE 0 //{"Name":"ENCODER4_BUTTON_TYPE","Title":"Is virtual button encoder","DefaultValue":"0","Type":"list","Condition":"ENABLED_ENCODERS_COUNT>3","ListValues":"0,Physically Connected;1,Serialized"} #define ENCODER4_ENABLE_PULLUP 0 //{"Name":"ENCODER4_ENABLE_PULLUP","Title":"Encoder 4 enable pullup resistor","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>3"} #define ENCODER4_REVERSE_DIRECTION 0 //{"Name":"ENCODER4_REVERSE_DIRECTION","Title":"Encoder 4 reverse direction","DefaultValue":"0","Type":"bool","Condition":"ENABLED_ENCODERS_COUNT>3"} @@ -726,7 +726,7 @@ SHDebouncer ButtonsDebouncer(10); int ENCODER_TYPE[]={ENCODER1_TYPE,ENCODER2_TYPE,ENCODER3_TYPE,ENCODER4_TYPE,ENCODER5_TYPE,ENCODER6_TYPE,ENCODER7_TYPE,ENCODER8_TYPE}; //SHRotaryEncoder encoder1, encoder2, encoder3, encoder4, encoder5, encoder6, encoder7, encoder8; //SHRotaryEncoder* SHRotaryEncoders[] = { &encoder1, &encoder2, &encoder3, &encoder4, &encoder5, &encoder6, &encoder7, &encoder8 }; -SHRotaryEncoder* SHRotaryEncoders[8]; +SHRotaryEncoder* SHRotaryEncoders[ENABLED_ENCODERS_COUNT]; #endif // ----------------------- ROTARY ENCODERS ------------------------------------------------------------------ @@ -1172,9 +1172,9 @@ void EncoderPositionChanged(int encoderId, int position, byte direction) { */ void axisStatusChanged(int axisId,int mappedValue){ #if I2C_SERIAL_BYPASS - if(I2C_SERIAL_BYPASS_DEBUG){ + #if I2C_SERIAL_BYPASS_DEBUG Serial.print("AxisStatusChanged"); - } + #endif arqserial.CustomPacketStart(0x13,3); arqserial.CustomPacketSendByte(axisId); @@ -1246,6 +1246,10 @@ void InitEncoders() ; */ void setup() { + + FlowSerialBegin(19200); + //while (!Serial1) ; // https://forum.arduino.cc/t/cant-view-serial-print-from-setup/167916 + #if INCLUDE_WIFI #if DEBUG_TCP_BRIDGE Serial.begin(115200); @@ -1258,7 +1262,9 @@ void setup() #if I2C_SERIAL_BYPASS Serial.begin(115200); + while(!Serial); Serial.println("MAIN - SETUP - I2C_SERIAL_BYPASS"); + I2CTransportManager::setup(&outgoingStream); //axis1.setCallBack(axisStatusChanged); @@ -1270,9 +1276,6 @@ void setup() #ifdef INCLUDE_FUELGAUGE shFUELPIN.SetValue((int)80); #endif - - FlowSerialBegin(19200); - while (!Serial1) ; // https://forum.arduino.cc/t/cant-view-serial-print-from-setup/167916 #if I2C_BYPASS_SLAVE @@ -1297,7 +1300,7 @@ void setup() #if I2C_SERIAL_BYPASS_DEBUG Serial.println("MAIN - SETUP - I2C_SERIAL_BYPASS"); #endif - I2CTransportManager::setup(&outgoingStream); + //I2CTransportManager::setup(&outgoingStream); /// TEST TRANSPORT // Serial.println("Lanzando prueba del canal I2C");