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
This commit is contained in:
Alexander Richter 2023-03-28 18:50:22 +02:00
parent 474c57a8e1
commit 6415e4af50
2 changed files with 75 additions and 11 deletions

View File

@ -20,7 +20,7 @@
To begin Transmitting Ready is send out and expects to receive E: to establish connection. Afterwards Data is exchanged. 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. 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 Outputs = 'O' -read only -Pin State: 0,1
PWM Outputs = 'P' -read only -Pin State: 0-255 PWM Outputs = 'P' -read only -Pin State: 0-255
Digital LED Outputs = 'D' -read only -Pin State: 0,1 Digital LED Outputs = 'D' -read only -Pin State: 0,1
@ -51,12 +51,18 @@
//###################################################IO's################################################### //###################################################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 #ifdef INPUTS
const int Inputs = 16; //number of inputs using internal Pullup resistor. (short to ground to trigger) const int Inputs = 5; //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}; int InPinmap[] = {37,38,39,40,41};
#endif #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. //#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 #ifdef OUTPUTS
@ -180,17 +186,24 @@ Adafruit_NeoPixel strip(DLEDcount, DLEDPin, NEO_GRB + NEO_KHZ800);//Color sequen
#endif #endif
//#define DEBUG
//###Misc Settings### //###Misc Settings###
const int timeout = 10000; // timeout after 10 sec not receiving Stuff const int timeout = 10000; // timeout after 10 sec not receiving Stuff
const int debounceDelay = 50;
//#define DEBUG
//Variables for Saving States //Variables for Saving States
#ifdef INPUTS #ifdef INPUTS
int InState[Inputs]; int InState[Inputs];
int oldInState[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 #endif
#ifdef OUTPUTS #ifdef OUTPUTS
int OutState[Outputs]; int OutState[Outputs];
@ -242,6 +255,16 @@ void setup() {
oldInState[i] = -1; oldInState[i] = -1;
} }
#endif #endif
#ifdef SINPUTS
//setting Inputs with internal Pullup Resistors
for(int i= 0; i<sInputs;i++){
pinMode(sInPinmap[i], INPUT_PULLUP);
soldInState[i] = -1;
togglesinputs[i] = 0;
}
#endif
#ifdef AINPUTS #ifdef AINPUTS
for(int i= 0; i<AInputs;i++){ for(int i= 0; i<AInputs;i++){
@ -302,6 +325,9 @@ void loop() {
#ifdef INPUTS #ifdef INPUTS
readInputs(); //read Inputs & send data readInputs(); //read Inputs & send data
#endif #endif
#ifdef SINPUTS
readsInputs(); //read Inputs & send data
#endif
#ifdef AINPUTS #ifdef AINPUTS
readAInputs(); //read Analog Inputs & send data readAInputs(); //read Analog Inputs & send data
#endif #endif
@ -451,13 +477,38 @@ int readAInputs(){
void readInputs(){ void readInputs(){
for(int i= 0;i<Inputs; i++){ for(int i= 0;i<Inputs; i++){
int State = digitalRead(InPinmap[i]); int State = digitalRead(InPinmap[i]);
if(InState[i]!= State){ if(InState[i]!= State && millis()- lastInputDebounce[i] > debounceDelay){
InState[i] = State; InState[i] = State;
sendData('I',InPinmap[i],InState[i]); sendData('I',InPinmap[i],InState[i]);
lastInputDebounce[i] = millis();
} }
} }
} }
#endif #endif
#ifdef SINPUTS
void readsInputs(){
for(int i= 0;i<sInputs; i++){
sInState[i] = digitalRead(sInPinmap[i]);
if (sInState[i] != soldInState[i] && millis()- lastsInputDebounce[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 #ifdef ABSENCODER
int readAbsKnob(){ int readAbsKnob(){
@ -497,11 +548,11 @@ void commandReceived(char cmd, uint16_t io, uint16_t value){
writePwmOutputs(io,value); writePwmOutputs(io,value);
} }
#endif #endif
//#ifdef DLED #ifdef DLED
if(cmd == 'D'){ if(cmd == 'D'){
controlDLED(io,value); controlDLED(io,value);
} }
//#endif #endif
if(cmd == 'E'){ if(cmd == 'E'){
lastcom=millis(); lastcom=millis();
} }

View File

@ -50,8 +50,13 @@ connection = '/dev/ttyACM0'
# Set how many Inputs you have programmed in Arduino and which pins are Inputs # Set how many Inputs you have programmed in Arduino and which pins are Inputs
Inputs = 16 Inputs = 5
InPinmap = [32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48] 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 # Set how many Outputs you have programmed in Arduino and which pins are Outputs
Outputs = 9 Outputs = 9
@ -84,6 +89,12 @@ Debug = 1
olddOutStates= [0]*Outputs olddOutStates= [0]*Outputs
oldPwmOutStates=[0]*PwmOutputs 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 of HalPins ########
# setup Input halpins # setup Input halpins
@ -127,6 +138,8 @@ arduino = serial.Serial(connection, 115200, timeout=1, xonxoff=False, rtscts=Fal
firstcom = 0 firstcom = 0
event = time.time() event = time.time()
timeout = 9 #send something after max 9 seconds timeout = 9 #send something after max 9 seconds
######## Functions ######## ######## Functions ########
def keepAlive(event): def keepAlive(event):