@@ -41,7 +41,7 @@ def decode_pin_name(pin):
41
41
except KeyError as bad_name :
42
42
print ("There's no pin called {0}. Try a pin 3-13, 'LED', or 'A0'-'A3'." .format (bad_name ))
43
43
44
- def read_analog (pin ):
44
+ def analogRead (pin ):
45
45
chan = decode_pin_name (pin ) - 96 # offset of 96 maps channel to Port C on CPU
46
46
if (chan in range (4 )):
47
47
with open ('/sys/devices/platform/at91_adc/chan' + str (chan ), 'r' ) as f :
@@ -50,13 +50,34 @@ def read_analog(pin):
50
50
else :
51
51
return "Not an analog pin. Try 'A0', 'A1', 'A2', or 'A3'."
52
52
53
- def read_pin (pin ):
53
+ def analogWrite ():
54
+ pass
55
+
56
+ def digitalRead (pin ):
54
57
pin = decode_pin_name (pin )
55
58
with open ('/sys/class/gpio/gpio' + str (pin ) + '/value' , 'r' ) as f :
56
59
reading = f .read ()
57
60
return reading .strip ()
58
61
59
- def send_serial (text , speed = 19200 , port = '1' ):
62
+ def digitalWrite (pin , state ):
63
+ pin = decode_pin_name (pin )
64
+ with open ('/sys/class/gpio/gpio' + str (pin ) + '/value' , 'w' ) as f :
65
+ if (state == 'HIGH' ):
66
+ f .write ('1' )
67
+ else :
68
+ f .write ('0' )
69
+
70
+ def i2cRead ():
71
+ pass
72
+
73
+ def i2cWrite (val ):
74
+ cmd = 'i2cset -y 0 0x29 ' + str (val )
75
+ subprocess .Popen ([cmd ], shell = True )
76
+
77
+ def serialRead ():
78
+ pass
79
+
80
+ def serialWrite (text , speed = 19200 , port = '1' ):
60
81
import serial
61
82
ports = {
62
83
'1' : '/dev/ttyS1' ,
@@ -66,54 +87,15 @@ def send_serial(text, speed=19200, port='1'):
66
87
ser = serial .Serial (ports [str (port )], speed , timeout = 1 )
67
88
ser .write (str (text [0 :80 ]))
68
89
ser .close ()
90
+
91
+ def toggle (pin ):
92
+ if digitalRead (pin ):
93
+ digitalWrite (pin , 'LOW' )
94
+ else :
95
+ digitalWrite (pin , 'HIGH' )
69
96
70
- def set_pin_high (pin ):
71
- pin = decode_pin_name (pin )
72
- with open ('/sys/class/gpio/gpio' + str (pin ) + '/value' , 'w' ) as f :
73
- f .write ('1' )
74
-
75
- def set_pin_low (pin ):
76
- pin = decode_pin_name (pin )
77
- with open ('/sys/class/gpio/gpio' + str (pin ) + '/value' , 'w' ) as f :
78
- f .write ('0' )
79
-
80
- def summarize_analog_data ():
81
- import os , time
82
-
83
- # TODO: Figure out how the byte rounding works below to conserve RAM
84
- filename = '/var/log/ana.log'
85
- try :
86
- f = open (filename , 'r' )
87
- except :
88
- return [[0 ],[0 ],[0 ],[0 ]]
89
- if (os .stat (filename ).st_size < 10000 ):
90
- return [[0 ],[0 ],[0 ],[0 ]]
91
- f .seek (- 10000 , 2 ) # seek 10000 bytes before the end of the file
92
- data = f .readlines (100000 )[- 100 :- 1 ] # try to read 100000 bytes
93
- f .close ()
94
- cur_time = time .time ()
95
- times = [str (float (line .strip ().split (',' )[0 ]) - cur_time ) for line in data ]
96
- r1 = [line .strip ().split (',' )[1 ] for line in data ]
97
- r2 = [line .strip ().split (',' )[2 ] for line in data ]
98
- r3 = [line .strip ().split (',' )[3 ] for line in data ]
99
- r4 = [line .strip ().split (',' )[4 ] for line in data ]
100
-
101
- p1 = ['[' + str (t ) + ', ' + str (r ) + ']' for t , r in zip (times , r1 )]
102
- p2 = ['[' + str (t ) + ', ' + str (r ) + ']' for t , r in zip (times , r2 )]
103
- p3 = ['[' + str (t ) + ', ' + str (r ) + ']' for t , r in zip (times , r3 )]
104
- p4 = ['[' + str (t ) + ', ' + str (r ) + ']' for t , r in zip (times , r4 )]
105
-
106
- summary = ('[' + ',' .join (p1 ) + ']' , '[' + ',' .join (p2 ) + ']' , '[' + ',' .join (p3 ) + ']' , '[' + ',' .join (p4 ) + ']' )
107
- return summary
97
+ def spiRead ():
98
+ pass
108
99
109
- def analogger ():
110
- from time import sleep , time
111
- while (1 ):
112
- reading0 = str (float (read_analog ('A0' )) * 3.3 / 1024.0 )
113
- reading1 = str (float (read_analog ('A1' )) * 3.3 / 1024.0 )
114
- reading2 = str (float (read_analog ('A2' )) * 3.3 / 1024.0 )
115
- reading3 = str (float (read_analog ('A3' )) * 3.3 / 1024.0 )
116
- f = open ('/var/log/ana.log' , 'a' )
117
- f .write (',' .join ([str (time ()), reading0 , reading1 , reading2 , reading3 ]) + '\n ' )
118
- f .close ()
119
- sleep (1 )
100
+ def spiWrite (val ):
101
+ pass
0 commit comments