-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpracticum.py
98 lines (83 loc) · 3.16 KB
/
practicum.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
import usb
RQ_GET_ID = 0
RQ_SET_SERVO = 1
####################################
def find_mcu_boards():
'''
Find all Practicum MCU boards attached to the machine, then return a list
of USB device handles for all the boards
>>> devices = find_mcu_boards()
>>> first_board = McuBoard(devices[0])
'''
boards = [dev for bus in usb.busses()
for dev in bus.devices
if (dev.idVendor,dev.idProduct) == (0x16c0,0x05dc)]
return boards
####################################
class McuBoard:
'''
Generic class for accessing Practicum MCU board via USB connection.
'''
################################
def __init__(self, dev):
self.device = dev
self.handle = dev.open()
################################
def usb_write(self, request, data=[], index=0, value=0):
'''
Send data output to the USB device (i.e., MCU board)
request: request number to appear as bRequest field on the USB device
index: 16-bit value to appear as wIndex field on the USB device
value: 16-bit value to appear as wValue field on the USB device
'''
reqType = usb.TYPE_VENDOR | usb.RECIP_DEVICE | usb.ENDPOINT_OUT
self.handle.controlMsg(
reqType, request, data, value=value, index=index)
################################
def usb_read(self, request, length=1, index=0, value=0):
'''
Request data input from the USB device (i.e., MCU board)
request: request number to appear as bRequest field on the USB device
length: number of bytes to read from the USB device
index: 16-bit value to appear as wIndex field on the USB device
value: 16-bit value to appear as wValue field on the USB device
If successful, the method returns a tuple of length specified
containing data returned from the MCU board.
'''
reqType = usb.TYPE_VENDOR | usb.RECIP_DEVICE | usb.ENDPOINT_IN
buf = self.handle.controlMsg(
reqType, request, length, value=value, index=index)
return buf
####################################
class PeriBoard:
################################
def __init__(self, mcu):
self.mcu = mcu
################################
def set_led(self, led_no, led_state):
'''
Set status of LED led_no on peripheral board to led_state
(0 => off, 1 => on)
'''
self.mcu.usb_write(request=RQ_SET_LED,index=led_no,value=led_state)
return
################################
def set_led_value(self, value):
'''
Display right 3 bits of value on peripheral board's LEDs
'''
return
################################
def get_switch(self):
'''
Return a boolean value indicating whether the switch on the peripheral
board is currently pressed
'''
state = self.mcu.usb_read(request=RQ_GET_SWITCH,length=1)
return state[0] == 1
################################
def get_light(self):
'''
Return the current reading of light sensor on peripheral board
'''
return 0