2023-05-02 21:35:57 +02:00
#!/usr/bin/python3.9
2022-11-25 23:58:26 +01:00
import serial , time , hal
2022-11-25 02:43:59 +01:00
# LinuxCNC_ArduinoConnector
# By Alexander Richter, info@theartoftinkering.com 2022
# This Software is used as IO Expansion for LinuxCNC. Here i am using a Mega 2560.
# It is NOT intended for timing and security relevant IO's. Don't use it for Emergency Stops or Endstop switches!
# You can create as many digital & analog Inputs, Outputs and PWM Outputs as your Arduino can handle.
# 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.
# Currently the Software provides:
2023-07-23 18:46:50 +02:00
# - analog Inputss
2022-11-25 02:43:59 +01:00
# - latching Potentiometers
2023-05-02 19:16:06 +02:00
# - 1 binary encoded Selector Switch
2022-11-25 02:43:59 +01:00
# - digital Inputs
# - digital Outputs
# The Send and receive Protocol is <Signal><PinNumber>:<Pin State>
# 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.
2023-03-28 18:54:54 +02:00
# Inputs & Toggle Inputs = 'I' -write only -Pin State: 0,1
2023-07-23 17:41:21 +02:00
# 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
# Analog Inputs = 'A' -write only -Pin State: 0-1024
2022-11-25 02:43:59 +01:00
# Latching Potentiometers = 'L' -write only -Pin State: 0-max Position
2023-05-02 19:16:06 +02:00
# binary encoded Selector = 'K' -write only -Pin State: 0-32
2023-05-02 12:30:53 +02:00
# Matrix Keypad = 'M' -write only -Pin State: 0,1
2023-07-23 17:41:21 +02:00
# Quadrature Encoders = 'R' -write only -Pin State: 0(down),1(up),-2147483648 to 2147483647(counter)
# Joystick Input = 'R' -write only -Pin State: -2147483648 to 2147483647(counter)
2022-11-25 02:43:59 +01:00
# Command 'E0:0' is used for connectivity checks and is send every 5 seconds as keep alive signal
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2022-11-21 12:53:33 +01:00
2023-03-30 23:37:00 +02:00
c = hal . component ( " arduino " ) #name that we will cal pins from in hal
connection = ' /dev/ttyACM0 ' #this is the port your Arduino is connected to. You can check with ""sudo dmesg | grep tty"" in Terminal
2022-11-25 02:43:59 +01:00
2022-11-21 12:53:33 +01:00
2023-03-30 23:37:00 +02:00
# Set how many Inputs you have programmed in Arduino and which pins are Inputs, Set Inputs = 0 to disable
2023-07-23 10:43:43 +02:00
Inputs = 0
2023-03-28 18:50:22 +02:00
InPinmap = [ 37 , 38 , 39 , 40 , 41 ]
2022-11-21 12:53:33 +01:00
2023-03-30 23:37:00 +02:00
# Set how many Toggled ("sticky") Inputs you have programmed in Arduino and which pins are Toggled Inputs , Set SInputs = 0 to disable
2023-07-23 10:43:43 +02:00
SInputs = 0
2023-03-28 18:50:22 +02:00
sInPinmap = [ 32 , 33 , 34 , 35 , 36 ]
2022-11-21 12:53:33 +01:00
2022-11-21 19:37:43 +01:00
2023-03-30 23:37:00 +02:00
# Set how many Outputs you have programmed in Arduino and which pins are Outputs, Set Outputs = 0 to disable
2023-07-23 10:43:43 +02:00
Outputs = 0 #9 Outputs, Set Outputs = 0 to disable
2023-03-30 23:37:00 +02:00
OutPinmap = [ 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 21 ]
2022-11-21 19:37:43 +01:00
2023-03-30 23:37:00 +02:00
# 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.
2023-05-02 17:49:15 +02:00
PwmOutputs = 0 #number of PwmOutputs, Set PwmOutputs = 0 to disable
2023-03-30 23:37:00 +02:00
PwmOutPinmap = [ 11 , 12 ] #PwmPutput connected to Pin 11 & 12
2022-11-21 19:37:43 +01:00
2023-03-30 23:37:00 +02:00
# 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.
2023-05-02 17:49:15 +02:00
AInputs = 0 #number of AInputs, Set AInputs = 0 to disable
2023-03-30 23:37:00 +02:00
AInPinmap = [ 1 ] #Potentiometer connected to Pin 1 (A0)
2022-11-21 19:37:43 +01:00
2023-03-30 23:37:00 +02:00
# 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.
2023-05-02 17:49:15 +02:00
LPoti = 0 #number of LPotis, Set LPoti = 0 to disable
2023-08-14 12:54:41 +02:00
LPotiLatches = [ [ 1 , 9 ] , #Poti is connected to Pin 1 (A1) and has 9 positions
[ 2 , 4 ] ] #Poti is connected to Pin 2 (A2) and has 4 positions
2022-11-21 19:37:43 +01:00
2023-05-07 11:39:32 +02:00
#Do you want the Latching Potis to control override Settings in LinuxCNC? This function lets you define values for each Position.
2023-08-14 12:54:41 +02:00
SetLPotiValue = [ 0 , 0 ] #0 = disable 1= enable
2023-05-07 11:39:32 +02:00
LPotiValues = [ [ 40 , 50 , 60 , 70 , 80 , 90 , 100 , 110 , 120 ] ,
[ 0.001 , 0.01 , 0.1 , 1 ] ]
2023-05-02 19:16:06 +02:00
# 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)
# Set BinSelKnob = 0 to disable
BinSelKnob = 0 #1 enable
BinSelKnobPos = 32
2022-11-21 19:37:43 +01:00
2023-05-07 17:39:14 +02:00
#Do you want the Binary Encoded Selector Switches to control override Settings in LinuxCNC? This function lets you define values for each Position.
2023-08-14 12:54:41 +02:00
SetBinSelKnobValue = [ 0 ] #0 = disable 1= enable
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 ] ]
2023-07-23 10:43:43 +02:00
2023-07-23 16:44:40 +02:00
#Enable Quadrature Encoders
2023-07-31 13:33:01 +02:00
QuadEncs = 0
2023-07-23 17:29:17 +02:00
QuadEncSig = [ 2 , 2 ]
2023-07-23 16:44:40 +02:00
#1 = send up or down signal (typical use for selecting modes in hal)
#2 = send position signal (typical use for MPG wheel)
2023-07-23 10:43:43 +02:00
2023-07-23 14:17:07 +02:00
#Enable Joystick support.
# Intended for use as MPG. useing the Joystick will update a counter, which can be used as Jog Input.
# Moving the Joystick will either increase or decrease the counter. Modify Jog-scale in hal to increase or decrease speed.
2023-07-31 13:33:01 +02:00
JoySticks = 0 #number of installed Joysticks
2023-08-14 12:54:41 +02:00
JoyStickPins = [ 0 , 1 ] #Pins the Joysticks are connected to.
#in this example X&Y Pins of the Joystick are connected to Pin A0& A1.
2023-07-23 10:43:43 +02:00
2023-03-25 14:38:12 +01:00
# Set how many Digital LED's you have connected.
2023-05-02 17:49:15 +02:00
DLEDcount = 0
2022-11-25 23:58:26 +01:00
2023-05-02 17:49:15 +02:00
# Support For Matrix Keypads. This requires you to install and test "xdotool".
2023-07-31 13:33:01 +02:00
# 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.
# It should enter Hello World.
2023-05-02 17:49:15 +02:00
# If it doesn't, something is not working and this program will not work either. Please get xdotool working first.
2023-03-28 20:27:27 +02:00
#
# Assign Values to each Key in the following Settings.
# These Inputs are handled differently from everything else, because thy are send to the Host instead and emulate actual Keyboard input.
# You can specify special Charakters however, which will be handled as Inputs in LinuxCNC. Define those in the LCNC Array below.
2023-05-02 12:30:53 +02:00
2023-07-31 13:33:01 +02:00
Keypad = 0 # Set to 1 to Activate
LinuxKeyboardInput = 1 #Activate direct Keyboard integration to Linux.
2023-08-14 12:54:41 +02:00
2023-07-31 13:33:01 +02:00
Columns = 4
Rows = 4
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.
2023-05-02 12:30:53 +02:00
" 1 " , " 2 " , " 3 " , " A " ,
" 4 " , " 5 " , " 6 " , " B " ,
" 7 " , " 8 " , " 9 " , " C " ,
2023-07-31 13:33:01 +02:00
" Yay " , " 0 " , " # " , " D "
]
2023-05-02 12:30:53 +02:00
# These are Settings to connect Keystrokes to Linux, you can ignore them if you only use them as LinuxCNC Inputs.
2023-07-31 13:33:01 +02:00
Destination = [ #define, which Key should be inserted in LinuxCNC as Input or as Keystroke in Linux.
#you can ignore it if you want to use all Keys as LinuxCNC Inputs.
# 0 = LinuxCNC
# 1 = press Key in Linux
# 2 = write Text in Linux
1 , 1 , 1 , 0 ,
1 , 1 , 1 , 0 ,
1 , 1 , 1 , 0 ,
2 , 1 , 0 , 0
2023-03-28 20:27:27 +02:00
]
2023-07-31 13:33:01 +02:00
# Background Info:
# 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.
# Key Numbering is calculated in an 2D Matrix. for a 4x4 Keypad the numbering of the Keys will be like this:
#
# 0, 1, 2, 3,
# 4, 5, 6, 7,
# 8, 9, 10, 11,
# 12, 13, 14, 15
#
2023-05-07 17:39:14 +02:00
2023-03-30 23:37:00 +02:00
Debug = 0 #only works when this script is run from halrun in Terminal. "halrun","loadusr arduino" now Debug info will be displayed.
2022-11-21 19:37:43 +01:00
######## End of Config! ########
2023-08-14 12:54:41 +02:00
2022-11-25 23:58:26 +01:00
olddOutStates = [ 0 ] * Outputs
oldPwmOutStates = [ 0 ] * PwmOutputs
2023-05-07 11:20:04 +02:00
oldDLEDStates = [ 0 ] * DLEDcount
2022-11-25 02:43:59 +01:00
2023-03-28 20:27:27 +02:00
2023-05-02 12:30:53 +02:00
if LinuxKeyboardInput :
import subprocess
2023-03-28 20:27:27 +02:00
2023-03-28 18:50:22 +02:00
# 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
2023-07-23 16:44:40 +02:00
# Storing Variables for counter timing Stuff
counter_last_update = { }
2023-07-23 17:06:16 +02:00
min_update_interval = 100
2022-11-21 19:37:43 +01:00
######## SetUp of HalPins ########
# setup Input halpins
for port in range ( Inputs ) :
2023-07-23 17:36:32 +02:00
c . newpin ( " din. {} " . format ( InPinmap [ port ] ) , hal . HAL_BIT , hal . HAL_OUT )
2023-08-14 12:54:41 +02:00
c . newpin ( " din. {} -invert " . format ( InPinmap [ port ] ) , hal . HAL_BIT , hal . HAL_OUT )
2022-11-21 19:37:43 +01:00
# setup Output halpins
for port in range ( Outputs ) :
2023-07-23 17:36:32 +02:00
c . newpin ( " dout. {} " . format ( OutPinmap [ port ] ) , hal . HAL_BIT , hal . HAL_IN )
2023-07-23 16:44:40 +02:00
olddOutStates [ port ] = 0
2022-11-21 19:37:43 +01:00
# setup Pwm Output halpins
for port in range ( PwmOutputs ) :
2023-07-23 17:36:32 +02:00
c . newpin ( " pwmout. {} " . format ( PwmOutPinmap [ port ] ) , hal . HAL_FLOAT , hal . HAL_IN )
2023-07-23 16:44:40 +02:00
oldPwmOutStates [ port ] = 255
2022-11-21 19:37:43 +01:00
# setup Analog Input halpins
for port in range ( AInputs ) :
2023-07-23 17:36:32 +02:00
c . newpin ( " ain. {} " . format ( AInPinmap [ port ] ) , hal . HAL_FLOAT , hal . HAL_OUT )
2022-11-21 19:37:43 +01:00
# setup Latching Poti halpins
2022-11-25 22:05:54 +01:00
for Poti in range ( LPoti ) :
2023-05-07 11:39:32 +02:00
if SetLPotiValue [ port ] == 0 :
for Pin in range ( LPotiLatches [ Poti ] [ 1 ] ) :
2023-07-23 17:32:58 +02:00
c . newpin ( " lpoti. {} . {} " . format ( LPotiLatches [ Poti ] [ 0 ] , Pin ) , hal . HAL_BIT , hal . HAL_OUT )
2023-05-07 11:39:32 +02:00
else :
2023-07-23 17:32:58 +02:00
c . newpin ( " lpoti. {} . {} " . format ( LPotiLatches [ Poti ] [ 0 ] , " out " ) , hal . HAL_S32 , hal . HAL_OUT )
2022-11-21 19:37:43 +01:00
# setup Absolute Encoder Knob halpins
2023-05-02 19:16:06 +02:00
if BinSelKnob :
2023-05-07 17:39:14 +02:00
if SetBinSelKnobValue == 0 :
for port in range ( BinSelKnobPos ) :
2023-07-23 17:32:58 +02:00
c . newpin ( " binselknob.0. {} " . format ( port ) , hal . HAL_BIT , hal . HAL_OUT )
2023-05-07 17:39:14 +02:00
else :
2023-07-23 17:32:58 +02:00
c . newpin ( " binselknob. {} . {} " . format ( " 0 " , " out " ) , hal . HAL_S32 , hal . HAL_OUT )
2023-05-07 17:39:14 +02:00
2022-11-21 12:53:33 +01:00
2023-03-25 14:38:12 +01:00
# setup Digital LED halpins
if DLEDcount > 0 :
for port in range ( DLEDcount ) :
2023-07-23 17:32:58 +02:00
c . newpin ( " dled. {} " . format ( port ) , hal . HAL_BIT , hal . HAL_IN )
2023-05-07 11:20:04 +02:00
oldDLEDStates [ port ] = 0
2023-07-31 13:33:01 +02:00
2023-05-02 12:30:53 +02:00
# setup MatrixKeyboard halpins
2023-05-02 16:20:24 +02:00
if Keypad > 0 :
2023-07-31 13:33:01 +02:00
for port in range ( Columns * Rows ) :
if Destination [ port ] == 0 & LinuxKeyboardInput :
c . newpin ( " keypad. {} " . format ( Chars [ port ] ) , hal . HAL_BIT , hal . HAL_IN )
2023-07-23 10:43:43 +02:00
#setup JoyStick Pins
if JoySticks > 0 :
2023-07-23 14:17:07 +02:00
for port in range ( JoySticks * 2 ) :
2023-07-23 17:41:21 +02:00
c . newpin ( " counter. {} " . format ( JoyStickPins [ port ] ) , hal . HAL_S32 , hal . HAL_OUT )
2023-07-23 16:44:40 +02:00
if QuadEncs > 0 :
for port in range ( QuadEncs ) :
if QuadEncSig [ port ] == 1 :
2023-07-23 17:41:21 +02:00
c . newpin ( " counterup. {} " . format ( port ) , hal . HAL_BIT , hal . HAL_OUT )
c . newpin ( " counterdown. {} " . format ( port ) , hal . HAL_BIT , hal . HAL_OUT )
2023-07-23 16:44:40 +02:00
if QuadEncSig [ port ] == 2 :
2023-07-23 17:41:21 +02:00
c . newpin ( " counter. {} " . format ( port ) , hal . HAL_S32 , hal . HAL_OUT )
2023-07-23 16:44:40 +02:00
2022-11-21 12:53:33 +01:00
c . ready ( )
2022-11-25 22:05:54 +01:00
#setup Serial connection
arduino = serial . Serial ( connection , 115200 , timeout = 1 , xonxoff = False , rtscts = False , dsrdtr = True )
######## GlobalVariables ########
firstcom = 0
event = time . time ( )
timeout = 9 #send something after max 9 seconds
2023-03-28 18:50:22 +02:00
2022-11-25 00:38:01 +01:00
######## Functions ########
2022-11-21 12:53:33 +01:00
2022-11-25 22:05:54 +01:00
def keepAlive ( event ) :
2023-07-23 14:17:07 +02:00
return event + timeout < time . time ( )
2022-11-25 22:05:54 +01:00
2022-11-21 12:53:33 +01:00
def readinput ( input_str ) :
for i in range ( 50 ) :
if input_str :
string = input_str . decode ( ) # convert the byte string to a unicode string
print ( string )
num = int ( string ) # convert the unicode string to an int
return num
def extract_nbr ( input_str ) :
2023-07-23 14:17:07 +02:00
if input_str is None or input_str == ' ' :
return 0
2022-11-21 12:53:33 +01:00
2023-07-23 14:17:07 +02:00
out_number = ' '
for i , ele in enumerate ( input_str ) :
if ele . isdigit ( ) or ( ele == ' - ' and i + 1 < len ( input_str ) and input_str [ i + 1 ] . isdigit ( ) ) :
out_number + = ele
return int ( out_number )
2022-11-21 12:53:33 +01:00
2022-11-25 23:58:26 +01:00
def managageOutputs ( ) :
2022-11-25 22:05:54 +01:00
for port in range ( PwmOutputs ) :
2023-07-23 17:32:58 +02:00
State = int ( c [ " pwmout. {} " . format ( PwmOutPinmap [ port ] ) ] )
2022-11-25 23:58:26 +01:00
if oldPwmOutStates [ port ] != State : #check if states have changed
Sig = ' P '
Pin = int ( PwmOutPinmap [ port ] )
command = " {} {} : {} \n " . format ( Sig , Pin , State )
arduino . write ( command . encode ( ) )
if ( Debug ) : print ( " Sending: {} " . format ( command . encode ( ) ) )
oldPwmOutStates [ port ] = State
for port in range ( Outputs ) :
2023-07-23 17:32:58 +02:00
State = int ( c [ " dout. {} " . format ( OutPinmap [ port ] ) ] )
2022-11-25 23:58:26 +01:00
if olddOutStates [ port ] != State : #check if states have changed
Sig = ' O '
Pin = int ( OutPinmap [ port ] )
command = " {} {} : {} \n " . format ( Sig , Pin , State )
arduino . write ( command . encode ( ) )
if ( Debug ) : print ( " Sending: {} " . format ( command . encode ( ) ) )
olddOutStates [ port ] = State
2022-11-25 22:05:54 +01:00
2023-03-25 14:38:12 +01:00
for port in range ( DLEDcount ) :
2023-07-23 17:32:58 +02:00
State = int ( c [ " dled. {} " . format ( port ) ] )
2023-05-07 11:20:04 +02:00
if oldDLEDStates [ port ] != State : #check if states have changed
Sig = ' D '
Pin = int ( port )
command = " {} {} : {} \n " . format ( Sig , Pin , State )
arduino . write ( command . encode ( ) )
oldDLEDStates [ port ] = State
if ( Debug ) : print ( " Sending: {} " . format ( command . encode ( ) ) )
2022-11-21 12:53:33 +01:00
while True :
2022-11-25 22:05:54 +01:00
try :
2023-03-30 23:37:00 +02:00
data = arduino . readline ( ) . decode ( ' utf-8 ' ) #read Data received from Arduino and decode it
2022-11-25 22:05:54 +01:00
if ( Debug ) : print ( " I received: {} " . format ( data ) )
2022-11-21 12:53:33 +01:00
data = data . split ( " : " , 1 )
2022-11-25 22:05:54 +01:00
try :
2022-11-25 00:38:01 +01:00
cmd = data [ 0 ] [ 0 ]
2022-11-25 22:05:54 +01:00
if cmd == " " :
2023-07-23 14:17:07 +02:00
if ( Debug ) : print ( " No Command!: {} " . format ( cmd ) )
2022-11-25 22:05:54 +01:00
else :
if not data [ 0 ] [ 1 ] :
io = 0
else :
io = extract_nbr ( data [ 0 ] )
value = extract_nbr ( data [ 1 ] )
2023-07-23 14:17:07 +02:00
#if value<0: value = 0
if ( Debug ) : print ( " No Command!: {} . " . format ( cmd ) )
2022-11-25 22:05:54 +01:00
if cmd == " I " :
2022-11-25 23:58:26 +01:00
firstcom = 1
2022-11-25 22:05:54 +01:00
if value == 1 :
2023-08-14 13:10:11 +02:00
if c [ " din. {} -invert " . format ( io ) ] == 0 :
c [ " din. {} " . format ( io ) ] = 1
if ( Debug ) : print ( " din {} : {} " . format ( io , 1 ) )
else :
c [ " din. {} " . format ( io ) ] = 0
if ( Debug ) : print ( " din {} : {} " . format ( io , 0 ) )
2022-11-25 23:58:26 +01:00
2022-11-25 22:05:54 +01:00
if value == 0 :
2023-08-14 13:10:11 +02:00
if c [ " din. {} -invert " . format ( io ) ] == 0 :
c [ " din. {} " . format ( io ) ] = 0
if ( Debug ) : print ( " din {} : {} " . format ( io , 0 ) )
else :
c [ " din. {} " . format ( io ) ] = 1
if ( Debug ) : print ( " din {} : {} " . format ( io , 1 ) )
2022-11-25 22:05:54 +01:00
else : pass
2023-08-14 13:10:11 +02:00
2022-11-25 22:05:54 +01:00
elif cmd == " A " :
2022-11-25 23:58:26 +01:00
firstcom = 1
2023-07-23 17:32:58 +02:00
c [ " ain. {} " . format ( io ) ] = value
if ( Debug ) : print ( " ain. {} : {} " . format ( io , value ) )
2022-11-26 01:15:46 +01:00
2022-11-25 22:05:54 +01:00
elif cmd == " L " :
2022-11-25 23:58:26 +01:00
firstcom = 1
2022-11-26 01:15:46 +01:00
for Poti in range ( LPoti ) :
2023-05-07 11:39:32 +02:00
if LPotiLatches [ Poti ] [ 0 ] == io and SetLPotiValue [ Poti ] == 0 :
2022-11-26 01:15:46 +01:00
for Pin in range ( LPotiLatches [ Poti ] [ 1 ] ) :
if Pin == value :
2023-07-23 17:32:58 +02:00
c [ " lpoti. {} . {} " . format ( io , Pin ) ] = 1
if ( Debug ) : print ( " lpoti. {} . {} =1 " . format ( io , Pin ) )
2022-11-26 01:15:46 +01:00
else :
2023-07-23 17:32:58 +02:00
c [ " lpoti. {} . {} " . format ( io , Pin ) ] = 0
if ( Debug ) : print ( " lpoti. {} . {} =0 " . format ( io , Pin ) )
2023-05-07 11:39:32 +02:00
if LPotiLatches [ Poti ] [ 0 ] == io and SetLPotiValue [ Poti ] == 1 :
2023-07-23 17:32:58 +02:00
c [ " lpoti. {} . {} " . format ( io , " out " ) ] = LPotiValues [ Poti ] [ value ]
if ( Debug ) : print ( " lpoti. {} . {} = 0 " . format ( " out " , LPotiValues [ Poti ] [ value ] ) )
2022-11-26 01:15:46 +01:00
2022-11-25 22:05:54 +01:00
elif cmd == " K " :
2022-11-25 23:58:26 +01:00
firstcom = 1
2023-05-07 17:39:14 +02:00
if SetBinSelKnobValue == 0 :
for port in range ( BinSelKnobPos ) :
if port == value :
2023-07-23 17:32:58 +02:00
c [ " binselknob. {} " . format ( port ) ] = 1
if ( Debug ) : print ( " binselknob. {} : {} " . format ( port , 1 ) )
2023-05-07 17:39:14 +02:00
else :
2023-07-23 17:32:58 +02:00
c [ " binselknob. {} " . format ( port ) ] = 0
if ( Debug ) : print ( " binselknob. {} : {} " . format ( port , 0 ) )
2023-05-07 17:39:14 +02:00
else :
2023-07-23 17:32:58 +02:00
c [ " binselknob. {} . {} " . format ( 0 , " out " ) ] = BinSelKnobvalues [ value ]
2023-05-07 17:58:52 +02:00
2023-03-28 20:27:27 +02:00
elif cmd == " M " :
2023-07-31 13:33:01 +02:00
firstcom = 1
if value == 1 :
if Destination [ io ] == 1 and LinuxKeyboardInput == 1 :
2023-08-02 23:35:40 +02:00
subprocess . call ( [ " xdotool " , " key " , Chars [ io ] ] )
2023-05-02 16:04:26 +02:00
if ( Debug ) : print ( " Emulating Keypress {} " . format ( Chars [ io ] ) )
2023-07-31 13:33:01 +02:00
if Destination [ io ] == 2 and LinuxKeyboardInput == 1 :
2023-08-02 23:35:40 +02:00
subprocess . call ( [ " xdotool " , " type " , Chars [ io ] ] )
2023-07-31 13:33:01 +02:00
if ( Debug ) : print ( " Emulating Keypress {} " . format ( Chars [ io ] ) )
else :
2023-08-02 23:35:40 +02:00
c [ " keypad. {} " . format ( Chars [ io ] ) ] = 1
2023-07-23 17:32:58 +02:00
if ( Debug ) : print ( " keypad {} : {} " . format ( Chars [ io ] , 1 ) )
2023-05-02 12:30:53 +02:00
2023-07-31 13:33:01 +02:00
if value == 0 & Destination [ io ] == 0 :
c [ " keypad. {} " . format ( Chars [ io ] ) ] = 0
if ( Debug ) : print ( " keypad {} : {} " . format ( Chars [ io ] , 0 ) )
2023-05-02 12:30:53 +02:00
2023-07-31 13:33:01 +02:00
2023-07-23 10:50:52 +02:00
elif cmd == " R " :
2023-07-23 17:29:17 +02:00
firstcom = 1
if JoySticks > 0 :
for pins in range ( JoySticks * 2 ) :
if ( io == JoyStickPins [ pins ] ) :
2023-07-23 17:41:21 +02:00
c [ " counter. {} " . format ( io ) ] = value
if ( Debug ) : print ( " counter. {} : {} " . format ( io , value ) )
2023-07-23 17:29:17 +02:00
if QuadEncs > 0 :
if QuadEncSig [ io ] == 1 :
2023-07-23 17:06:16 +02:00
if value == 0 :
2023-07-23 17:41:21 +02:00
c [ " counterdown. {} " . format ( io ) ] = 1
2023-07-23 17:29:17 +02:00
time . sleep ( 0.05 )
2023-07-23 17:41:21 +02:00
c [ " counterdown. {} " . format ( io ) ] = 0
2023-07-23 17:29:17 +02:00
time . sleep ( 0.05 )
if value == 1 :
2023-07-23 17:41:21 +02:00
c [ " counterup. {} " . format ( io ) ] = 1
2023-07-23 17:29:17 +02:00
time . sleep ( 0.05 )
2023-07-23 17:41:21 +02:00
c [ " counterup. {} " . format ( io ) ] = 0
2023-07-23 17:29:17 +02:00
time . sleep ( 0.05 )
if QuadEncSig [ io ] == 2 :
2023-07-23 17:41:21 +02:00
c [ " counter. {} " . format ( io ) ] = value
2023-05-02 12:30:53 +02:00
2022-11-25 22:05:54 +01:00
elif cmd == ' E ' :
2022-11-25 23:58:26 +01:00
arduino . write ( b " E0:0 \n " )
if ( Debug ) : print ( " Sending E0:0 to establish contact " )
2022-11-25 22:05:54 +01:00
else : pass
except : pass
except KeyboardInterrupt :
if ( Debug ) : print ( " Keyboard Interrupted.. BYE " )
exit ( )
except :
if ( Debug ) : print ( " I received garbage " )
arduino . flush ( )
2022-11-25 23:58:26 +01:00
2023-03-30 23:37:00 +02:00
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
2022-11-25 23:58:26 +01:00
2023-03-30 23:37:00 +02:00
if keepAlive ( event ) : #keep com alive. This is send to help Arduino detect connection loss.
2022-11-25 22:05:54 +01:00
arduino . write ( b " E: \n " )
if ( Debug ) : print ( " keepAlive " )
event = time . time ( )
2023-05-07 11:20:04 +02:00
2023-07-23 14:17:07 +02:00
time . sleep ( 0.01 )
2023-07-23 10:50:52 +02:00