-
Notifications
You must be signed in to change notification settings - Fork 1
/
Microcontroller.py
75 lines (57 loc) · 1.72 KB
/
Microcontroller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import time
import serial
class Microcontroller:
def __init__(self, name: str, address: str, baud: int):
pass
def reset(self):
pass
def read(self, num_bytes) -> bytes:
pass
def write(self, num_bytes) -> bytes:
pass
def ping(self, timeout) -> bool:
pass
import os
class SimulatedArduino(Microcontroller):
def read(self, num_bytes) -> bytes:
return os.urandom(num_bytes)
class Arduino(Microcontroller):
def __init__(self, name: str, address: str, baud: int):
self.name = name
self.serial = serial.Serial(address, baud)
def reset(self):
self.serial.setDTR(True)
time.sleep(1)
self.serial.flush()
self.serial.reset_input_buffer()
self.serial.reset_output_buffer()
self.serial.setDTR(False)
# Wait for arduino to reset
time.sleep(3)
def read(self, num_bytes) -> bytes:
# Blocks until enough bytes are read.
# To change this, add a timeout to self.serial.read().
return self.serial.read(num_bytes)
def write(self, bytes: bytes) -> int:
# Returns the number of bytes actually written.
return self.serial.write(bytes)
def ping(self, timeout) -> bool:
initial_timeout = self.serial.timeout
self.serial.timeout = timeout
self.serial.write(bytes([1]))
reading = self.serial.read(1)
self.serial.timeout = initial_timeout
return len(reading) > 0
READ = 0
SET_PIN = 1
class SensorController:
def __init__(self, arduino: Microcontroller):
self.arduino = arduino
def read_pin(self, pin: int) -> int:
self.arduino.write(bytes([READ, pin]))
return int.from_bytes(self.arduino.read(4), byteorder='big')
class ValveController:
def __init__(self, arduino: Microcontroller):
self.arduino = arduino
def set_pin(self, pin: int, value: int):
self.arduino.write(bytes([SET_PIN, pin, value]))