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.
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<sInputs;i++){
pinMode(sInPinmap[i], INPUT_PULLUP);
soldInState[i] = -1;
togglesinputs[i] = 0;
}
#endif
#ifdef AINPUTS
for(int i= 0; i<AInputs;i++){
@ -302,6 +325,9 @@ void loop() {
#ifdef INPUTS
readInputs(); //read Inputs & send data
#endif
#ifdef SINPUTS
readsInputs(); //read Inputs & send data
#endif
#ifdef AINPUTS
readAInputs(); //read Analog Inputs & send data
#endif
@ -451,9 +477,34 @@ int readAInputs(){
void readInputs(){
for(int i= 0;i<Inputs; i++){
int State = digitalRead(InPinmap[i]);
if(InState[i]!= State){
if(InState[i]!= State && millis()- lastInputDebounce[i] > debounceDelay){
InState[i] = State;
sendData('I',InPinmap[i],InState[i]);
lastInputDebounce[i] = millis();
}
}
}
#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();
}
}
}
@ -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();
}

View File

@ -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):