-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_machine.py
82 lines (63 loc) · 1.57 KB
/
mock_machine.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
76
77
78
79
80
81
82
# mock_machine.py
class MockPin:
IN = 0
OUT = 1
PULL_UP = 2
PULL_DOWN = 3
def __init__(self, pin_number, mode, pull=None):
self.pin_number = pin_number
self.mode = mode
self.pull = pull
self._value = 0
def on(self):
self._value = 1
def off(self):
self._value = 0
def value(self, val=None):
if val is None:
return self._value
else:
self._value = val
class MockADC:
def __init__(self, pin):
self.pin = pin
self._value = 0
def read(self):
return self._value
def set_value(self, val):
self._value = val
class MockPWM:
def __init__(self, pin, freq=500, duty=512):
self.pin = pin
self._freq = freq
self._duty = duty
def freq(self, freq=None):
if freq is None:
return self._freq
else:
self._freq = freq
def duty(self, duty=None):
if duty is None:
return self._duty
else:
self._duty = duty
class MockI2C:
def __init__(self, id, scl, sda, freq=100000):
self.id = id
self.scl = scl
self.sda = sda
self.freq = freq
self.memory = {}
def readfrom(self, addr, nbytes):
return self.memory.get(addr, [0]*nbytes)
def writeto(self, addr, buf):
self.memory[addr] = buf
def sleep_ms(ms):
# No-op function to simulate time delay in tests
pass
class MockMachine:
Pin = MockPin
ADC = MockADC
PWM = MockPWM
I2C = MockI2C
sleep_ms = sleep_ms