-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontroller.py
More file actions
126 lines (95 loc) · 3.79 KB
/
controller.py
File metadata and controls
126 lines (95 loc) · 3.79 KB
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
120
121
122
123
124
125
126
from time import sleep
class Controller:
class Mock:
pass
ON_BOARD_LED_PIN_NO = None
ON_BOARD_LED_HIGH_IS_ON = True
GPIO_PINS = []
PIN_ID_FOR_LORA_RESET = None
PIN_ID_FOR_LORA_SS = None
PIN_ID_SCK = None
PIN_ID_MOSI = None
PIN_ID_MISO = None
PIN_ID_FOR_LORA_DIO0 = None
PIN_ID_FOR_LORA_DIO1 = None
PIN_ID_FOR_LORA_DIO2 = None
PIN_ID_FOR_LORA_DIO3 = None
PIN_ID_FOR_LORA_DIO4 = None
PIN_ID_FOR_LORA_DIO5 = None
def __init__(self,
pin_id_led = ON_BOARD_LED_PIN_NO,
on_board_led_high_is_on = ON_BOARD_LED_HIGH_IS_ON,
pin_id_reset = PIN_ID_FOR_LORA_RESET,
blink_on_start = (2, 0.5, 0.5)):
self.pin_led = self.prepare_pin(pin_id_led)
self.on_board_led_high_is_on = on_board_led_high_is_on
self.pin_reset = self.prepare_pin(pin_id_reset)
self.reset_pin(self.pin_reset)
self.transceivers = {}
self.blink_led(*blink_on_start)
def add_transceiver(self,
transceiver,
pin_id_ss = PIN_ID_FOR_LORA_SS,
pin_id_RxDone = PIN_ID_FOR_LORA_DIO0,
pin_id_RxTimeout = PIN_ID_FOR_LORA_DIO1,
pin_id_ValidHeader = PIN_ID_FOR_LORA_DIO2,
pin_id_CadDone = PIN_ID_FOR_LORA_DIO3,
pin_id_CadDetected = PIN_ID_FOR_LORA_DIO4,
pin_id_PayloadCrcError = PIN_ID_FOR_LORA_DIO5):
transceiver.blink_led = self.blink_led
transceiver.pin_ss = self.prepare_pin(pin_id_ss)
transceiver.pin_RxDone = self.prepare_irq_pin(pin_id_RxDone)
transceiver.pin_RxTimeout = self.prepare_irq_pin(pin_id_RxTimeout)
transceiver.pin_ValidHeader = self.prepare_irq_pin(pin_id_ValidHeader)
transceiver.pin_CadDone = self.prepare_irq_pin(pin_id_CadDone)
transceiver.pin_CadDetected = self.prepare_irq_pin(pin_id_CadDetected)
transceiver.pin_PayloadCrcError = self.prepare_irq_pin(pin_id_PayloadCrcError)
self.spi = self.prepare_spi(self.get_spi())
transceiver.transfer = self.spi.transfer
transceiver.init()
self.transceivers[transceiver.name] = transceiver
return transceiver
def prepare_pin(self, pin_id, in_out = None):
reason = '''
# a pin should provide:
# .pin_id
# .low()
# .high()
# .value() # read input.
# .irq() # (ESP8266/ESP32 only) ref to the irq function of real pin object.
'''
raise NotImplementedError(reason)
def prepare_irq_pin(self, pin_id):
reason = '''
# a irq_pin should provide:
# .set_handler_for_irq_on_rising_edge() # to set trigger and handler.
# .detach_irq()
'''
raise NotImplementedError(reason)
def get_spi(self):
reason = '''
# initialize SPI interface
'''
raise NotImplementedError(reason)
def prepare_spi(self, spi):
reason = '''
# a spi should provide:
# .close()
# .transfer(pin_ss, address, value = 0x00)
'''
raise NotImplementedError(reason)
def led_on(self, on = True):
self.pin_led.high() if self.on_board_led_high_is_on == on else self.pin_led.low()
def blink_led(self, times = 1, on_seconds = 0.1, off_seconds = 0.1):
for i in range(times):
self.led_on(True)
sleep(on_seconds)
self.led_on(False)
sleep(off_seconds)
def reset_pin(self, pin, duration_low = 0.05, duration_high = 0.05):
pin.low()
sleep(duration_low)
pin.high()
sleep(duration_high)
def __exit__(self):
self.spi.close()