Skip to content

Instantly share code, notes, and snippets.

@quenhus
Created January 27, 2018 18:19
Show Gist options
  • Save quenhus/060b7518d9472cbeae026f37158fd7f0 to your computer and use it in GitHub Desktop.
Save quenhus/060b7518d9472cbeae026f37158fd7f0 to your computer and use it in GitHub Desktop.
Python/Arduino Bridge
#ifndef SERIAL_RATE
#define SERIAL_RATE 115200
#endif
unsigned long serial_delay = 50;
void setup() {
Serial.begin(SERIAL_RATE);
//Attendre que l'usb soit connecté
while (!Serial) {
delay(serial_delay);
}
handshake();
}
void loop() {
static unsigned long last_serial_time = 0;
//Si la dernière lecture était il y a plus de serial_delay
if(millis() - last_serial_time >= serial_delay) {
last_serial_time = millis();
int pin;
int value;
char request_type = requestType();
if(request_type != 0) {
switch (request_type) {
case 1 :
//Mettre un pin en output
pinMode(nextData(), OUTPUT); break;
case 2 :
//Pin à low
digitalWrite(nextData(), LOW); break;
case 3 :
//Pin à high
digitalWrite(nextData(), HIGH); break;
case 4 :
//Valeur numérique d'un pin
Serial.println(digitalRead(nextData())); break;
case 5 :
//Ecrire une valeur analogique d'un pin
pin = nextData();
value = nextData();
analogWrite(pin, value); break;
case 6 :
//Lire une valeur analogique d'un pin
Serial.println(analogRead(nextData())); break;
case 7 :
//Lire une valeur analogique d'un pin
serial_delay = nextData(); break;
default:
break;
}
handshake();
}
}
}
//Récuperer le type de requête faites
int requestType() {
if(Serial.available() == 0) {
return 0;
}
return char(Serial.parseInt());
}
//Récuprer la data suivante
int nextData() {
return Serial.parseInt();
}
//Nettoyer le buffer et envoyer un handshake
void handshake() {
Serial.read();
Serial.println('w');
}
import serial
import time
class Arduino:
__output_pins = []
def __init__(self, port, baudrate=115200):
self.serial = serial.Serial(port, baudrate)
def __str__(self):
return "ArduinoQ is on port %s at %d baudrate" %(self.serial.port, self.serial.baudrate)
def output(self, *pin_tuple):
for pin in pin_tuple:
self.__sendData(1, pin)
self.__output_pins.append(pin)
return True
def serial_delay(self, delay):
self.__sendData(7, delay)
return True
def setLow(self, *pin_tuple):
for pin in pin_tuple:
self.__sendData(2, pin)
return True
def setHigh(self, *pin_tuple):
for pin in pin_tuple:
self.__sendData(3, pin)
return True
def getState(self, pin):
self.__sendData(4, pin)
return self.__formatPinState(self.__getData()[0])
def analogWrite(self, pin, value):
self.__sendData(5, pin, value)
return True
def analogRead(self, pin):
self.__sendData(6, pin)
return int(self.__getData())
def turnOff(self):
for pin in self.__output_pins:
self.setLow(pin)
return True
def __sendData(self, *data_tuple):
while(self.__getData() != 'w'):
pass
serial_data = str('&'.join([str(i) for i in data_tuple]) + '&').encode('utf-8')
self.serial.write(serial_data)
def __getData(self):
input_string = self.serial.readline()
input_string = input_string.decode('utf-8')
return input_string.replace('\r', "").replace('\n', "")
def __formatPinState(self, pinValue):
if pinValue == '1':
return True
else:
return False
def close(self):
self.serial.close()
return True
import time
import random
from arduino3 import *
red_pin = 3
green_pin = 5
blue_pin = 6
button_pin = 2
time_off = 1
a = Arduino('COM3')
a.turnOff()
a.output(red_pin, green_pin, blue_pin)
while not a.getState(button_pin):
a.analogWrite(red_pin, random.randint(0, 255))
a.analogWrite(green_pin, random.randint(0, 255))
a.analogWrite(blue_pin, random.randint(0, 255))
time.sleep(0.005)
a.turnOff()
a.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment