-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmachine.py
120 lines (87 loc) · 3.27 KB
/
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
The purpose of this module is to emulate the machine module of the ESP32 chip. The primary reason to
emulate the chip is for test-driving code (TDD).
Verified Firmware version: esp32-20190610-v1.11-37-g62f004ba4
Portions also tested with esp8266-20190529-v1.11
"""
import time
EMULATION_MODE = True
__author__ = 'Todd Flanders https://github.com/tflander/Esp32IotKata'
expectedPulseTimeForTesting = 0
expectedPulseTimeErrorForTesting = None
expectedTimeSleepMs = []
expectedTimeSleepUs = []
reset_called_for_testing = False
def resetExpectationsForTesting():
global expectedPulseTimeForTesting
global expectedPulseTimeErrorForTesting
global expectedTimeSleepMs
global expectedTimeSleepUs
global reset_called_for_testing
expectedPulseTimeForTesting = 0
expectedPulseTimeErrorForTesting = None
expectedTimeSleepMs = []
expectedTimeSleepUs = []
reset_called_for_testing = False
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def time_pulse_us(pin, pulse_level, timeout_us):
global expectedPulseTimeErrorForTesting
if expectedPulseTimeErrorForTesting is not None:
raise expectedPulseTimeErrorForTesting # pylint: disable=raising-bad-type
try:
pulse_time = expectedPulseTimeForTesting.pop(0)
except:
pulse_time = expectedPulseTimeForTesting
if type(pulse_time) == int:
return pulse_time
else:
if not pulse_time:
raise Exception("unexpected call to time_pulse_us on empty expectation list")
raise pulse_time
def reset():
global reset_called_for_testing
reset_called_for_testing = True
def sleep_us_for_monkey_patching(delayUs):
time.sleep(delayUs / 1000000)
expectedTimeSleepUs.append(delayUs)
def sleep_ms_for_monkey_patching(delayMs):
time.sleep(delayMs / 1000)
expectedTimeSleepMs.append(delayMs)
time.sleep_us = sleep_us_for_monkey_patching
time.sleep_ms = sleep_ms_for_monkey_patching
class Pin:
IN = "in"
OUT = "out"
def resetExpectationsForTesting(self):
self.triggerValuesForTesting = []
# noinspection PyUnusedLocal,PyUnusedLocal
def __init__(self, pin, mode=OUT, pull=None):
self.currentStateForTesting = None
self.currentStateForTesting = 1
self.currentStateForTesting = None
self.pinForTesting = pin
self.triggerValuesForTesting = []
self.resetExpectationsForTesting()
def on(self):
self.currentStateForTesting = 1
self.triggerValuesForTesting.append(self.currentStateForTesting)
def off(self):
self.currentStateForTesting = 0
self.triggerValuesForTesting.append(self.currentStateForTesting)
def value(self, newValue=None):
if newValue == 0:
self.off()
elif newValue == 1:
self.on()
if self.currentStateForTesting is None:
raise Exception("Checking Value of Uninitialized OUT Pin. Set the value before checking.")
return self.currentStateForTesting
class Timer:
PERIODIC = 0
def __init__(self, id):
self.timer_id_for_testing = id
self.is_running_for_testing = False
def deinit(self):
self.is_running_for_testing = False
def init(self, period, mode=PERIODIC, callback=None):
self.is_running_for_testing = True