-
Notifications
You must be signed in to change notification settings - Fork 0
/
board_interface.py
138 lines (105 loc) · 3.63 KB
/
board_interface.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
import time
import random
from pyfirmata import Arduino, util
from pyfirmata import SERVO as SERVO_MODE, PWM as PWM_MODE, OUTPUT as OUTPUT_MODE
from utils import Singleton
UPPER_BOUND_DEGREES = 179
LOWER_BOUND_DEGREES = 0
SLEEP_TIME = 0.5
class PinManager():
"""
This class is responsible for the pins on the board
"""
dig_pin_output_format = 'd:{0}:o'
dig_pin_input_format = 'd:{0}:i'
analog_pin_output_format = 'a:{0}:o'
analog_pin_input_format = 'a:{0}:i'
digital_pins_available = []
analog_pins_available = []
digital_pins_allocated = []
analog_pins_allocated = []
__metaclass__ = Singleton
def __init__(self, board):
self.board = board
self.digital_pins_available = [x for x in range(2, 13)]
self.analog_pins_available = [x for x in range(6)]
def get_digital_pin_out(self, pin_number=None):
if (not pin_number or (pin_number not in self.digital_pins_available)):
pin_number = self.digital_pins_available.pop()
self.digital_pins_allocated.append(pin_number)
return self.board.get_pin(self.dig_pin_output_format.format(pin_number))
def get_analog_pin_out(self, pin_number=None):
if (not pin_number or (pin_number not in self.analog_pins_available)):
pin_number = self.analog_pins_available.pop()
self.analog_pins_allocated.append(pin_number)
return self.board.get_pin(self.analog_pin_output_format.format(pin_number))
def set_pin_mode(self, pin, mode):
if mode == "Servo":
pin.mode = SERVO_MODE
elif mode == "PWM":
pin.mode = PWM_MODE
elif mode == "OUTPUT":
pin.mode = OUTPUT_MODE
class HoduinoBoardInterface():
"""
This is the Arduino Board Interface.
This class initialize the Board and provides all the methods
for the interaction with it.
"""
__metaclass__ = Singleton
def __init__(self, port):
try:
self.board = Arduino(port)
except OSError as e:
raise Exception("Arduino not found on: {0}".format(port))
self._setup()
def _setup(self):
# Setup the pins
self.pin_manager = PinManager(self.board)
self.motor_pin = self.pin_manager.get_digital_pin_out(9)
self.pin_manager.set_pin_mode(self.motor_pin, "Servo")
self.buzzer_pin = self.pin_manager.get_digital_pin_out(3)
self.pin_manager.set_pin_mode(self.buzzer_pin, "PWM")
def _spin(self, degrees):
# spin the weel
self.motor_pin.write(degrees)
time.sleep(SLEEP_TIME)
def _buzzer(self, value):
self.buzzer_pin.write(value)
time.sleep(SLEEP_TIME)
self.buzzer_pin.write(0.0)
def _melody(self):
notes = {
'c': 62,
'd': 94,
'e': 30,
'f': 49,
'g': 50,
'a': 140,
'b': 194,
'C': 223
}
freqs = ['c', 'g', 'a', 'a', 'f', 'e']
for f in freqs:
self.buzzer_pin.write(notes[f]/255.0)
time.sleep(0.2)
self.buzzer_pin.write(0.0)
def donation_reaction(self):
# buzzer
# self._melody()
# spin procedure for donation reaction
self._spin(90)
self._spin(150)
self._spin(30)
self._spin(150)
self._spin(30)
self._spin(90)
def close_arduino(self):
self._exit()
def _exit(self):
try:
self.board.exit()
print "Succesfully exit from arduino board"
except:
print "Error while exiting arduino board"