-
Notifications
You must be signed in to change notification settings - Fork 64
/
s2a_fm.py
145 lines (117 loc) · 5.28 KB
/
s2a_fm.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
139
140
141
142
143
144
145
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 25 13:17:15 2013
@author: Alan Yorinks
Copyright (c) 2013-14 Alan Yorinks All right reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import os
import sys
import logging
from PyMata.pymata import PyMata
import scratch_http_server
from scratch_command_handlers import ScratchCommandHandlers
import time
#noinspection PyBroadException
def s2a_fm():
"""
This is the "main" function of the program.
It will instantiate PyMata for communication with an Arduino micro-controller
and the command handlers class.
It will the start the HTTP server to communicate with Scratch 2.0
@return : This is the main loop and should never return
"""
# total number of pins on arduino board
total_pins_discovered = 0
# number of pins that are analog
number_of_analog_pins_discovered = 0
# make sure we have a log directory and if not, create it.
if not os.path.exists('log'):
os.makedirs('log')
# turn on logging
logging.basicConfig(filename='./log/s2a_fm_debugging.log', filemode='w', level=logging.DEBUG)
logging.info('s2a_fm version 1.5 Copyright(C) 2013-14 Alan Yorinks All Rights Reserved ')
print 's2a_fm version 1.5 Copyright(C) 2013-14 Alan Yorinks All Rights Reserved '
# get the com_port from the command line or default if none given
# if user specified the com port on the command line, use that when invoking PyMata,
# else use '/dev/ttyACM0'
if len(sys.argv) == 2:
com_port = str(sys.argv[1])
else:
com_port = '/dev/ttyACM0'
logging.info('com port = %s' % com_port)
try:
# instantiate PyMata
firmata = PyMata(com_port) # pragma: no cover
except Exception:
print 'Could not instantiate PyMata - is your Arduino plugged in?'
logging.exception('Could not instantiate PyMata - is your Arduino plugged in?')
logging.debug("Exiting s2a_fm")
return
# determine the total number of pins and the number of analog pins for the Arduino
# get the arduino analog pin map
# it will contain an entry for all the pins with non-analog set to firmata.IGNORE
firmata.analog_mapping_query()
capability_map = firmata.get_analog_mapping_request_results()
firmata.capability_query()
print "Please wait for Total Arduino Pin Discovery to complete. This can take up to 30 additional seconds."
# count the pins
for pin in capability_map:
total_pins_discovered += 1
# non analog pins will be marked as IGNORE
if pin != firmata.IGNORE:
number_of_analog_pins_discovered += 1
# log the number of pins found
logging.info('%d Total Pins and %d Analog Pins Found' % (total_pins_discovered, number_of_analog_pins_discovered))
# instantiate the command handler
scratch_command_handler = ScratchCommandHandlers(firmata, com_port, total_pins_discovered,
number_of_analog_pins_discovered)
# wait for a maximum of 30 seconds to retrieve the Arduino capability query
start_time = time.time()
pin_capability = firmata.get_capability_query_results()
while not pin_capability:
if time.time() - start_time > 30:
print ''
print "Could not determine pin capability - exiting."
firmata.close()
# keep sending out a capability query until there is a response
pin_capability = firmata.get_capability_query_results()
time.sleep(.1)
# we've got the capability, now build a dictionary with pin as the key and a list of all the capabilities
# for the pin as the key's value
pin_list = []
total_pins_discovered = 0
for entry in pin_capability:
# bump up pin counter each time IGNORE is found
if entry == firmata.IGNORE:
scratch_command_handler.pin_map[total_pins_discovered] = pin_list
total_pins_discovered += 1
pin_list = []
else:
pin_list.append(entry)
print "Arduino Total Pin Discovery completed in %d seconds" % (int(time.time() - start_time))
try:
# start the server passing it the handle to PyMata and the command handler.
scratch_http_server.start_server(firmata, scratch_command_handler)
except Exception:
logging.debug('Exception in s2a_fm.py %s' % str(Exception))
firmata.close()
return
except KeyboardInterrupt:
# give control back to the shell that started us
logging.info('s2a_fm.py: keyboard interrupt exception')
firmata.close()
return
if __name__ == "__main__":
s2a_fm()