|
1
|
|
|
#import smbus |
|
2
|
|
|
import time |
|
3
|
|
|
|
|
4
|
|
|
from lib.updater import Updater |
|
5
|
|
|
from lib.configure import Configure |
|
6
|
|
|
from lib.node import Node |
|
7
|
|
|
from lib.game import Game |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class SpeedOfPi(object): |
|
11
|
|
|
def __init__(self): |
|
12
|
|
|
self.bus = '3' #smbus.SMBus(1) |
|
13
|
|
|
self.nodes = {} |
|
14
|
|
|
|
|
15
|
|
|
self.config = Configure() |
|
16
|
|
|
self.load_config() |
|
17
|
|
|
|
|
18
|
|
|
def create_nodes(self, node_config): |
|
19
|
|
|
for node in node_config['nodes']: |
|
20
|
|
|
try: |
|
21
|
|
|
node_number = list(node)[0] |
|
22
|
|
|
button_address = node[node_number][0]['button'][0]['address'] |
|
23
|
|
|
button_port = node[node_number][0]['button'][1]['port'] |
|
24
|
|
|
led_address = node[node_number][1]['led'][0]['address'] |
|
25
|
|
|
led_port = node[node_number][1]['led'][1]['port'] |
|
26
|
|
|
|
|
27
|
|
|
self.nodes[node_number] = Node(self.bus, node_number, button_address, button_port, led_address, led_port) |
|
28
|
|
|
except: |
|
29
|
|
|
# throw exception here so user checks config file |
|
30
|
|
|
print('unable to load node ' + node[0]) |
|
31
|
|
|
|
|
32
|
|
|
def load_config(self): |
|
33
|
|
|
if not self.config.read_config(): |
|
34
|
|
|
print('Could not read file. would you like to setup nodes?') |
|
35
|
|
|
print('Would you like to setup nodes?') |
|
36
|
|
|
#todo expect keyboad input [Y/n] (Y,y, yes, YES, *enter) |
|
37
|
|
|
#if no exit else run set_config() |
|
38
|
|
|
exit() |
|
39
|
|
|
|
|
40
|
|
|
node_config = self.config.get_config() |
|
41
|
|
|
self.create_nodes(node_config) |
|
42
|
|
|
|
|
43
|
|
|
def set_config(self): |
|
44
|
|
|
self.config.set_config() |
|
45
|
|
|
|
|
46
|
|
|
def update(self): |
|
47
|
|
|
updater = Updater() |
|
48
|
|
|
|
|
49
|
|
|
if updater.check(): |
|
50
|
|
|
updater.update() |
|
51
|
|
|
else: |
|
52
|
|
|
print("You are up to date!") |
|
53
|
|
|
|
|
54
|
|
|
def multi_player(self): |
|
55
|
|
|
difficulty = 1 |
|
56
|
|
|
game = Game(difficulty) |
|
57
|
|
|
game.multi_player(self.nodes) |
|
58
|
|
|
|
|
59
|
|
|
def single_player(self): |
|
60
|
|
|
difficulty = 1 |
|
61
|
|
|
game = Game(difficulty) |
|
62
|
|
|
game.single_player(self.nodes) |
|
63
|
|
|
|
|
64
|
|
|
if __name__ == '__main__': |
|
65
|
|
|
a = SpeedOfPi() |
|
66
|
|
|
a.single_player() |