def ReadByte(): byte = 0 for i in range(8): time.sleep(0.000001) if pol == 0: GPIO.output(SCK, HIGH) else: GPIO.output(SCK, LOW) if pha == 0: if GPIO.input(MISO) == 1: if pol == 0: byte |= 1 else: byte |= 0 else: if pol == 0: byte |= 0 else: byte |= 1 if i != 7: byte <<= 1 time.sleep(0.000001) GPIO.output(SCK, LOW) if pha == 1: if GPIO.input(MISO) == 1: if pol == 0: byte |= 1 else: byte |= 0 else: if pol == 0: byte |= 0 else: byte |= 1 if i != 7: byte <<= 1 return byte
def ReadByte(): byte = 0 for i in range(8): time.sleep(0.000001) if pol == 0: GPIO.output(SCK, HIGH) else: GPIO.output(SCK, LOW) if pha == 0: if GPIO.input(MISO) == 1: if pol == 0: byte |= 1 else: byte |= 0 else: if pol == 0: byte |= 0 else: byte |= 1 if i != 7: byte <<= 1 time.sleep(0.000001) GPIO.output(SCK, LOW) if pha == 1: if GPIO.input(MISO) == 1: if pol == 0: byte |= 1 else: byte |= 0 else: if pol == 0: byte |= 0 else: byte |= 1 if i != 7: byte <<= 1 return byte;
def receive(self): temp = 0 GPIO.setup(self.dio, GPIO.IN, pull_up_down=GPIO.PUD_UP) for i in range(8): temp >>= 1 GPIO.output(self.clk, GPIO.LOW) if GPIO.input(self.dio): temp |= 0x80 GPIO.output(self.clk, GPIO.HIGH) GPIO.setup(self.dio, GPIO.OUT) return temp
def receive(self): temp = 0 GPIO.setup(self.dio, GPIO.IN, pull_up_down=GPIO.PUD_UP) for i in range(8): temp >>= 1 GPIO.output(self.clk,GPIO.LOW) if GPIO.input(self.dio): temp |= 0x80 GPIO.output(self.clk,GPIO.HIGH) GPIO.setup(self.dio, GPIO.OUT) return temp
def readadc(ADC): ''' Read adc data from specified channels ''' # ADC inputs readings = {} cell = 0 prev_cell_values = 0 for channel in range(ADC['minch'], ADC['maxch'] + 1): # Start the communication with the the device # If the device was powered up with the CS line low, it must be # brought high and back low to initiate communication. # So always here, first the CS signal is put high and then put low. GPIO.output(ADC['cs'], True) # Perform the start signal GPIO.output(ADC['clk'], False) GPIO.output(ADC['cs'], False) # Set the command # Start and single bits set # Examples: let channel 7, 7 | 0x18 = 0x1F = 11111 # Start Single/Diff D2 D1 D0 # 1 1 1 1 1 # let channel 5, 5 | 0x18 = 0x1D = 11101 # Start Single/Diff D2 D1 D0 # 1 1 1 0 1 command = 0 command = channel | 0x18 for i in range (5): # Check most significant bit of the five if (command & 0x10): GPIO.output(ADC['mosi'], True) else: GPIO.output(ADC['mosi'], False) # Shift left the command to send the next bit command <<= 1 GPIO.output(ADC['clk'], True) GPIO.output(ADC['clk'], False) adcout = 0 # read one empty bit, one null bit and 10 ADC data bits for i in range(12): GPIO.output(ADC['clk'], True) GPIO.output(ADC['clk'], False) data = GPIO.input(ADC['miso']) # Shift left the reading from the ADC to set the next bit adcout <<= 1 # Introduce the received bit adcout |= data GPIO.output(ADC['cs'], True) # Set the reading of the corresponding channel # The less significant bit is a meaningless bit (NULL bit) adcout >>= 1 if ADC['voltage_div']: print "adcout = %s" % adcout # Read voltage from the voltage divider vin = (adcout * ADC['vref']) / 1024.0 # Read voltage from the battery r1 = ADC['r'][cell][0] r2 = ADC['r'][cell][1] value = (vin * (r1 + r2)) / r1 # Voltage of the corresponding cell readings[cell] = (value - prev_cell_values, percentage (value - prev_cell_values)) prev_cell_values = value else: # Read voltage from the voltage divider vin = (adcout * ADC['vref']) / 1024.0 readings[cell] = ((adcout * ADC['vref']) / 1024.0, 100.0) cell +=1 return readings
ans = requests.get(tts_server + '?tex=' + tex + '&lan=zh&cuid=112233445566&ctp=1&tok=' + access_token) audio = open('./tmp.mp3', 'wb') audio.write(ans.content) GPIO.init() BUTTON = GPIO.PD2 LED = GPIO.PD1 GPIO.setcfg(BUTTON, GPIO.IN) GPIO.setcfg(LED, GPIO.OUT) r = 0 print "Ready" while True: state = GPIO.input(BUTTON) if state == GPIO.HIGH and r == 0: child = subprocess.Popen( "arecord -D \"plughw:1,0\" -r 16000 -c 1 -f S16_LE ./tmp.wav", shell=True) r = 1 elif state == GPIO.LOW and r == 1: print child.pid time.sleep(0.2) subprocess.call("kill " + str(child.pid + 1), shell=True) #subprocess.call("python wav.py",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) rs = baidu_asr('./tmp.wav') #print type(rs[u'err_no']) if rs[u'err_no'] == 0: vtt = ''.join(rs[u'result']) if u'开灯' in vtt:
def tts(tex): ans = requests.get(tts_server+'?tex='+tex+'&lan=zh&cuid=112233445566&ctp=1&tok='+access_token) audio = open('./tmp.mp3','wb') audio.write(ans.content) GPIO.init() BUTTON = GPIO.PD2 LED = GPIO.PD1 GPIO.setcfg(BUTTON, GPIO.IN) GPIO.setcfg(LED, GPIO.OUT) r = 0 print "Ready" while True: state = GPIO.input(BUTTON) if state == GPIO.HIGH and r == 0 : child = subprocess.Popen("arecord -D \"plughw:1,0\" -r 16000 -c 1 -f S16_LE ./tmp.wav",shell=True) r = 1 elif state == GPIO.LOW and r == 1 : print child.pid time.sleep(0.2) subprocess.call("kill "+str(child.pid+1), shell=True) r = 0 #subprocess.call("python wav.py ./tmp.wav",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) rs = baidu_asr('./tmp.wav') #print type(rs[u'err_no']) if rs[u'err_no'] == 0: vtt = ''.join(rs[u'result']) if u'开灯' in vtt: GPIO.output(LED, GPIO.HIGH)