├── .gitignore
├── ArduinoChip.svg
├── LinuxCNC_ArduinoConnector.ino
├── README.md
└── arduino-connector.py
/.gitignore:
--------------------------------------------------------------------------------
1 | .vscode
2 | .vscode/arduino.json
3 |
--------------------------------------------------------------------------------
/ArduinoChip.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
1163 |
--------------------------------------------------------------------------------
/LinuxCNC_ArduinoConnector.ino:
--------------------------------------------------------------------------------
1 | /*
2 | LinuxCNC_ArduinoConnector
3 | By Alexander Richter, info@theartoftinkering.com 2022
4 |
5 | This Software is used as IO Expansion for LinuxCNC. Here i am using a Mega 2560.
6 |
7 | It is NOT intended for timing and security relevant IO's. Don't use it for Emergency Stops or Endstop switches!
8 |
9 | You can create as many digital & analog Inputs, Outputs and PWM Outputs as your Arduino can handle.
10 | You can also generate "virtual Pins" by using latching Potentiometers, which are connected to one analog Pin, but are read in Hal as individual Pins.
11 |
12 | Currently the Software Supports:
13 | - analog Inputs
14 | - latching Potentiometers
15 | - 1 binary encoded selector Switch
16 | - digital Inputs
17 | - digital Outputs
18 | - Matrix Keypad
19 | - Multiplexed LEDs
20 | - Quadrature encoders
21 | - Joysticks
22 |
23 | The Send and receive Protocol is :
24 | To begin Transmitting Ready is send out and expects to receive E: to establish connection. Afterwards Data is exchanged.
25 | Data is only send everythime it changes once.
26 |
27 | Inputs & Toggle Inputs = 'I' -write only -Pin State: 0,1
28 | Outputs = 'O' -read only -Pin State: 0,1
29 | PWM Outputs = 'P' -read only -Pin State: 0-255
30 | Digital LED Outputs = 'D' -read only -Pin State: 0,1
31 | Analog Inputs = 'A' -write only -Pin State: 0-1024
32 | Latching Potentiometers = 'L' -write only -Pin State: 0-max Position
33 | binary encoded Selector = 'K' -write only -Pin State: 0-32
34 | rotary encoder = 'R' -write only -Pin State: up/ down / -2147483648 to 2147483647
35 | joystick = 'R' -write only -Pin State: up/ down / -2147483648 to 2147483647
36 | multiplexed LEDs = 'M' -read only -Pin State: 0,1
37 |
38 | Keyboard Input:
39 | Matrix Keypad = 'M' -write only -Pin State: 0,1
40 |
41 | Communication Status = 'E' -read/Write -Pin State: 0:0
42 |
43 | The Keyboard is encoded in the Number of the Key in the Matrix. The according Letter is defined in the receiving end in the Python Skript.
44 | Here you only define the Size of the Matrix.
45 |
46 | Command 'E0:0' is used for connectivity checks and is send every 5 seconds as keep alive signal. If the Signal is not received again, the Status LED will Flash.
47 | The Board will still work as usual and try to send it's data, so this feature is only to inform the User.
48 |
49 |
50 | This program is free software; you can redistribute it and/or modify
51 | it under the terms of the GNU General Public License as published by
52 | the Free Software Foundation; either version 2 of the License, or
53 | (at your option) any later version.
54 | This program is distributed in the hope that it will be useful,
55 | but WITHOUT ANY WARRANTY; without even the implied warranty of
56 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
57 | See the GNU General Public License for more details.
58 | You should have received a copy of the GNU General Public License
59 | along with this program; if not, write to the Free Software
60 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
61 | */
62 |
63 |
64 |
65 |
66 | //###################################################IO's###################################################
67 |
68 |
69 | #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.
70 | #ifdef INPUTS
71 | const int Inputs = 2; //number of inputs using internal Pullup resistor. (short to ground to trigger)
72 | int InPinmap[] = {8,9};
73 | #endif
74 |
75 | //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.
76 | #define SINPUTS //Define how many Toggle Inputs you want in total and then which Pins you want to be Toggle Inputs.
77 | #ifdef SINPUTS
78 | const int sInputs = 1; //number of inputs using internal Pullup resistor. (short to ground to trigger)
79 | int sInPinmap[] = {10};
80 | #endif
81 |
82 | #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.
83 | #ifdef OUTPUTS
84 | const int Outputs = 2; //number of outputs
85 | int OutPinmap[] = {11,12};
86 | #endif
87 |
88 | //#define PWMOUTPUTS //Use Arduino PWM Capable IO's as PWM Outputs. Define how many PWM Outputs you want in total and then which Pins you want to be PWM Outputs.
89 | #ifdef PWMOUTPUTS
90 | const int PwmOutputs = 2; //number of outputs
91 | int PwmOutPinmap[] = {12,11};
92 | #endif
93 |
94 | //#define AINPUTS //Use Arduino ADC's as Analog Inputs. Define how many Analog Inputs you want in total and then which Pins you want to be Analog Inputs.
95 | //Note that Analog Pin numbering is different to the Print on the PCB.
96 | #ifdef AINPUTS
97 | const int AInputs = 1;
98 | int AInPinmap[] = {0}; //Potentiometer for SpindleSpeed override
99 | int smooth = 200; //number of samples to denoise ADC, try lower numbers on your setup 200 worked good for me.
100 | #endif
101 |
102 |
103 |
104 | /*This is a special mode of AInputs. My machine had originally Selector Knobs with many Pins on the backside to select different Speed Settings.
105 | I turned them into a "Potentiometer" by connecting all Pins with 10K Resistors in series. Then i applied GND to the first and 5V to the last Pin.
106 | Now the Selector is part of an Voltage Divider and outputs different Voltage for each Position. This function generates Pins for each Position in Linuxcnc Hal.
107 |
108 | It can happen, that when you switch position, that the selector is floating for a brief second. This might be detected as Position 0.
109 | This shouldn't be an issue in most usecases, but think about that in your application.
110 |
111 |
112 |
113 | Connect it to an Analog In Pin of your Arduino and define how many of these you want.
114 | Then in the Array, {which Pin, How many Positions}
115 | Note that Analog Pin numbering is different to the Print on the PCB.
116 |
117 | */
118 | //#define LPOTIS
119 | #ifdef LPOTIS
120 | const int LPotis = 2;
121 | const int LPotiPins[LPotis][2] = {
122 | {1,9}, //Latching Knob Spindle Overdrive on A1, has 9 Positions
123 | {2,4} //Latching Knob Feed Resolution on A2, has 4 Positions
124 | };
125 | int margin = 20; //giving it some margin so Numbers dont jitter, make this number smaller if your knob has more than 50 Positions
126 | #endif
127 |
128 |
129 |
130 | //#define BINSEL //Support of an Rotating Knob that was build in my Machine. It encodes 32 Positions with 5 Pins in Binary. This will generate 32 Pins in LinuxCNC Hal.
131 | #ifdef BINSEL
132 | const int BinSelKnobPins[] = {2,6,4,3,5}; //1,2,4,8,16
133 | #endif
134 |
135 |
136 | //#define QUADENC
137 | //Support for Quadrature Encoders. Define Pins for A and B Signals for your encoders. Visit https://www.pjrc.com/teensy/td_libs_Encoder.html for further explanation.
138 | // Download Zip from here: https://github.com/PaulStoffregen/Encoder and import as Library to your Arduino IDE.
139 | #ifdef QUADENC
140 | #include
141 | #define QUADENCS 2 //how many Rotary Encoders do you want?
142 |
143 | // Encoders have 2 signals, which must be connected to 2 pins. There are three options.
144 |
145 | //Best Performance: Both signals connect to interrupt pins.
146 | //Good Performance: First signal connects to an interrupt pin, second to a non-interrupt pin.
147 | //Low Performance: Both signals connect to non-interrupt pins, details below.
148 |
149 | //Board Interrupt Pins LED Pin(do not use)
150 | //Teensy 4.0 - 4.1 All Digital Pins 13
151 | //Teensy 3.0 - 3.6 All Digital Pins 13
152 | //Teensy LC 2 - 12, 14, 15, 20 - 23 13
153 | //Teensy 2.0 5, 6, 7, 8 11
154 | //Teensy 1.0 0, 1, 2, 3, 4, 6, 7, 16
155 | //Teensy++ 2.0 0, 1, 2, 3, 18, 19, 36, 37 6
156 | //Teensy++ 1.0 0, 1, 2, 3, 18, 19, 36, 37
157 | //Arduino Due All Digital Pins 13
158 | //Arduino Uno 2, 3 13
159 | //Arduino Leonardo 0, 1, 2, 3 13
160 | //Arduino Mega 2, 3, 18, 19, 20, 21 13
161 | //Sanguino 2, 10, 11 0
162 |
163 | Encoder Encoder0(2,3); //A,B Pin
164 | Encoder Encoder1(31,33); //A,B Pin
165 | //Encoder Encoder2(A,B);
166 | //Encoder Encoder3(A,B);
167 | //Encoder Encoder4(A,B);
168 | const int QuadEncSig[] = {2,2}; //define wich kind of Signal you want to generate.
169 | //1= send up or down signal (typical use for selecting modes in hal)
170 | //2= send position signal (typical use for MPG wheel)
171 | const int QuadEncMp[] = {4,4}; //some Rotary encoders send multiple Electronical Impulses per mechanical pulse. How many Electrical impulses are send for each mechanical Latch?
172 |
173 | #endif
174 |
175 | //#define JOYSTICK //Support of an Rotating Knob that was build in my Machine. It encodes 32 Positions with 5 Pins in Binary. This will generate 32 Pins in LinuxCNC Hal.
176 | #ifdef JOYSTICK
177 | const int JoySticks = 1; // Number of potentiometers connected
178 | const int JoyStickPins[JoySticks*2] = {0, 1}; // Analog input pins for the potentiometers
179 | const int middleValue = 512; // Middle value of the potentiometer
180 | const int deadband = 20; // Deadband range around the middleValue
181 | const float scalingFactor = 0.01; // Scaling factor to control the impact of distanceFromMiddle
182 | #endif
183 |
184 |
185 |
186 |
187 |
188 |
189 | //The Software will detect if there is an communication issue. When you power on your machine, the Buttons etc won't work, till LinuxCNC is running. THe StatusLED will inform you about the State of Communication.
190 | // Slow Flash = Not Connected
191 | // Steady on = connected
192 | // short Flash = connection lost.
193 |
194 | // if connection is lost, something happened. (Linuxcnc was closed for example or USB Connection failed.) It will recover when Linuxcnc is restartet. (you could also run "unloadusr arduino", "loadusr arduino" in Hal)
195 | // Define an Pin you want to connect the LED to. it will be set as Output indipendand of the OUTPUTS function, so don't use Pins twice.
196 | // If you use Digital LED's such as WS2812 or PL9823 (only works if you set up the DLED settings below) you can also define a position of the LED. In this case StatLedPin will set the number of the Digital LED Chain.
197 |
198 | #define STATUSLED
199 | #ifdef STATUSLED
200 | const int StatLedPin = 13; //Pin for Status LED
201 | const int StatLedErrDel[] = {1000,10}; //Blink Timing for Status LED Error (no connection)
202 | const int DLEDSTATUSLED = 0; //set to 1 to use Digital LED instead. set StatLedPin to the according LED number in the chain.
203 | #endif
204 |
205 |
206 |
207 |
208 | /* Instead of connecting LED's to Output pins, you can also connect digital LED's such as WS2812 or PL9823.
209 | This way you can have how many LED's you want and also define it's color with just one Pin.
210 |
211 | DLEDcount defines, how many Digital LED's you want to control. Count from 0. For Each LED an output Pin will be generated in LinuxCNC hal.
212 | To use this funcion you need to have the Adafruit_NeoPixel.h Library installed in your Arduino IDE.
213 |
214 | In LinuxCNC you can set the Pin to HIGH and LOW, for both States you can define an color per LED.
215 | This way, you can make them glow or shut of, or have them Change color, from Green to Red for example.
216 |
217 | DledOnColors defines the color of each LED when turned "on". For each LED set {Red,Green,Blue} with Numbers from 0-255.
218 | depending on the Chipset of your LED's Colors might be in a different order. You can try it out by setting {255,0,0} for example.
219 |
220 | You need to define a color to DledOffColors too. Like the Name suggests it defines the color of each LED when turned "off".
221 | If you want the LED to be off just define {0,0,0}, .
222 |
223 |
224 | If you use STATUSLED, it will also take the colors of your definition here.
225 | */
226 |
227 | //#define DLED
228 | #ifdef DLED
229 | #include
230 |
231 | const int DLEDcount = 8; //How Many DLED LED's are you going to connect?
232 | const int DLEDPin = 4; //Where is DI connected to?
233 | const int DLEDBrightness = 70; //Brightness of the LED's 0-100%
234 |
235 | int DledOnColors[DLEDcount][3] = {
236 | {0,0,255},
237 | {255,0,0},
238 | {0,255,0},
239 | {0,255,0},
240 | {0,255,0},
241 | {0,255,0},
242 | {0,255,0},
243 | {0,255,0}
244 | };
245 |
246 | int DledOffColors[DLEDcount][3] = {
247 | {0,0,0},
248 | {0,0,0},
249 | {255,0,0},
250 | {255,0,0},
251 | {255,0,0},
252 | {0,0,255},
253 | {0,0,255},
254 | {0,0,255}
255 | };
256 |
257 |
258 | Adafruit_NeoPixel strip(DLEDcount, DLEDPin, NEO_GRB + NEO_KHZ800);//Color sequence is different for LED Chipsets. Use RGB for WS2812 or GRB for PL9823.
259 |
260 |
261 | #endif
262 | /*
263 | Matrix Keypads are supported. The input is NOT added as HAL Pin to LinuxCNC. Instead it is inserted to Linux as Keyboard direktly.
264 | So you could attach a QWERT* Keyboard to the arduino and you will be able to write in Linux with it (only while LinuxCNC is running!)
265 | */
266 | //#define KEYPAD
267 | #ifdef KEYPAD
268 | const int numRows = 4; // Define the number of rows in the matrix
269 | const int numCols = 4; // Define the number of columns in the matrix
270 |
271 | // Define the pins connected to the rows and columns of the matrix
272 | const int rowPins[numRows] = {2, 3, 4, 5};
273 | const int colPins[numCols] = {6, 7, 8, 9};
274 | int keys[numRows][numCols] = {0};
275 | int lastKey= -1;
276 | #endif
277 |
278 |
279 | //#define MULTIPLEXLEDS // Special mode for Multiplexed LEDs. This mode is experimental and implemented to support Matrix Keyboards with integrated Key LEDs.
280 | // check out this thread on LinuxCNC Forum for context. https://forum.linuxcnc.org/show-your-stuff/49606-matrix-keyboard-controlling-linuxcnc
281 | // for Each LED an Output Pin is generated in LinuxCNC.
282 |
283 | //If your Keyboard shares pins with the LEDs, you have to check polarity.
284 | //rowPins[numRows] = {} are Pullup Inputs
285 | //colPins[numCols] = {} are GND Pins
286 | //the matrix keyboard described in the thread shares GND Pins between LEDs and KEys, therefore LedGndPins[] and colPins[numCols] = {} use same Pins.
287 |
288 | #ifdef MULTIPLEXLEDS
289 |
290 | const int numVccPins = 8; // Number of rows in the matrix
291 | const int numGndPins = 8; // Number of columns in the matrix
292 | const int LedVccPins[] = {30,31,32,33,34,35,36,37}; // Arduino pins connected to rows
293 | const int LedGndPins[] = {40,41,42,43,44,45,46,47}; // Arduino pins connected to columns
294 |
295 | // Define the LED matrix
296 | int ledStates[numVccPins*numGndPins] = {0};
297 |
298 | unsigned long previousMillis = 0;
299 | const unsigned long interval = 500; // Time (in milliseconds) per LED display
300 |
301 | int currentLED = 0;
302 | #endif
303 |
304 |
305 |
306 |
307 | //#define DEBUG
308 | //####################################### END OF CONFIG ###########################
309 |
310 | //###Misc Settings###
311 | const int timeout = 10000; // timeout after 10 sec not receiving Stuff
312 | const int debounceDelay = 50;
313 |
314 |
315 | //Variables for Saving States
316 | #ifdef INPUTS
317 | int InState[Inputs];
318 | int oldInState[Inputs];
319 | unsigned long lastInputDebounce[Inputs];
320 | #endif
321 | #ifdef SINPUTS
322 | int sInState[sInputs];
323 | int soldInState[sInputs];
324 | int togglesinputs[sInputs];
325 | unsigned long lastsInputDebounce[sInputs];
326 | #endif
327 | #ifdef OUTPUTS
328 | int OutState[Outputs];
329 | int oldOutState[Outputs];
330 | #endif
331 | #ifdef PWMOUTPUTS
332 | int OutPWMState[PwmOutputs];
333 | int oldOutPWMState[PwmOutputs];
334 | #endif
335 | #ifdef AINPUTS
336 | int oldAinput[AInputs];
337 | unsigned long sumAinput[AInputs];
338 | #endif
339 | #ifdef LPOTIS
340 | int Lpoti[LPotis];
341 | int oldLpoti[LPotis];
342 | #endif
343 | #ifdef BINSEL
344 | int oldAbsEncState;
345 | #endif
346 | #ifdef KEYPAD
347 | byte KeyState = 0;
348 | #endif
349 | #ifdef MULTIPLEXLEDS
350 | byte KeyLedStates[numVccPins*numGndPins];
351 | #endif
352 | #if QUADENCS == 1
353 | const int QuadEncs = 1;
354 | #endif
355 | #if QUADENCS == 2
356 | const int QuadEncs = 2;
357 | #endif
358 | #if QUADENCS == 3
359 | const int QuadEncs = 3;
360 | #endif
361 | #if QUADENCS == 4
362 | const int QuadEncs = 4;
363 | #endif
364 | #if QUADENCS == 5
365 | const int QuadEncs = 5;
366 | #endif
367 | #ifdef QUADENC
368 | long EncCount[QuadEncs];
369 | long OldEncCount[QuadEncs];
370 | #endif
371 |
372 |
373 | #ifdef JOYSTICK
374 | long counter[JoySticks*2] = {0}; // Initialize an array for the counters
375 | long prevCounter[JoySticks*2] = {0}; // Initialize an array for the previous counters
376 | float incrementFactor[JoySticks*2] = {0.0}; // Initialize an array for the incrementFactors
377 | unsigned long lastUpdateTime[JoySticks*2] = {0}; // Store the time of the last update for each potentiometer
378 |
379 | #endif
380 |
381 | //### global Variables setup###
382 | //Please don't touch them
383 | unsigned long oldmillis = 0;
384 | unsigned long newcom = 0;
385 | unsigned long lastcom = 0;
386 | int connectionState = 0;
387 |
388 | #define STATE_CMD 0
389 | #define STATE_IO 1
390 | #define STATE_VALUE 2
391 |
392 |
393 | byte state = STATE_CMD;
394 | char inputbuffer[5];
395 | byte bufferIndex = 0;
396 | char cmd = 0;
397 | uint16_t io = 0;
398 | uint16_t value = 0;
399 |
400 | // Function Prototypes
401 | void readCommands();
402 | void commandReceived(char cmd, uint16_t io, uint16_t value);
403 | void multiplexLeds();
404 | void readKeypad();
405 | int readAbsKnob();
406 | void readsInputs();
407 | void readInputs();
408 | void readAInputs();
409 | void readLPoti();
410 | void controlDLED(int Pin, int Stat);
411 | void initDLED();
412 | void writePwmOutputs(int Pin, int Stat);
413 | void writeOutputs(int Pin, int Stat);
414 | void StatLedErr(int offtime, int ontime);
415 | void flushSerial();
416 | void sendData(char sig, int pin, int state);
417 | void reconnect();
418 | void comalive();
419 | void readEncoders();
420 | void readJoySticks();
421 |
422 | void setup() {
423 |
424 | #ifdef INPUTS
425 | //setting Inputs with internal Pullup Resistors
426 | for(int i= 0; i= 100) { // Adjust 100 milliseconds based on your needs
540 | lastUpdateTime[i] = currentTime; // Update the last update time for this potentiometer
541 |
542 | int potValue = analogRead(JoyStickPins[i]); // Read the potentiometer value
543 |
544 | // Calculate the distance of the potentiometer value from the middle
545 | int distanceFromMiddle = potValue - middleValue;
546 |
547 | // Apply deadband to ignore small variations around middleValue
548 | if (abs(distanceFromMiddle) <= deadband) {
549 | incrementFactor[i] = 0.0; // Set incrementFactor to 0 within the deadband range
550 | } else {
551 | // Apply non-linear scaling to distanceFromMiddle to get the incrementFactor
552 | incrementFactor[i] = pow((distanceFromMiddle * scalingFactor), 3);
553 | }
554 |
555 | // Update the counter if the incrementFactor has reached a full number
556 | if (incrementFactor[i] >= 1.0 || incrementFactor[i] <= -1.0) {
557 | counter[i] += static_cast(incrementFactor[i]); // Increment or decrement the counter by the integer part of incrementFactor
558 | incrementFactor[i] -= static_cast(incrementFactor[i]); // Subtract the integer part from incrementFactor
559 | }
560 |
561 | // Check if the counter value has changed
562 | if (counter[i] != prevCounter[i]) {
563 | sendData('R',JoyStickPins[i],counter[i]);
564 | // Update the previous counter value with the current counter value
565 | prevCounter[i] = counter[i];
566 | }
567 | }
568 | }
569 | }
570 | #endif
571 |
572 | #ifdef QUADENC
573 | void readEncoders(){
574 | if(QuadEncs>=1){
575 | #if QUADENCS >= 1
576 | EncCount[0] = Encoder0.read()/QuadEncMp[0];
577 | #endif
578 | }
579 | if(QuadEncs>=2){
580 | #if QUADENCS >= 2
581 | EncCount[1] = Encoder1.read()/QuadEncMp[1];
582 | #endif
583 | }
584 | if(QuadEncs>=3){
585 | #if QUADENCS >= 3
586 | EncCount[2] = Encoder2.read()/QuadEncMp[2];
587 | #endif
588 | }
589 | if(QuadEncs>=4){
590 | #if QUADENCS >= 4
591 | EncCount[3] = Encoder3.read()/QuadEncMp[3];
592 | #endif
593 | }
594 | if(QuadEncs>=5){
595 | #if QUADENCS >= 5
596 | EncCount[4] = Encoder4.read()/QuadEncMp[4];
597 | #endif
598 | }
599 |
600 | for(int i=0; i EncCount[i]){
613 | sendData('R',i,0); //send Increase by 1 Signal
614 | OldEncCount[i] = EncCount[i];
615 | }
616 | }
617 | }
618 | }
619 |
620 | #endif
621 |
622 | void comalive(){
623 | if(lastcom == 0){ //no connection yet. send E0:0 periodicly and wait for response
624 | while (lastcom == 0){
625 | readCommands();
626 | flushSerial();
627 | Serial.println("E0:0");
628 | delay(200);
629 | #ifdef STATUSLED
630 | StatLedErr(1000,1000);
631 | #endif
632 | }
633 | connectionState = 1;
634 | flushSerial();
635 | #ifdef DEBUG
636 | Serial.println("first connect");
637 | #endif
638 | }
639 | if(millis() - lastcom > timeout){
640 | #ifdef STATUSLED
641 | StatLedErr(500,200);
642 | #endif
643 | if(connectionState == 1){
644 | #ifdef DEBUG
645 | Serial.println("disconnected");
646 | #endif
647 | connectionState = 2;
648 | }
649 |
650 | }
651 | else{
652 | connectionState=1;
653 | #ifdef STATUSLED
654 | if(DLEDSTATUSLED == 1){
655 | #ifdef DLED
656 | controlDLED(StatLedPin, 1);
657 | #endif
658 | }
659 | else{
660 | digitalWrite(StatLedPin, HIGH);
661 | }
662 | #endif
663 | }
664 | }
665 |
666 |
667 | void reconnect(){
668 | #ifdef DEBUG
669 | Serial.println("reconnected");
670 | Serial.println("resending Data");
671 | #endif
672 |
673 | #ifdef INPUTS
674 | for (int x = 0; x < Inputs; x++){
675 | InState[x]= -1;
676 | }
677 | #endif
678 | #ifdef SINPUTS
679 | for (int x = 0; x < sInputs; x++){
680 | soldInState[x]= -1;
681 | togglesinputs[x] = 0;
682 | }
683 | #endif
684 | #ifdef AINPUTS
685 | for (int x = 0; x < AInputs; x++){
686 | oldAinput[x] = -1;
687 | sumAinput[x] = 0;
688 | }
689 | #endif
690 | #ifdef LPOTIS
691 | for (int x = 0; x < LPotis; x++){
692 | oldLpoti[x] = -1;
693 | }
694 | #endif
695 | #ifdef BINSEL
696 | oldAbsEncState = -1;
697 | #endif
698 |
699 |
700 | #ifdef INPUTS
701 | readInputs(); //read Inputs & send data
702 | #endif
703 | #ifdef SINPUTS
704 | readsInputs(); //read Inputs & send data
705 | #endif
706 | #ifdef AINPUTS
707 | readAInputs(); //read Analog Inputs & send data
708 | #endif
709 | #ifdef LPOTIS
710 | readLPoti(); //read LPotis & send data
711 | #endif
712 | #ifdef BINSEL
713 | readAbsKnob(); //read ABS Encoder & send data
714 | #endif
715 | #ifdef MULTIPLEXLEDS
716 | multiplexLeds(); //Flash LEDS.
717 | #endif
718 |
719 | connectionState = 1;
720 |
721 |
722 | }
723 |
724 |
725 | void sendData(char sig, int pin, int state){
726 | Serial.print(sig);
727 | Serial.print(pin);
728 | Serial.print(":");
729 | Serial.println(state);
730 | }
731 |
732 | void flushSerial(){
733 | while (Serial.available() > 0) {
734 | Serial.read();
735 | }
736 | }
737 |
738 | #ifdef STATUSLED
739 | void StatLedErr(int offtime, int ontime){
740 | unsigned long newMillis = millis();
741 |
742 | if (newMillis - oldmillis >= offtime){
743 | #ifdef DLED
744 | if(DLEDSTATUSLED == 1){
745 | controlDLED(StatLedPin, 1);}
746 | #endif
747 | if(DLEDSTATUSLED == 0){digitalWrite(StatLedPin, HIGH);}
748 | }
749 | if (newMillis - oldmillis >= offtime+ontime){{
750 | #ifdef DLED
751 | if(DLEDSTATUSLED == 1){
752 | controlDLED(StatLedPin, 0);}
753 | #endif
754 | if(DLEDSTATUSLED == 0){digitalWrite(StatLedPin, LOW);}
755 |
756 | oldmillis = newMillis;
757 |
758 | }
759 | }
760 |
761 | }
762 | #endif
763 |
764 | #ifdef OUTPUTS
765 | void writeOutputs(int Pin, int Stat){
766 | digitalWrite(Pin, Stat);
767 | }
768 | #endif
769 |
770 | #ifdef PWMOUTPUTS
771 | void writePwmOutputs(int Pin, int Stat){
772 | analogWrite(Pin, Stat);
773 | }
774 |
775 | #endif
776 |
777 | #ifdef DLED
778 | void initDLED(){
779 | strip.begin();
780 | strip.setBrightness(DLEDBrightness);
781 |
782 | for (int i = 0; i < DLEDcount; i++) {
783 | strip.setPixelColor(i, strip.Color(DledOffColors[i][0],DledOffColors[i][1],DledOffColors[i][2]));
784 | }
785 | strip.show();
786 | #ifdef DEBUG
787 | Serial.print("DLED initialised");
788 | #endif
789 | }
790 |
791 | void controlDLED(int Pin, int Stat){
792 | if(Stat == 1){
793 |
794 | strip.setPixelColor(Pin, strip.Color(DledOnColors[Pin][0],DledOnColors[Pin][1],DledOnColors[Pin][2]));
795 | #ifdef DEBUG
796 | Serial.print("DLED No.");
797 | Serial.print(Pin);
798 | Serial.print(" set to:");
799 | Serial.println(Stat);
800 |
801 | #endif
802 | }
803 | else{
804 |
805 | strip.setPixelColor(Pin, strip.Color(DledOffColors[Pin][0],DledOffColors[Pin][1],DledOffColors[Pin][2]));
806 | #ifdef DEBUG
807 | Serial.print("DLED No.");
808 | Serial.print(Pin);
809 | Serial.print(" set to:");
810 | Serial.println(Stat);
811 |
812 | #endif
813 | }
814 | strip.show();
815 | }
816 | #endif
817 |
818 | #ifdef LPOTIS
819 | void readLPoti(){
820 | for(int i= 0;i debounceDelay){
862 | InState[i] = State;
863 | sendData('I',InPinmap[i],InState[i]);
864 |
865 | lastInputDebounce[i] = millis();
866 | }
867 | }
868 | }
869 | #endif
870 | #ifdef SINPUTS
871 | void readsInputs(){
872 | for(int i= 0;i debounceDelay){
875 | // Button state has changed and debounce delay has passed
876 |
877 | if (sInState[i] == LOW || soldInState[i]== -1) { // Stuff after || is only there to send States at Startup
878 | // Button has been pressed
879 | togglesinputs[i] = !togglesinputs[i]; // Toggle the LED state
880 |
881 | if (togglesinputs[i]) {
882 | sendData('I',sInPinmap[i],togglesinputs[i]); // Turn the LED on
883 | }
884 | else {
885 | sendData('I',sInPinmap[i],togglesinputs[i]); // Turn the LED off
886 | }
887 | }
888 | soldInState[i] = sInState[i];
889 | lastsInputDebounce[i] = millis();
890 | }
891 | }
892 | }
893 | #endif
894 |
895 | #ifdef BINSEL
896 | int readAbsKnob(){
897 | int var = 0;
898 | if(digitalRead(BinSelKnobPins[0])==1){
899 | var += 1;
900 | }
901 | if(digitalRead(BinSelKnobPins[1])==1){
902 | var += 2;
903 | }
904 | if(digitalRead(BinSelKnobPins[2])==1){
905 | var += 4;
906 | }
907 | if(digitalRead(BinSelKnobPins[3])==1){
908 | var += 8;
909 | }
910 | if(digitalRead(BinSelKnobPins[4])==1){
911 | var += 16;
912 | }
913 | if(var != oldAbsEncState){
914 | Serial.print("K0:");
915 | Serial.println(var);
916 | }
917 | oldAbsEncState = var;
918 | return (var);
919 | }
920 | #endif
921 |
922 | #ifdef KEYPAD
923 | void readKeypad(){
924 | //detect if Button is Pressed
925 | for (int col = 0; col < numCols; col++) {
926 | pinMode(colPins[col], OUTPUT);
927 | digitalWrite(colPins[col], LOW);
928 | // Read the state of the row pins
929 | for (int row = 0; row < numRows; row++) {
930 | pinMode(rowPins[row], INPUT_PULLUP);
931 | if (digitalRead(rowPins[row]) == LOW && lastKey != keys[row][col]) {
932 | // A button has been pressed
933 | sendData('M',keys[row][col],1);
934 | lastKey = keys[row][col];
935 | break;
936 |
937 | }
938 | if (digitalRead(rowPins[row]) == HIGH && lastKey == keys[row][col]) {
939 | // The Last Button has been unpressed
940 | sendData('M',keys[row][col],0);
941 | lastKey = -1; //reset Key pressed
942 | break;
943 | }
944 | }
945 |
946 | // Set the column pin back to input mode
947 | pinMode(colPins[col], INPUT);
948 | }
949 |
950 | }
951 | #endif
952 |
953 | #ifdef MULTIPLEXLEDS
954 | void multiplexLeds() {
955 | unsigned long currentMillis = millis();
956 | //init Multiplex
957 | #ifdef KEYPAD //if Keyboard is presend disable Pullup Resistors to not mess with LEDs while a Button is pressed.
958 | for (int row = 0; row < numRows; row++) {
959 | pinMode(rowPins[row], OUTPUT);
960 | digitalWrite(rowPins[row], LOW);
961 | }
962 | #endif
963 |
964 | for (int i = 0; i < numVccPins; i++) {
965 | pinMode(LedVccPins[i], OUTPUT);
966 | digitalWrite(LedVccPins[i], LOW); // Set to LOW to disable all Vcc Pins
967 | }
968 | for (int i = 0; i < numGndPins; i++) {
969 | pinMode(LedGndPins[i], OUTPUT);
970 | digitalWrite(LedGndPins[i], HIGH); // Set to HIGH to disable all GND Pins
971 | }
972 |
973 | for(currentLED = 0; currentLED < numVccPins*numGndPins ;currentLED ++){
974 | if(ledStates[currentLED] == 1){ //only handle turned on LEDs
975 | digitalWrite(LedVccPins[currentLED/numVccPins],HIGH); //turn current LED on
976 | digitalWrite(LedGndPins[currentLED%numGndPins],LOW);
977 |
978 | #ifdef debug
979 | Serial.print("VCC: ");
980 | Serial.print(LedVccPins[currentLED/numVccPins]);
981 | Serial.print(" GND: ");
982 | Serial.println(LedGndPins[currentLED%numGndPins]);
983 | #endif
984 |
985 | delayMicroseconds(interval); //wait couple ms
986 | digitalWrite(LedVccPins[currentLED/numVccPins],LOW); //turn off and go to next one
987 | digitalWrite(LedGndPins[currentLED%numGndPins],HIGH);
988 | }
989 | }
990 | /*
991 | }
992 | if(ledStates[currentLED]==0){//If currentLED is Off, manage next one.
993 | currentLED++;
994 | }
995 | if(currentLED >= numVccPins*numGndPins){
996 | currentLED= 0;
997 | }
998 | */
999 | }
1000 | #endif
1001 |
1002 | void commandReceived(char cmd, uint16_t io, uint16_t value){
1003 | #ifdef OUTPUTS
1004 | if(cmd == 'O'){
1005 | writeOutputs(io,value);
1006 | lastcom=millis();
1007 |
1008 | }
1009 | #endif
1010 | #ifdef PWMOUTPUTS
1011 | if(cmd == 'P'){
1012 | writePwmOutputs(io,value);
1013 | lastcom=millis();
1014 |
1015 | }
1016 | #endif
1017 | #ifdef DLED
1018 | if(cmd == 'D'){
1019 | controlDLED(io,value);
1020 | lastcom=millis();
1021 | #ifdef debug
1022 | Serial.print("DLED:");
1023 | Serial.print(io);
1024 | Serial.print(" State:");
1025 | Serial.println(DLEDstate[io]);
1026 | #endif
1027 |
1028 | }
1029 | #endif
1030 | #ifdef MULTIPLEXLEDS
1031 | if(cmd == 'M'){
1032 | ledStates[io] = value; // Set the LED state
1033 | lastcom=millis();
1034 | #ifdef DEBUG
1035 | Serial.print("multiplexed Led No:");
1036 | Serial.print(io);
1037 | Serial.print("Set to:");
1038 | Serial.println(ledStates[io]);
1039 | #endif
1040 |
1041 | }
1042 | #endif
1043 |
1044 |
1045 | if(cmd == 'E'){
1046 | lastcom=millis();
1047 | if(connectionState == 2){
1048 | reconnect();
1049 | }
1050 | }
1051 |
1052 |
1053 | #ifdef DEBUG
1054 | Serial.print("I Received= ");
1055 | Serial.print(cmd);
1056 | Serial.print(io);
1057 | Serial.print(":");
1058 | Serial.println(value);
1059 | #endif
1060 | }
1061 |
1062 |
1063 | void readCommands(){
1064 | byte current;
1065 | while(Serial.available() > 0){
1066 | current = Serial.read();
1067 | switch(state){
1068 | case STATE_CMD:
1069 | cmd = current;
1070 | state = STATE_IO;
1071 | bufferIndex = 0;
1072 | break;
1073 | case STATE_IO:
1074 | if(isDigit(current)){
1075 | inputbuffer[bufferIndex++] = current;
1076 | }else if(current == ':'){
1077 | inputbuffer[bufferIndex] = 0;
1078 | io = atoi(inputbuffer);
1079 | state = STATE_VALUE;
1080 | bufferIndex = 0;
1081 |
1082 | }
1083 | else{
1084 | #ifdef DEBUG
1085 | Serial.print("Ungültiges zeichen: ");
1086 | Serial.println(current);
1087 | #endif
1088 | }
1089 | break;
1090 | case STATE_VALUE:
1091 | if(isDigit(current)){
1092 | inputbuffer[bufferIndex++] = current;
1093 | }
1094 | else if(current == '\n'){
1095 | inputbuffer[bufferIndex] = 0;
1096 | value = atoi(inputbuffer);
1097 | commandReceived(cmd, io, value);
1098 | state = STATE_CMD;
1099 | }
1100 | else{
1101 | #ifdef DEBUG
1102 | Serial.print("Ungültiges zeichen: ");
1103 | Serial.println(current);
1104 | #endif
1105 |
1106 | }
1107 | break;
1108 | }
1109 |
1110 | }
1111 | }
1112 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # LinuxCNC_ArduinoConnector
3 |
4 |
5 |
6 | By Alexander Richter, info@theartoftinkering.com 2022
7 | please consider supporting me on Patreon:
8 | https://www.patreon.com/theartoftinkering
9 |
10 | Website: https://theartoftinkering.com
11 | Youtube: https://youtube.com/@theartoftinkering
12 |
13 |
14 | This Project enables you to connect an Arduino to LinuxCNC and provides as many IO's as you could ever wish for.
15 | This Software is used as IO Expansion for LinuxCNC.
16 |
17 | ## It is NOT intended for timing and security relevant IO's. Don't use it for Emergency Stops or Endstop switches! ##
18 |
19 |
20 | You can create as many digital & analog Inputs, Outputs and PWM Outputs as your Arduino can handle.
21 | It also supports Digital LEDs such as WS2812 or PL9823. This way you can have as many LEDs as you want and you can also define the color of them with just one Pin.
22 |
23 |
24 | | Currently the Software Supports: | Arduino Mega | Ardunio Micro | Ardunio Uno |
25 | | --------------------------------------------- | ------------ | ------------- | ----------- |
26 | | Analog Inputs | Up to 16 | Up to 12 | Up to 6 |
27 | | Digital Inputs | Up to 52 | Up to 20 | Up to 12 |
28 | | Digital Outputs | Up to 52 | Up to 20 | Up to 12 |
29 | | PWM Outputs | Up to 15 | Up to 7 | Up to 6 |
30 | | Digital RGB LEDs like WS2812 or PL9823 | ~ 1000 | ~ 1000 | ~ 1000 |
31 | | latching Potentiometers / Selector Switches | Up to 16 | Up to 12 | Up to 6 |
32 | | binary encoded Selector Switch | 1 | 1 | 1 |
33 | | Quadrature Encoder Input | 3 or more | 1 or more | 1 or more |
34 | | Joystick Support (2Axis) | 8 | 6 | 3 |
35 | | Matrix Keyboard | 1 | 1 | 1 |
36 | | Multiplexed LEDs | ~ 1000 | ~ 1000 | ~ 1000 |
37 |
38 |
39 | Planned Features:
40 | - Temperature Probes using 4.7k Pullup-Resistor
41 | - Support for i2C LCDs
42 |
43 | # Compatiblity
44 | This software works with LinuxCNC 2.8, 2.9 and 2.10. For 2.8, however, you have to change #!/usr/bin/python3.9 in the first line of arduino.py to #!/usr/bin/python2.7.
45 |
46 | You should be able to use any Arduino or Arduino compatible Boards, currently Tested are:
47 | Arduino Mega 2560
48 | Arduino Nano
49 | Arduino Duemilanove
50 |
51 | Other Arduino compatible Boards like Teensy should work fine also.
52 |
53 | # Configuration
54 | To Install LinuxCNC_ArduinoConnector.ino on your Arduino first work through the settings in the beginning of the file.
55 | The Settings are commented in the file.
56 |
57 | To test your Arduino you can connect to it after flashing with the Arduino IDE. Set your Baudrate to 115200.
58 | In the beginning the Arduino will Spam ```E0:0``` to the console. This is used to establish connection.
59 | Just return ```E0:0``` to it. You can now communicate with the Arduino. Further info is in the Chapter [Serial Communication](#serial-communication-over-usb)
60 |
61 |
62 | # Installation
63 | 1. configure the .ino file to your demands and flash it to your arduino
64 | 2. connect the arduino to your LinuxCNC Computer via USB
65 | 3. install python-serial
66 | ```sudo apt-get install python-serial```
67 | 4. edit arduino.py to match your arduino settings. If you're running 2.8 change
68 | #!/usr/bin/env python3 in the first line of arduino.py to #!/usr/bin/python2.7.
69 | 5. also check if the Serial adress is correct for your Arduino. I found it easyest to run
70 | ```sudo dmesg | grep tty``` in Terminal while plugging and unplugging the arduino a couple of times and whatch which entry is changing.
71 | 6. make arduino.py executable with chmod +x, delete the suffix .py and copy
72 | it to /usr/bin
73 | ```sudo chmod +x arduino.py ```
74 | ```sudo cp arduino-connector.py /usr/bin/arduino-connector ```
75 |
76 | 7. add this entry to the end of your hal file: ```loadusr arduino-connector```
77 |
78 | # Testing
79 | To test your Setup, you can run ```halrun``` in Terminal.
80 | Then you will see halcmd:
81 |
82 | Enter ```loadusr arduino-connector``` and then ```show pin```
83 |
84 | All the Arduino generated Pins should now be listed and the State they are in.
85 | You can click buttons now and if you run show pin again the state should've changed.
86 |
87 | you can also set Pins that are listed in DIR as IN.
88 | Enter "setp arduino.DLED.1 TRUE" for example. This will set said Pin to HIGH or in this case, if you have it set up turn the 2. Digital LED on.
89 |
90 |
91 | You can now use arduino pins in your hal file.
92 | Pin Names are named arduino.[Pin Type]-[Pin Number]. Example:
93 | arduino.digital-in-32 for Pin 32 on an Arduino Mega2560
94 |
95 | Watch the Video explanation on Youtube:
96 | [](https://www.youtube.com/watch?v=bjKfnLbsvgA&list=PLdrOU2f3sjtApTdxhmAiXL4lET_ZnntGc "How to set up and test arduino-connector with LinuxCNC")
97 |
98 |
99 | # Configuration - HowTo
100 | In the Arduino .ino File you will see the configuration Parameters for each kind of Signal.
101 | For example we will take a look at the First setting:
102 |
103 | > #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.
104 | > #ifdef INPUTS
105 | > const int Inputs = 5; //number of inputs using internal Pullup resistor. (short to ground to trigger)
106 | > int InPinmap[] = {37,38,39,40,41};
107 | > #endif
108 |
109 | You can easily modify it to fit your needs. Set Inputs to how many Pins you want to use as Inputs and edit the Array InPinmap by setting the Pin Number that should be set as Input. You can add as many as you want until your Arduino runs out of available Pins.
110 |
111 | After you've set your Pin definitions, copy your settings over to the arduino.py file.
112 | The .ino is written in C while the other one is written in Python, hence the Syntax is a little different.
113 | You only need to worry that the contents of the variables match.
114 |
115 | > Inputs = 5
116 | > InPinmap = [37,38,39,40,41]
117 |
118 |
119 | # Analog Inputs
120 | These are used for example to connect Potentiometers. You can add as many as your Arduino has Analog Pins.
121 | The Software has a smoothing parameter, which will remove jitter.
122 |
123 | # Digital Inputs
124 | Digital Inputs use internal Pullup Resistors. So to trigger them you just short the Pin to Ground. There are two Digital Input Types implemented.
125 | Don't use them for Timing or Safety relevant Stuff like Endstops or Emergency Switches.
126 | 1. INPUTS uses the spezified Pins as Inputs. The Value is parsed to LinuxCNC dirketly. There is also a inverted Parameter per Pin.
127 | 2. Trigger INPUTS (SINPUTS) are handled like INPUTS, but simulate Latching Buttons. So when you press once, the Pin goes HIGH and stays HIGH, until you press the Button again.
128 | # Digital Outputs
129 | Digital Outputs drive the spezified Arduinos IO's as Output Pins. You can use it however you want, but don't use it for Timing or Safety relevant Stuff like Stepper Motors.
130 | # support of Digital RGB LEDs like WS2812 or PL9823
131 | Digital LED's do skale very easily, you only need one Pin to drive an infinite amount of them.
132 | To make implementation in LinuxCNC easy you can set predefined LED RGB colors.
133 | You can set a color for "on" and "off" State for each LED.
134 | LED colors are set with values 0-255 for Red, Green and Blue. 0 beeing off and 255 beeing full on.
135 | Here are two examples:
136 |
137 | 1. This LED should be glowing Red when "on" and just turn off when "off".
138 | The Setting in Arduino is:
139 | ```int DledOnColors[DLEDcount][3] = {{255,0,0}};```
140 |
141 | ```int DledOffColors[DLEDcount][3] = {{0,0,0}};```
142 |
143 |
144 | 2. This LED should glow Green when "on" and Red when "off".
145 | ```int DledOnColors[DLEDcount][3] = {{0,255,0}};```
146 |
147 | ```int DledOffColors[DLEDcount][3] = {{255,0,0}};```
148 |
149 | Depending on the used LED Chipset, Color sequence can vary. Please try, which value correspons to which color with your LED's.
150 | Typically it should be R G B for WS2812 and G R B for PL9823.
151 | You can mix both in one chain, just modify the color values accordingly.
152 |
153 | Watch the Video explanation on Youtube:
154 | [](https://www.youtube.com/watch?v=L_FBEtP9il0&list=PLdrOU2f3sjtApTdxhmAiXL4lET_ZnntGc&index=2 "using digital RGB LEDs with LinuxCNC")
155 |
156 |
157 |
158 | # Latching Potentiometers / Selector Switches
159 | This is a special Feature for rotary Selector Switches. Instead of loosing one Pin per Selection you can turn your Switch in a Potentiometer by soldering 10K resistors between the Pins and connecting the Selector Pin to an Analog Input.
160 | The Software will divide the Measured Value and create Hal Pins from it. This way you can have Selector Switches with many positions while only needing one Pin for it.
161 |
162 | # 1 binary encoded Selector Switch input
163 | Some rotary Selector Switches work with Binary Encoded Positions. The Software Supports Encoders with 32 Positions. (this could be more if requested)
164 | For each Bit one Pin is needed. So for all 32 Positions 5 Pins are needed = 1,2,4,8,16
165 | If this feature is enabled, 32 Hal Pins will be created in LinuxCNC.
166 |
167 | # Status LED
168 | The Arduino only works, if LinuxCNC is running and an USB Connection is established.
169 | To give optical Feedback of the State of the connection a Status LED setting is provided.
170 | This can be either an LED connected to an Output Pin or you can select one LED in your Digital LED Chain.
171 | - It will flash slowly after startup, when it waits for communication setup by LinuxCNC.
172 | - It will glow constantly when everything works.
173 | - it Will flash short when Connection was lost.
174 |
175 | # Matrix Keyboard
176 | Connecting Matrix Keyboards is supported.
177 | You can adapt the Settings to fit all kinds of Matrix Keyboards. The Software can emulate an Keyboard in Linux. This is useful, because for some Keys you may want to enter Letters or Numbers, for others you may want to set functions in LinuxCNC. To input Text it is neccessary to emulate Keypresses.
178 | In the Config file you can define, which Key should be connected to LinuxCNC as Inputpins and which should be handled like a Keyboard in Linux.
179 |
180 | To run Matrix Keyboards requires you to install and test "xdotool".
181 | You can install it by typing "sudo apt install xdotool" in your console. After installing "xdotool type "Hello World" should return "Hello World" in the Terminal.
182 | If it doesn't, something is not working and this program will not work either. Please get xdotool working first.
183 |
184 | In the Settings a cheap 4x4 Keyboard is used such as https://theartoftinkering.com/recommends/matrix-keyboard/ (referral link)
185 |
186 | WaWatch the Video explanation on Youtube:
187 | ch the Video explanation on Youtube:
188 | [](https://www.youtube.com/watch?v=oOhzm7pbvXo&list=PLdrOU2f3sjtApTdxhmAiXL4lET_ZnntGc&index=4 "connect Matrix Keyboards to LinuxCNC using ArduinoC")
189 |
190 |
191 | # Multiplexed LEDs
192 | Special mode for Multiplexed LEDs. This mode is experimental and implemented to support Matrix Keyboards with integrated Key LEDs. Please provide feedback if u use this feature.
193 | check out this thread on LinuxCNC Forum for context. https://forum.linuxcnc.org/show-your-stuff/49606-matrix-keyboard-controlling-linuxcnc
194 | for Each LED an Output Pin is generated in LinuxCNC.
195 |
196 | If your Keyboard shares pins with the LEDs, you have to check polarity. The Matrix Keyboard uses Pins as such:
197 |
198 | rowPins[numRows] = {} are Pullup Inputs
199 | colPins[numCols] = {} are GND Pins
200 |
201 | the matrix keyboard described in the thread shares GND Pins between LEDs and KEYs, therefore LedGndPins[] and colPins[numCols] = {} use same Pins, LedVccPins[] are Outputs and drive the LEDs.
202 |
203 |
204 | # Quadrature Encoders
205 | Quadrature Encoders require a Library to be installed.
206 | More Info about the used Library can be found here: https://www.pjrc.com/teensy/td_libs_Encoder.html
207 | It can be downloaded here: https://www.arduino.cc/reference/en/libraries/encoder/
208 |
209 | This function is made with Rotating encoders in mind but supports all kinds of quadrature Signals.
210 | For easy implementation in LinuxCNC two modes are supported.
211 |
212 | 1 = Up or Down Signals per Impuls , this is intended for use with Feed or Spindle Speed Override.
213 | 2 = Counter Signal, this is intended for the usecase of using the Encoder as MPG for example. Arduino will count Impulses and add them to a counter, which then is send to LinuxCNC.
214 | there you can connect it to x & y yog signals.
215 |
216 | If your Encoder can be pressed and there is a button inside, use the Input or Latching Input functionality mentioned above.
217 |
218 | Encoders have 2 signals, which must be connected to 2 pins. There are three options.
219 |
220 | Best Performance: Both signals connect to interrupt pins.
221 |
222 | Good Performance: First signal connects to an interrupt pin, second to a non-interrupt pin.
223 |
224 | Low Performance: Both signals connect to non-interrupt pins, details below.
225 |
226 | | Board | Interrupt Pins |LED Pin(do not use) |
227 | | ------------- | ------------- |------------- |
228 | |Teensy 4.0 - 4.1 |All Digital Pins |13 |
229 | |Teensy 3.0 - 3.6 |All Digital Pins |13 |
230 | |Teensy LC | 2 - 12, 14, 15, 20 - 23 |13 |
231 | |Teensy 2.0 |5, 6, 7, 8 |11 |
232 | |Teensy 1.0 |0, 1, 2, 3, 4, 6, 7, 16 | |
233 | |Teensy++ 2.0 |0, 1, 2, 3, 18, 19, 36, 37 |6 |
234 | |Teensy++ 1.0 |0, 1, 2, 3, 18, 19, 36, 37 | |
235 | |Arduino Due | All Digital Pins |13 |
236 | |Arduino Uno | 2, 3 |13 |
237 | |Arduino Leonardo |0, 1, 2, 3 |13 |
238 | |Arduino Mega |2, 3, 18, 19, 20, 21 |13 |
239 | |Sanguino |2, 10, 11 |0 |
240 |
241 | Watch the Video explanation on Youtube:
242 | [](https://www.youtube.com/watch?v=hgKXgRvjwPg&list=PLdrOU2f3sjtApTdxhmAiXL4lET_ZnntGc&index=3 "How to connect Rotary Encoders and Joysticks for MPG to LinuxCNC using Arduino")
243 |
244 | # Joysticks
245 | Joysticks use a similar implementation as Quadrature encoders and are implemented with the usecase as MPG in mind.
246 | Connect your X and Y Pin of your Joystick to an Analog Pin of your choice.
247 | Depending of the position of the Joystick it will add or substract from a counter, which then is send to LinuxCNC. The more you move the Joystick from the middle Position to the end of movement the more will be added to the counter, which will increase the speed of motion in Jog mode.
248 |
249 | Currently Joysticks will only generate an counter in LinuxCNC.
250 |
251 | Watch the Video explanation on Youtube:
252 | [](https://youtu.be/hgKXgRvjwPg?si=nVdQgR5Q6rLq4QGQ&t=780 "How to connect Rotary Encoders and Joysticks to LinuxCNC using Arduino")
253 |
254 | # Serial communication over USB
255 | The Send and receive Protocol is :
256 | After Bootup the Arduino will continuously print E0:0 to Serial. Once the Host Python skript runs and connects, it will answer and hence the Arduino knows, the connection is established.
257 |
258 | For testing you can still connect to it with your Serial terminal. Send ```E0:0```, afterwards it will listen to your commands and post Input Changes.
259 |
260 | Data is always only send once, everytime it changes.
261 |
262 | | Signal | Header |direction |Values |
263 | | ------------- | ------------- |------------- |------------- |
264 | | Inputs & Toggle Inputs | I | write only |0,1 |
265 | | Outputs | O | read only |0,1 |
266 | | PWM Outputs | P | read only |0-255 |
267 | | Digital LED Outputs | D | read only |0,1 |
268 | | Analog Inputs | A | write only |0-1024 |
269 | | Latching Potentiometers | L | write only |0-max Position|
270 | | binary encoded Selector | K | write only |0-32 |
271 | | Matrix Keyboard | M | write only |0,1 |
272 | | Quadrature Encoders | R | write only |0,1,counter |
273 | | Joystick | R | write only |counter |
274 | | Connection established | E | read/ write |0:0 |
275 |
276 |
277 | Example:
278 | You want to Tell LinuxCNC you pressed Input on GPIO Pin 2, The command would be : "I2:1".
279 | If LinuxCNC sends the Arduino to Set GPIO Pin 3 HIGH, the command would be: "O3:1" and "O3:0" to set it LOW.
280 |
281 | Command 'E0:0' is used for connectivity checks and is send every 5 seconds as keep alive signal. If it is not received in Time, the connection is lost and the arduino begins flashing an LED to alarm the User. It will however work the same and try to send it's Data to the Host.
282 |
283 | # License
284 | This program is free software; you can redistribute it and/or modify
285 | it under the terms of the GNU General Public License as published by
286 | the Free Software Foundation; either version 2 of the License, or
287 | (at your option) any later version.
288 | This program is distributed in the hope that it will be useful,
289 | but WITHOUT ANY WARRANTY; without even the implied warranty of
290 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
291 | See the GNU General Public License for more details.
292 | You should have received a copy of the GNU General Public License
293 | along with this program; if not, write to the Free Software
294 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
295 |
--------------------------------------------------------------------------------
/arduino-connector.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | import serial, time, hal
3 | # LinuxCNC_ArduinoConnector
4 | # By Alexander Richter, info@theartoftinkering.com 2022
5 |
6 | # This Software is used as IO Expansion for LinuxCNC. Here i am using a Mega 2560.
7 |
8 | # It is NOT intended for timing and security relevant IO's. Don't use it for Emergency Stops or Endstop switches!
9 |
10 | # You can create as many digital & analog Inputs, Outputs and PWM Outputs as your Arduino can handle.
11 | # You can also generate "virtual Pins" by using latching Potentiometers, which are connected to one analog Pin, but are read in Hal as individual Pins.
12 |
13 | # Currently the Software provides:
14 | # - analog Inputss
15 | # - latching Potentiometers
16 | # - 1 binary encoded Selector Switch
17 | # - digital Inputs
18 | # - digital Outputs
19 |
20 | # The Send and receive Protocol is :
21 | # To begin Transmitting Ready is send out and expects to receive E: to establish connection. Afterwards Data is exchanged.
22 | # Data is only send everythime it changes once.
23 |
24 | # Inputs & Toggle Inputs = 'I' -write only -Pin State: 0,1
25 | # Outputs = 'O' -read only -Pin State: 0,1
26 | # PWM Outputs = 'P' -read only -Pin State: 0-255
27 | # Digital LED Outputs = 'D' -read only -Pin State: 0,1
28 | # Analog Inputs = 'A' -write only -Pin State: 0-1024
29 | # Latching Potentiometers = 'L' -write only -Pin State: 0-max Position
30 | # binary encoded Selector = 'K' -write only -Pin State: 0-32
31 | # Matrix Keypad = 'M' -write only -Pin State: 0,1
32 | # Multiplexed LEDs = 'M' -read only -Pin State: 0,1
33 | # Quadrature Encoders = 'R' -write only -Pin State: 0(down),1(up),-2147483648 to 2147483647(counter)
34 | # Joystick Input = 'R' -write only -Pin State: -2147483648 to 2147483647(counter)
35 |
36 |
37 |
38 | # Command 'E0:0' is used for connectivity checks and is send every 5 seconds as keep alive signal
39 |
40 | # This program is free software; you can redistribute it and/or modify
41 | # it under the terms of the GNU General Public License as published by
42 | # the Free Software Foundation; either version 2 of the License, or
43 | # (at your option) any later version.
44 | # This program is distributed in the hope that it will be useful,
45 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
46 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
47 | # See the GNU General Public License for more details.
48 | # You should have received a copy of the GNU General Public License
49 | # along with this program; if not, write to the Free Software
50 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
51 |
52 |
53 | c = hal.component("arduino") #name that we will cal pins from in hal
54 | connection = '/dev/ttyACM0' #this is the port your Arduino is connected to. You can check with ""sudo dmesg | grep tty"" in Terminal
55 |
56 |
57 | # Set how many Inputs you have programmed in Arduino and which pins are Inputs, Set Inputs = 0 to disable
58 | Inputs = 2
59 | InPinmap = [8,9] #Which Pins are Inputs?
60 |
61 | # Set how many Toggled ("sticky") Inputs you have programmed in Arduino and which pins are Toggled Inputs , Set SInputs = 0 to disable
62 | SInputs = 1
63 | sInPinmap = [10] #Which Pins are SInputs?
64 |
65 |
66 | # Set how many Outputs you have programmed in Arduino and which pins are Outputs, Set Outputs = 0 to disable
67 | Outputs = 2 #9 Outputs, Set Outputs = 0 to disable
68 | OutPinmap = [11,12] #Which Pins are Outputs?
69 |
70 | # Set how many PWM Outputs you have programmed in Arduino and which pins are PWM Outputs, you can set as many as your Arduino has PWM pins. List the connected pins below.
71 | PwmOutputs = 0 #number of PwmOutputs, Set PwmOutputs = 0 to disable
72 | PwmOutPinmap = [11,12] #PwmPutput connected to Pin 11 & 12
73 |
74 | # Set how many Analog Inputs you have programmed in Arduino and which pins are Analog Inputs, you can set as many as your Arduino has Analog pins. List the connected pins below.
75 | AInputs = 0 #number of AInputs, Set AInputs = 0 to disable
76 | AInPinmap = [1] #Potentiometer connected to Pin 1 (A0)
77 |
78 |
79 |
80 | # Set how many Latching Analog Inputs you have programmed in Arduino and how many latches there are, you can set as many as your Arduino has Analog pins. List the connected pins below.
81 | LPoti = 0 #number of LPotis, Set LPoti = 0 to disable
82 |
83 | LPotiLatches = [[1,9], #Poti is connected to Pin 1 (A1) and has 9 positions
84 | [2,4]] #Poti is connected to Pin 2 (A2) and has 4 positions
85 |
86 | SetLPotiValue = [1,2] #0 OFF - creates Pin for each Position
87 | #1 S32 - Whole Number between -2147483648 to 2147483647
88 | #2 FLOAT - 32 bit floating point value
89 |
90 | LPotiValues = [[40, 50,60,70,80,90,100,110,120],
91 | [0.001,0.01,0.1,1]]
92 |
93 |
94 |
95 | # Set if you have an binary encoded Selector Switch and how many positions it has (only one supported, as i don't think they are very common and propably nobody uses these anyway)
96 | # Set BinSelKnob = 0 to disable
97 | BinSelKnob = 0 #1 enable
98 | BinSelKnobPos = 32
99 |
100 | #Do you want the Binary Encoded Selector Switches to control override Settings in LinuxCNC? This function lets you define values for each Position.
101 | SetBinSelKnobValue = [[0]] #0 = disable 1= enable
102 | BinSelKnobvalues = [[180,190,200,0,0,0,0,0,0,0,0,0,0,0,0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170]]
103 |
104 | #Enable Quadrature Encoders
105 | QuadEncs = 0
106 | QuadEncSig = [2,2]
107 | #1 = send up or down signal (typical use for selecting modes in hal)
108 | #2 = send position signal (typical use for MPG wheel)
109 |
110 |
111 | #Enable Joystick support.
112 | # Intended for use as MPG. useing the Joystick will update a counter, which can be used as Jog Input.
113 | # Moving the Joystick will either increase or decrease the counter. Modify Jog-scale in hal to increase or decrease speed.
114 | JoySticks = 0 #number of installed Joysticks
115 | JoyStickPins = [0,1] #Pins the Joysticks are connected to.
116 | #in this example X&Y Pins of the Joystick are connected to Pin A0& A1.
117 |
118 |
119 |
120 |
121 | # Set how many Digital LED's you have connected.
122 | DLEDcount = 0
123 |
124 |
125 | # Support For Matrix Keypads. This requires you to install and test "xdotool".
126 | # You can install it by typing "sudo apt install xdotool" in your console. After installing you can test your setup by entering: " xdotool type 'Hello World' " in Terminal.
127 | # It should enter Hello World.
128 | # If it doesn't, something is not working and this program will not work either. Please get xdotool working first.
129 | #
130 | # Assign Values to each Key in the following Settings.
131 | # These Inputs are handled differently from everything else, because thy are send to the Host instead and emulate actual Keyboard input.
132 | # You can specify special Charakters however, which will be handled as Inputs in LinuxCNC. Define those in the LCNC Array below.
133 |
134 |
135 | Keypad = 0 # Set to 1 to Activate
136 | LinuxKeyboardInput = 0 # set to 1 to Activate direct Keyboard integration to Linux.
137 |
138 |
139 | Columns = 4
140 | Rows = 4
141 | Chars = [ #here you must define as many characters as your Keypad has keys. calculate columns * rows . for example 4 *4 = 16. You can write it down like in the example for ease of readability.
142 | "1", "2", "3", "A",
143 | "4", "5", "6", "B",
144 | "7", "8", "9", "C",
145 | "Yay", "0", "#", "D"
146 | ]
147 |
148 | # These are Settings to connect Keystrokes to Linux, you can ignore them if you only use them as LinuxCNC Inputs.
149 |
150 | Destination = [ #define, which Key should be inserted in LinuxCNC as Input or as Keystroke in Linux.
151 | #you can ignore it if you want to use all Keys as LinuxCNC Inputs.
152 | # 0 = LinuxCNC
153 | # 1 = press Key in Linux
154 | # 2 = write Text in Linux
155 | 1, 1, 1, 0,
156 | 1, 1, 1, 0,
157 | 1, 1, 1, 0,
158 | 2, 1, 0, 0
159 | ]
160 | # Background Info:
161 | # The Key press is received as M Number of Key:HIGH/LOW. M2:1 would represent Key 2 beeing Pressed. M2:0 represents letting go of the key.
162 | # Key Numbering is calculated in an 2D Matrix. for a 4x4 Keypad the numbering of the Keys will be like this:
163 | #
164 | # 0, 1, 2, 3,
165 | # 4, 5, 6, 7,
166 | # 8, 9, 10, 11,
167 | # 12, 13, 14, 15
168 | #
169 |
170 | # this is an experimental feature, meant to support MatrixKeyboards with integrated LEDs in each Key but should work with any other LED Matrix too.
171 | # It creates Output Halpins that can be connected to signals in LinuxCNC
172 | MultiplexLED = 0 # Set to 1 to Activate
173 | LedVccPins = 3
174 | LedGndPins = 3
175 |
176 |
177 |
178 | Debug = 0 #only works when this script is run from halrun in Terminal. "halrun","loadusr arduino" now Debug info will be displayed.
179 |
180 | ######## End of Config! ########
181 |
182 |
183 | # global Variables for State Saving
184 |
185 | olddOutStates= [0]*Outputs
186 | oldPwmOutStates=[0]*PwmOutputs
187 | oldDLEDStates=[0]*DLEDcount
188 | oldMledStates = [0]*LedVccPins*LedGndPins
189 |
190 | if LinuxKeyboardInput:
191 | import subprocess
192 |
193 | # Inputs and Toggled Inputs are handled the same.
194 | # For DAU compatiblity we set them up seperately.
195 | # Here we merge the arrays.
196 |
197 | Inputs = Inputs+ SInputs
198 | InPinmap += sInPinmap
199 |
200 |
201 | # Storing Variables for counter timing Stuff
202 | counter_last_update = {}
203 | min_update_interval = 100
204 | ######## SetUp of HalPins ########
205 |
206 | # setup Input halpins
207 | for port in range(Inputs):
208 | c.newpin("din.{}".format(InPinmap[port]), hal.HAL_BIT, hal.HAL_OUT)
209 | c.newparam("din.{}-invert".format(InPinmap[port]), hal.HAL_BIT, hal.HAL_RW)
210 |
211 | # setup Output halpins
212 | for port in range(Outputs):
213 | c.newpin("dout.{}".format(OutPinmap[port]), hal.HAL_BIT, hal.HAL_IN)
214 | olddOutStates[port] = 0
215 |
216 | # setup Pwm Output halpins
217 | for port in range(PwmOutputs):
218 | c.newpin("pwmout.{}".format(PwmOutPinmap[port]), hal.HAL_FLOAT, hal.HAL_IN)
219 | oldPwmOutStates[port] = 255
220 | # setup Analog Input halpins
221 | for port in range(AInputs):
222 | c.newpin("ain.{}".format(AInPinmap[port]), hal.HAL_FLOAT, hal.HAL_OUT)
223 | # setup Latching Poti halpins
224 | for Poti in range(LPoti):
225 | if SetLPotiValue[Poti] == 0:
226 | for Pin in range(LPotiLatches[Poti][1]):
227 | c.newpin("lpoti.{}.{}" .format(LPotiLatches[Poti][0],Pin), hal.HAL_BIT, hal.HAL_OUT)
228 | if SetLPotiValue[Poti] == 1:
229 | c.newpin("lpoti.{}.out" .format(LPotiLatches[Poti][0]), hal.HAL_S32, hal.HAL_OUT)
230 | if SetLPotiValue[Poti] == 2:
231 | c.newpin("lpoti.{}.out" .format(LPotiLatches[Poti][0]), hal.HAL_FLOAT, hal.HAL_OUT)
232 |
233 | # setup Absolute Encoder Knob halpins
234 | if BinSelKnob:
235 | if SetBinSelKnobValue[0] == 0:
236 | for port in range(BinSelKnobPos):
237 | c.newpin("binselknob.0.{}".format(port), hal.HAL_BIT, hal.HAL_OUT)
238 | else :
239 | c.newpin("binselknob.{}.{}" .format("0","out"), hal.HAL_S32, hal.HAL_OUT)
240 |
241 |
242 | # setup Digital LED halpins
243 | if DLEDcount > 0:
244 | for port in range(DLEDcount):
245 | c.newpin("dled.{}".format(port), hal.HAL_BIT, hal.HAL_IN)
246 | oldDLEDStates[port] = 0
247 |
248 | # setup MatrixKeyboard halpins
249 | if Keypad > 0:
250 | for port in range(Columns*Rows):
251 | if Destination[port] == 0 & LinuxKeyboardInput:
252 | c.newpin("keypad.{}".format(Chars[port]), hal.HAL_BIT, hal.HAL_OUT)
253 |
254 | # setup MultiplexLED halpins
255 | if MultiplexLED > 0:
256 | for port in range(LedVccPins*LedGndPins):
257 | c.newpin("mled.{}".format(port), hal.HAL_BIT, hal.HAL_IN)
258 |
259 |
260 | #setup JoyStick Pins
261 | if JoySticks > 0:
262 | for port in range(JoySticks*2):
263 | c.newpin("counter.{}".format(JoyStickPins[port]), hal.HAL_S32, hal.HAL_OUT)
264 |
265 |
266 | if QuadEncs > 0:
267 | for port in range(QuadEncs):
268 | if QuadEncSig[port] == 1:
269 | c.newpin("counterup.{}".format(port), hal.HAL_BIT, hal.HAL_OUT)
270 | c.newpin("counterdown.{}".format(port), hal.HAL_BIT, hal.HAL_OUT)
271 | if QuadEncSig[port] == 2:
272 | c.newpin("counter.{}".format(port), hal.HAL_S32, hal.HAL_OUT)
273 |
274 |
275 |
276 | c.ready()
277 |
278 | #setup Serial connection
279 | arduino = serial.Serial(connection, 115200, timeout=1, xonxoff=False, rtscts=False, dsrdtr=True)
280 | ######## GlobalVariables ########
281 | firstcom = 0
282 | event = time.time()
283 | timeout = 9 #send something after max 9 seconds
284 |
285 |
286 | ######## Functions ########
287 |
288 | def keepAlive(event):
289 | return event + timeout < time.time()
290 |
291 | def readinput(input_str):
292 | for i in range(50):
293 |
294 | if input_str:
295 | string = input_str.decode() # convert the byte string to a unicode string
296 | print (string)
297 | num = int(string) # convert the unicode string to an int
298 | return num
299 |
300 |
301 | def extract_nbr(input_str):
302 | if input_str is None or input_str == '':
303 | return 0
304 |
305 | out_number = ''
306 | for i, ele in enumerate(input_str):
307 | if ele.isdigit() or (ele == '-' and i+1 < len(input_str) and input_str[i+1].isdigit()):
308 | out_number += ele
309 | return int(out_number)
310 |
311 | def managageOutputs():
312 | for port in range(PwmOutputs):
313 | State = int(c["pwmout.{}".format(PwmOutPinmap[port])])
314 | if oldPwmOutStates[port] != State: #check if states have changed
315 | Sig = 'P'
316 | Pin = int(PwmOutPinmap[port])
317 | command = "{}{}:{}\n".format(Sig,Pin,State)
318 | arduino.write(command.encode())
319 | if (Debug):print ("Sending:{}".format(command.encode()))
320 | oldPwmOutStates[port]= State
321 | time.sleep(0.01)
322 |
323 | for port in range(Outputs):
324 | State = int(c["dout.{}".format(OutPinmap[port])])
325 | if olddOutStates[port] != State: #check if states have changed
326 | Sig = 'O'
327 | Pin = int(OutPinmap[port])
328 | command = "{}{}:{}\n".format(Sig,Pin,State)
329 | arduino.write(command.encode())
330 | if (Debug):print ("Sending:{}".format(command.encode()))
331 | olddOutStates[port]= State
332 | time.sleep(0.01)
333 |
334 | for dled in range(DLEDcount):
335 | State = int(c["dled.{}".format(dled)])
336 | if oldDLEDStates[dled] != State: #check if states have changed
337 | Sig = 'D'
338 | Pin = dled
339 | command = "{}{}:{}\n".format(Sig,Pin,State)
340 | arduino.write(command.encode())
341 | if (Debug):print ("Sending:{}".format(command.encode()))
342 | oldDLEDStates[dled] = State
343 | time.sleep(0.01)
344 | if MultiplexLED > 0:
345 | for mled in range(LedVccPins*LedGndPins):
346 | State = int(c["mled.{}".format(mled)])
347 | if oldMledStates[mled] != State: #check if states have changed
348 | Sig = 'M'
349 | Pin = mled
350 | command = "{}{}:{}\n".format(Sig,Pin,State)
351 | arduino.write(command.encode())
352 | if (Debug):print ("Sending:{}".format(command.encode()))
353 | oldMledStates[mled] = State
354 | time.sleep(0.01)
355 |
356 |
357 | while True:
358 | try:
359 | data = arduino.readline().decode('utf-8') #read Data received from Arduino and decode it
360 | if (Debug):print ("I received:{}".format(data))
361 | data = data.split(":",1)
362 |
363 | try:
364 | cmd = data[0][0]
365 | if cmd == "":
366 | if (Debug):print ("No Command!:{}".format(cmd))
367 |
368 | else:
369 | if not data[0][1]:
370 | io = 0
371 | else:
372 | io = extract_nbr(data[0])
373 | value = extract_nbr(data[1])
374 | #if value<0: value = 0
375 | if (Debug):print ("No Command!:{}.".format(cmd))
376 |
377 | if cmd == "I":
378 | firstcom = 1
379 | if value == 1:
380 | if c["din.{}-invert".format(io)] == 0:
381 | c["din.{}".format(io)] = 1
382 | if(Debug):print("din{}:{}".format(io,1))
383 | else:
384 | c["din.{}".format(io)] = 0
385 | if(Debug):print("din{}:{}".format(io,0))
386 |
387 |
388 | if value == 0:
389 | if c["din.{}-invert".format(io)] == 0:
390 | c["din.{}".format(io)] = 0
391 | if(Debug):print("din{}:{}".format(io,0))
392 | else:
393 | c["din.{}".format(io)] = 1
394 | if(Debug):print("din{}:{}".format(io,1))
395 | else:pass
396 |
397 |
398 | elif cmd == "A":
399 | firstcom = 1
400 | c["ain.{}".format(io)] = value
401 | if (Debug):print("ain.{}:{}".format(io,value))
402 |
403 | elif cmd == "L":
404 | firstcom = 1
405 | for Poti in range(LPoti):
406 | if LPotiLatches[Poti][0] == io and SetLPotiValue[Poti] == 0:
407 | for Pin in range(LPotiLatches[Poti][1]):
408 | if Pin == value:
409 | c["lpoti.{}.{}" .format(io,Pin)] = 1
410 | if(Debug):print("lpoti.{}.{} =1".format(io,Pin))
411 | else:
412 | c["lpoti.{}.{}" .format(io,Pin)] = 0
413 | if(Debug):print("lpoti.{}.{} =0".format(io,Pin))
414 |
415 | if LPotiLatches[Poti][0] == io and SetLPotiValue[Poti] >= 1:
416 | c["lpoti.{}.out" .format(io)] = LPotiValues[Poti][value]
417 | if(Debug):print("lpoti.{}.out = {}".format(io,LPotiValues[Poti][value]))
418 |
419 | elif cmd == "K":
420 | firstcom = 1
421 | if SetBinSelKnobValue[0] == 0:
422 | for port in range(BinSelKnobPos):
423 | if port == value:
424 | c["binselknob.{}".format(port)] = 1
425 | if(Debug):print("binselknob.{}:{}".format(port,1))
426 | else:
427 | c["binselknob.{}".format(port)] = 0
428 | if(Debug):print("binselknob.{}:{}".format(port,0))
429 | else:
430 | c["binselknob.{}.{}" .format(0,"out")] = BinSelKnobvalues[0][value]
431 |
432 | elif cmd == "M":
433 | firstcom = 1
434 | if value == 1:
435 | if Destination[io] == 1 and LinuxKeyboardInput == 1:
436 | subprocess.call(["xdotool", "key", Chars[io]])
437 | if(Debug):print("Emulating Keypress{}".format(Chars[io]))
438 | if Destination[io] == 2 and LinuxKeyboardInput == 1:
439 | subprocess.call(["xdotool", "type", Chars[io]])
440 | if(Debug):print("Emulating Keypress{}".format(Chars[io]))
441 |
442 | else:
443 | c["keypad.{}".format(Chars[io])] = 1
444 | if(Debug):print("keypad{}:{}".format(Chars[io],1))
445 |
446 | if value == 0 & Destination[io] == 0:
447 | c["keypad.{}".format(Chars[io])] = 0
448 | if(Debug):print("keypad{}:{}".format(Chars[io],0))
449 |
450 |
451 | elif cmd == "R":
452 | firstcom = 1
453 | if JoySticks > 0:
454 | for pins in range(JoySticks*2):
455 | if (io == JoyStickPins[pins]):
456 | c["counter.{}".format(io)] = value
457 | if (Debug):print("counter.{}:{}".format(io,value))
458 | if QuadEncs > 0:
459 | if QuadEncSig[io]== 1:
460 | if value == 0:
461 | c["counterdown.{}".format(io)] = 1
462 | time.sleep(0.001)
463 | c["counterdown.{}".format(io)] = 0
464 | time.sleep(0.001)
465 | if value == 1:
466 | c["counterup.{}".format(io)] = 1
467 | time.sleep(0.001)
468 | c["counterup.{}".format(io)] = 0
469 | time.sleep(0.001)
470 | if QuadEncSig[io]== 2:
471 | c["counter.{}".format(io)] = value
472 |
473 | elif cmd == 'E':
474 | arduino.write(b"E0:0\n")
475 | if (Debug):print("Sending E0:0 to establish contact")
476 | else: pass
477 |
478 |
479 | except: pass
480 |
481 |
482 | except KeyboardInterrupt:
483 | if (Debug):print ("Keyboard Interrupted.. BYE")
484 | exit()
485 | except:
486 | if (Debug):print ("I received garbage")
487 | arduino.flush()
488 |
489 | if firstcom == 1: managageOutputs() #if ==1: E0:0 has been exchanged, which means Arduino knows that LinuxCNC is running and starts sending and receiving Data
490 |
491 | if keepAlive(event): #keep com alive. This is send to help Arduino detect connection loss.
492 | arduino.write(b"E:\n")
493 | if (Debug):print("keepAlive")
494 | event = time.time()
495 |
496 | time.sleep(0.001)
497 |
498 |
--------------------------------------------------------------------------------