first commit

this is my first "working" version
This commit is contained in:
Alexander Richter 2022-11-21 12:53:33 +01:00
parent d16b03bb83
commit 958cc0d9cb
2 changed files with 302 additions and 0 deletions

View File

@ -0,0 +1,191 @@
//###IO's###
//###Analog In###
int SpOd[] = {A1,8,-1}; //Knob Spindle Overdrive on A1, has 4 Pos, and initialised with 0
int FdRes[] = {A2,3,-1};//Knob Feed Resolution on A2, has 9 Pos, and initialised with 0
const int SpSp = A3;//Knob Potentiometer for SpindleSpeed in manual mode
//###Digital In###
//Absolute encoder Knob
const int DI0 = 27;//1
const int DI1 = 28;//2
const int DI2 = 31;//4
const int DI3 = 29;//8
const int DI4 = 30;//16
//Buttons
const int Buttons = 17; //Number of Buttons (Inputs)
int Button[] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48};
//Digital Out###
const int Outputs = 22; //Number of Outputs (i use them for LEDs in Buttons)
int OutPin[] = {13,12,11,10,9,8,7,6,5,4,3,2,22,23,24,25,26};
//Variables for Saving States
int oldvar = -1;
int SpSpvar = -1;
int ButtonState[Buttons];
int OutState[Outputs];
int FdOdSt = -1;
int SpOdSt = -1;
int SpSpSt = -1;
void setup() {
pinMode(DI0, INPUT_PULLUP);
pinMode(DI1, INPUT_PULLUP);
pinMode(DI2, INPUT_PULLUP);
pinMode(DI3, INPUT_PULLUP);
pinMode(DI4, INPUT_PULLUP);
//setting Buttons as Inputs with internal Pullup Resistors
for(int i= 0; i<Buttons;i++){
pinMode(Button[i], INPUT_PULLUP);
ButtonState[i] = -1;
}
for(int o= 0; o<Outputs;o++){
pinMode(OutPin[o], OUTPUT);
OutState[o] = 0;
}
//Setup Serial
Serial.begin(9600);
while (!Serial){
}
if (Serial){delay(1000);}
}
void loop() {
listenSerial();
readAbsKnob();
readPoti(SpSp);
SpOd[2] = readLPoti(SpOd[0],SpOd[1],SpOd[2]);
FdRes[2] = readLPoti(FdRes[0],FdRes[1],FdRes[2]);
writeOutputs();
readInputs();
delay(10);
}
int listenSerial(){
long rec=0;
if(Serial.available()){
rec = Serial.parseInt();
if(rec >= 10 && rec % 2){
rec --;
rec = rec/10;
if(rec < Buttons){
OutState[rec]=1;
}
}
if(rec >= 10 && !(rec % 2)){
rec = rec/10;
if(rec < Buttons){
OutState[rec]=0;
}
}
rec= 0;
}
}
int writeOutputs(){
for(int x = 0; x<Outputs;x++){
digitalWrite(OutPin[x], OutState[x]);
}
}
int readLPoti(int Pin,int Pos,int Stat){
int var = analogRead(Pin)+20; //giving it some margin so Numbers dont jitter
Pos = 1024/Pos;
var = var/Pos;
if(var != Stat){
Stat = var;
Serial.print("LP");
Serial.print(Pin);
Serial.print(":");
Serial.println(Stat);
}
return (Stat);
}
int readPoti(int Pin){
unsigned long var = 0;;
for(int i= 0;i<500; i++){
var = var+ analogRead(Pin);
}
var = var / 500;
if (SpSpSt!=var){
Serial.print("Pt");
Serial.print(Pin);
Serial.print(":");
Serial.println(var);
SpSpSt = var;
}
return (var);
}
int readInputs(){
for(int i= 0;i<Buttons; i++){
int State = digitalRead(Button[i]);
if(ButtonState[i]!= State){
ButtonState[i] = State;
Serial.print("I");
Serial.print(i);
Serial.print(":");
Serial.println(ButtonState[i]);
}
}
}
int readAbsKnob(){
int var = 0;
if(digitalRead(DI0)==1){
var += 1;
}
if(digitalRead(DI1)==1){
var += 2;
}
if(digitalRead(DI2)==1){
var += 4;
}
if(digitalRead(DI3)==1){
var += 8;
}
if(digitalRead(DI4)==1){
var += 16;
}
if(var != oldvar){
Serial.print("AK:");
Serial.println(var);
}
oldvar = var;
return (var);
}

111
arduino.py Normal file
View File

@ -0,0 +1,111 @@
#!/usr/bin/python3.9
# HAL userspace component to interface with Arduino board
# Copyright (C) 2007 Jeff Epler <jepler@unpythonic.net>
#
# 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
import serial, time, hal
Inputs = 17
InPinmap = [32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48]
# Name, positions
AnalogKnob = [""]
OutPinmap = [32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48]
c = hal.component("arduino")
c.newpin("SpOd", hal.HAL_FLOAT, hal.HAL_IN) #8 Pos latching poti
c.newpin("FdRes", hal.HAL_FLOAT, hal.HAL_IN) #4 pos latching poti
c.newpin("AK", hal.HAL_FLOAT, hal.HAL_IN) #absolute Encoder Knob
c.newpin("SpSp", hal.HAL_FLOAT, hal.HAL_IN) #potentiometer
#c.newpin("analog-in-%02d" % port, hal.HAL_FLOAT, hal.HAL_OUT)
#c.newparam("analog-in-%02d-offset" % port, hal.HAL_FLOAT, hal.HAL_RW)
#c.newparam("analog-in-%02d-gain" % port, hal.HAL_FLOAT, hal.HAL_RW)
for port in range(Inputs):
c.newpin("digital-in-%02d" % InPinmap[port], hal.HAL_BIT, hal.HAL_IN)
c.newparam("digital-in-%02d-invert" % InPinmap[port], hal.HAL_BIT, hal.HAL_RW)
c.ready()
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):
if input_str is None or input_str == '':
return 0
out_number = ''
for ele in input_str:
if ele.isdigit():
out_number += ele
return int(out_number)
#try:
arduino = serial.Serial('/dev/ttyACM0', 9600, timeout=1, xonxoff=False, rtscts=False, dsrdtr=True)
while True:
try:
data = arduino.readline().decode('utf-8')
data = data.split(":",1)
if len(data) == 2:
data[1] = extract_nbr(data[1])
if data[1]<0: data[1] = 0
if data[0] == "Pt57":
c.SpSp = data[1]
elif data[0] == "LP55":
c.SpOd = data[1]
elif data[0] == "LP56":
c.FdRes = data[1]
elif data[0] == "AK":
c.AK = data[1]
else: pass
finally:
pass
"""except :
print("Lost Connection")
finally :
# serial.close
print("Program closed")
"""