From 6415e4af50e7c85452ceb8b379e8f5747cc72b37 Mon Sep 17 00:00:00 2001 From: Alexander Richter Date: Tue, 28 Mar 2023 18:50:22 +0200 Subject: [PATCH] Toggle Mode Inputs This Update introduces Toggled Inputs. Pressing a Button once will Toggle the Input HIGH. Pressing it again will Toggle the Input LOW. Toggled Inputs are handled the same as Inputs in LinuxCNC --- LinuxCNC_ArduinoConnector.ino | 69 ++++++++++++++++++++++++++++++----- arduino.py | 17 ++++++++- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/LinuxCNC_ArduinoConnector.ino b/LinuxCNC_ArduinoConnector.ino index 3380a26..ae0ea54 100644 --- a/LinuxCNC_ArduinoConnector.ino +++ b/LinuxCNC_ArduinoConnector.ino @@ -20,7 +20,7 @@ To begin Transmitting Ready is send out and expects to receive E: to establish connection. Afterwards Data is exchanged. Data is only send everythime it changes once. - Inputs = 'I' -write only -Pin State: 0,1 + Inputs & Toggle Inputs = 'I' -write only -Pin State: 0,1 Outputs = 'O' -read only -Pin State: 0,1 PWM Outputs = 'P' -read only -Pin State: 0-255 Digital LED Outputs = 'D' -read only -Pin State: 0,1 @@ -51,12 +51,18 @@ //###################################################IO's################################################### -//#define INPUTS //Use Arduino IO's as Inputs. Define how many Inputs you want in total and then which Pins you want to be Inputs. +#define INPUTS //Use Arduino IO's as Inputs. Define how many Inputs you want in total and then which Pins you want to be Inputs. #ifdef INPUTS - const int Inputs = 16; //number of inputs using internal Pullup resistor. (short to ground to trigger) - int InPinmap[] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47}; + const int Inputs = 5; //number of inputs using internal Pullup resistor. (short to ground to trigger) + int InPinmap[] = {37,38,39,40,41}; #endif + //Use Arduino IO's as Toggle Inputs, which means Inputs (Buttons for example) keep HIGH State after Release and Send LOW only after beeing Pressed again. +#define SINPUTS //Define how many Sticky Inputs you want in total and then which Pins you want to be Sticky Inputs. +#ifdef SINPUTS + const int sInputs = 5; //number of inputs using internal Pullup resistor. (short to ground to trigger) + int sInPinmap[] = {32,33,34,35,36}; +#endif //#define OUTPUTS //Use Arduino IO's as Outputs. Define how many Outputs you want in total and then which Pins you want to be Outputs. #ifdef OUTPUTS @@ -180,17 +186,24 @@ Adafruit_NeoPixel strip(DLEDcount, DLEDPin, NEO_GRB + NEO_KHZ800);//Color sequen #endif - +//#define DEBUG //###Misc Settings### const int timeout = 10000; // timeout after 10 sec not receiving Stuff +const int debounceDelay = 50; -//#define DEBUG //Variables for Saving States #ifdef INPUTS int InState[Inputs]; int oldInState[Inputs]; + unsigned long lastInputDebounce[Inputs]; +#endif +#ifdef SINPUTS + int sInState[sInputs]; + int soldInState[sInputs]; + int togglesinputs[sInputs]; + unsigned long lastsInputDebounce[sInputs]; #endif #ifdef OUTPUTS int OutState[Outputs]; @@ -242,6 +255,16 @@ void setup() { oldInState[i] = -1; } #endif + +#ifdef SINPUTS +//setting Inputs with internal Pullup Resistors + for(int i= 0; i debounceDelay){ InState[i] = State; sendData('I',InPinmap[i],InState[i]); + + lastInputDebounce[i] = millis(); } } } #endif +#ifdef SINPUTS +void readsInputs(){ + for(int i= 0;i debounceDelay){ + // Button state has changed and debounce delay has passed + soldInState[i] = sInState[i]; + if (sInState[i] == LOW) { + // Button has been pressed + togglesinputs[i] = !togglesinputs[i]; // Toggle the LED state + + if (togglesinputs[i]) { + sendData('I',sInPinmap[i],togglesinputs[i]); // Turn the LED on + } + else { + sendData('I',sInPinmap[i],togglesinputs[i]); // Turn the LED off + } + } + lastsInputDebounce[i] = millis(); + } + } +} +#endif #ifdef ABSENCODER int readAbsKnob(){ @@ -497,11 +548,11 @@ void commandReceived(char cmd, uint16_t io, uint16_t value){ writePwmOutputs(io,value); } #endif - //#ifdef DLED + #ifdef DLED if(cmd == 'D'){ controlDLED(io,value); } - //#endif + #endif if(cmd == 'E'){ lastcom=millis(); } diff --git a/arduino.py b/arduino.py index 86b499d..243f776 100755 --- a/arduino.py +++ b/arduino.py @@ -50,8 +50,13 @@ connection = '/dev/ttyACM0' # Set how many Inputs you have programmed in Arduino and which pins are Inputs -Inputs = 16 -InPinmap = [32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48] +Inputs = 5 +InPinmap = [37,38,39,40,41] + +# Set how many Toggled Inputs you have programmed in Arduino and which pins are Toggled Inputs +SInputs = 5 +sInPinmap = [32,33,34,35,36] + # Set how many Outputs you have programmed in Arduino and which pins are Outputs Outputs = 9 @@ -84,6 +89,12 @@ Debug = 1 olddOutStates= [0]*Outputs oldPwmOutStates=[0]*PwmOutputs +# Inputs and Toggled Inputs are handled the same. +# For DAU compatiblity we set them up seperately. +# Here we merge the arrays. + +Inputs = Inputs+ SInputs +InPinmap += sInPinmap ######## SetUp of HalPins ######## # setup Input halpins @@ -127,6 +138,8 @@ arduino = serial.Serial(connection, 115200, timeout=1, xonxoff=False, rtscts=Fal firstcom = 0 event = time.time() timeout = 9 #send something after max 9 seconds + + ######## Functions ######## def keepAlive(event):