def Lcd_Write_Data(d): GPIO.output(LCD_RS, GPIO.HIGH) GPIO.output(LCD_CS, GPIO.LOW) setData(d) GPIO.output(LCD_WR, GPIO.LOW) GPIO.output(LCD_WR, GPIO.HIGH) GPIO.output(LCD_CS, GPIO.HIGH)
def write(*args): pol = (MODE >> 1) & 1 pha = MODE & 1 def SendByte(byte): for i in range(8): if pha == 0: if byte & 0x80: if pol == 0: GPIO.output(MOSI, HIGH) else: GPIO.output(MOSI, LOW) else: if pol == 0: GPIO.output(MOSI, LOW) else: GPIO.output(MOSI, HIGH) time.sleep(0.000001) if pol == 0: GPIO.output(SCK, HIGH) else: GPIO.output(SCK, LOW) if pha == 1: if byte & 0x80: if pol == 0: GPIO.output(MOSI, HIGH) else: GPIO.output(MOSI, LOW) else: if pol == 0: GPIO.output(MOSI, LOW) else: GPIO.output(MOSI, HIGH) time.sleep(0.000001) if pol == 0: GPIO.output(SCK, LOW) else: GPIO.output(SCK, HIGH) byte <<= 1 GPIO.output(CS, LOW) time.sleep(0.000001) for i in range(len(args)): SendByte(args[i]) time.sleep(0.000001) GPIO.output(CS, HIGH) return
def get_buttons(self): keys = 0 GPIO.output(self.stb, GPIO.LOW) self.send_byte(0x42) for i in range(4): keys |= self.receive() << i GPIO.output(self.stb, GPIO.HIGH) return keys
def get_buttons(self): keys = 0 GPIO.output(self.stb,GPIO.LOW) self.send_byte(0x42) for i in range(4): keys |= self.receive() << i GPIO.output(self.stb,GPIO.HIGH) return keys
def write_pin(pin, value): global pins, pin_values if pin not in pins : print "Fail to write pin which has not been inited ever!" sys.exit(1) if value < 0 or value > len(pin_values): print "Fail to write pin value which is not LOW /HIGH !" sys.exit(1) GPIO.output(pin, pin_values[value])
def LCD_Clear(j): Address_set(0, 0, 240, 320) GPIO.output(LCD_CS, GPIO.LOW) for i in range(0, 240): for m in range(0, 320): Lcd_Write_Data(j >> 8) Lcd_Write_Data(j) GPIO.output(LCD_CS, GPIO.HIGH)
def init_pins(all_pins): #init module GPIO.init() for pin in all_pins: #configure module cfgs = all_pins[pin] GPIO.setcfg(pin, cfgs[0]) config = GPIO.getcfg(cfgs[0]) GPIO.output(pin, cfgs[1]) GPIO.output(mr_pin, GPIO.HIGH)
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 send_char_by_74ls164(self, ch, chip_sel=-1): global CLEAR_CHAR, CLEAR_LED_CHAR if ch == CLEAR_CHAR : led_char = CLEAR_LED_CHAR elif ch not in self.ascii_table: print "char should be [0-9] or [a-z.]!" sys.exit(1) else: led_char = self.ascii_table[ch] #print "send char : %d, led value: 0x%x, chip_sel: %d" % (ch, led_char, chip_sel ) if chip_sel == -1: chips = self.led_selector elif chip_sel < len(self.led_selector): chips = [self.led_selector[chip_sel]] else: print "wrong led chip selector!" sys.exit(1) for sel in chips: GPIO.output(mr_pin, GPIO.HIGH) self.selector_led(sel) self.show_led_char(led_char) GPIO.output(mr_pin, GPIO.LOW)
def motor_step(motor_id, dir, steps): clock_wise = GPIO.HIGH anti_clock_wise = GPIO.LOW if dir: GPIO.output(GPIOS[motor_id * 2], clock_wise) else: GPIO.output(GPIOS[motor_id * 2], anti_clock_wise) time.sleep(0.05) for i in range(0, steps): GPIO.output(GPIOS[motor_id * 2 + 1], GPIO.HIGH) time.sleep(0.001) GPIO.output(GPIOS[motor_id * 2 + 1], GPIO.LOW) time.sleep(0.001)
def enable(self, intensity=7): GPIO.init() GPIO.setcfg(self.dio, GPIO.OUT) GPIO.setcfg(self.clk, GPIO.OUT) GPIO.setcfg(self.stb, GPIO.OUT) GPIO.output(self.stb,GPIO.HIGH) GPIO.output(self.clk,GPIO.HIGH) self.send_command(0x40) self.send_command(0x80 | 8 | min(7, intensity)) GPIO.output(self.stb,GPIO.LOW) self.send_byte(0xC0) for i in range(16): self.send_byte(0x00) GPIO.output(self.stb,GPIO.HIGH)
def enable(self, intensity=7): GPIO.init() GPIO.setcfg(self.dio, GPIO.OUT) GPIO.setcfg(self.clk, GPIO.OUT) GPIO.setcfg(self.stb, GPIO.OUT) GPIO.output(self.stb, GPIO.HIGH) GPIO.output(self.clk, GPIO.HIGH) self.send_command(0x40) self.send_command(0x80 | 8 | min(7, intensity)) GPIO.output(self.stb, GPIO.LOW) self.send_byte(0xC0) for i in range(16): self.send_byte(0x00) GPIO.output(self.stb, GPIO.HIGH)
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 init(mode): print("Selected mode: " + str(mode)) if mode < 0 or mode > 3: raise ModeError(mode) global MODE MODE = mode GPIO.init() GPIO.setcfg(MOSI, OUT) GPIO.setcfg(MISO, IN) GPIO.setcfg(SCK, OUT) GPIO.setcfg(CS, OUT) if mode == 0 or mode == 1: GPIO.output(CS, HIGH) GPIO.output(SCK, LOW) GPIO.output(MOSI, LOW) else: GPIO.output(CS, HIGH) GPIO.output(SCK, HIGH) GPIO.output(MOSI, HIGH) return
def steering_up(): GPIO.output(steer_up, GPIO.HIGH)
def steer_reset(): GPIO.output(steer_up, GPIO.LOW) GPIO.output(steer_down, GPIO.LOW) GPIO.output(steer_left, GPIO.LOW) GPIO.output(steer_right, GPIO.LOW) GPIO.output(steer_center, GPIO.LOW)
def NOK_OFF(): GPIO.output(RED_LED, GPIO.LOW)
def steering_right(): GPIO.output(steer_right, GPIO.HIGH)
def steering_down(): GPIO.output(steer_down, GPIO.HIGH)
def forward(): GPIO.output(turn_foward, GPIO.HIGH)
def read(address, n): pol = (MODE >> 1) & 1 pha = MODE & 1 def SendByte(byte): for i in range(8): if pha == 0: if byte & 1: if pol == 0: GPIO.output(MOSI, HIGH) else: GPIO.output(MOSI, LOW) else: if pol == 0: GPIO.output(MOSI, LOW) else: GPIO.output(MOSI, HIGH) time.sleep(0.000001) if pol == 0: GPIO.output(SCK, HIGH) else: GPIO.output(SCK, LOW) if pha == 1: if byte & 1: if pol == 0: GPIO.output(MOSI, HIGH) else: GPIO.output(MOSI, LOW) else: if pol == 0: GPIO.output(MOSI, LOW) else: GPIO.output(MOSI, HIGH) time.sleep(0.000001) if pol == 0: GPIO.output(SCK, LOW) else: GPIO.output(SCK, HIGH) byte >>= 1 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; GPIO.output(CS, LOW) time.sleep(0.000001) SendByte(address) args = [] for i in range(n): args.append(ReadByte()) time.sleep(0.000001) GPIO.output(CS, HIGH) return args
def init(mode): print ("Selected mode: " + str(mode)) if mode < 0 or mode > 3: raise ModeError(mode); global MODE MODE = mode GPIO.init() GPIO.setcfg(MOSI, OUT) GPIO.setcfg(MISO, IN) GPIO.setcfg(SCK, OUT) GPIO.setcfg(CS, OUT) if mode == 0 or mode == 1: GPIO.output(CS, HIGH) GPIO.output(SCK, LOW) GPIO.output(MOSI, LOW) else: GPIO.output(CS, HIGH) GPIO.output(SCK, HIGH) GPIO.output(MOSI, HIGH) return
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) print "turn on the light" tts("灯已打开") subprocess.call("lame --decode tmp.mp3 tmp.wav &>/dev/null", shell=True) subprocess.call("python wav.py ./tmp.wav",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) subprocess.call("rm tmp.mp3 tmp.wav",shell=True) continue elif u'关灯' in vtt: GPIO.output(LED, GPIO.LOW) print "turn off the light" tts("灯已关闭") subprocess.call("lame --decode tmp.mp3 tmp.wav &>/dev/null", shell=True) subprocess.call("python wav.py ./tmp.wav",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) subprocess.call("rm tmp.mp3 tmp.wav",shell=True) continue elif u'播放音乐' in vtt:
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: GPIO.output(LED, GPIO.HIGH) print "turn on the light" elif u'关灯' in vtt: GPIO.output(LED, GPIO.LOW) print "turn off the light" elif u'语音记事本' in vtt: tts("语音记事按下按钮后开始") subprocess.call("lame --decode tmp.mp3 tmp.wav &>/dev/null", shell=True) subprocess.call("python wav.py", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) subprocess.call("rm tmp.mp3 tmp.wav", shell=True) while True: if GPIO.input(BUTTON) == GPIO.HIGH:
import SUNXI_GPIO as GPIO import time RED_LEDS = (GPIO.PG0, GPIO.PG1, GPIO.PG2, GPIO.PG3, GPIO.PG4, GPIO.PG5, GPIO.PG6, GPIO.PG7, GPIO.PC19, GPIO.PC20, GPIO.PC21, GPIO.PC22, GPIO.PB18, GPIO.PB19) GPIO.init() for led in RED_LEDS: GPIO.setcfg(led, GPIO.OUT) GPIO.output(led, GPIO.LOW)
def steering_center(): GPIO.output(steer_center, GPIO.HIGH)
def read(address, n): pol = (MODE >> 1) & 1 pha = MODE & 1 def SendByte(byte): for i in range(8): if pha == 0: if byte & 1: if pol == 0: GPIO.output(MOSI, HIGH) else: GPIO.output(MOSI, LOW) else: if pol == 0: GPIO.output(MOSI, LOW) else: GPIO.output(MOSI, HIGH) time.sleep(0.000001) if pol == 0: GPIO.output(SCK, HIGH) else: GPIO.output(SCK, LOW) if pha == 1: if byte & 1: if pol == 0: GPIO.output(MOSI, HIGH) else: GPIO.output(MOSI, LOW) else: if pol == 0: GPIO.output(MOSI, LOW) else: GPIO.output(MOSI, HIGH) time.sleep(0.000001) if pol == 0: GPIO.output(SCK, LOW) else: GPIO.output(SCK, HIGH) byte >>= 1 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 GPIO.output(CS, LOW) time.sleep(0.000001) SendByte(address) args = [] for i in range(n): args.append(ReadByte()) time.sleep(0.000001) GPIO.output(CS, HIGH) return args
def send_data(self, addr, data): self.send_command(0x44) GPIO.output(self.stb,GPIO.LOW) self.send_byte(0xC0 | addr) self.send_byte(data) GPIO.output(self.stb,GPIO.HIGH)
def SendByte(byte): for i in range(8): if pha == 0: if byte & 1: if pol == 0: GPIO.output(MOSI, HIGH) else: GPIO.output(MOSI, LOW) else: if pol == 0: GPIO.output(MOSI, LOW) else: GPIO.output(MOSI, HIGH) time.sleep(0.000001) if pol == 0: GPIO.output(SCK, HIGH) else: GPIO.output(SCK, LOW) if pha == 1: if byte & 1: if pol == 0: GPIO.output(MOSI, HIGH) else: GPIO.output(MOSI, LOW) else: if pol == 0: GPIO.output(MOSI, LOW) else: GPIO.output(MOSI, HIGH) time.sleep(0.000001) if pol == 0: GPIO.output(SCK, LOW) else: GPIO.output(SCK, HIGH) byte >>= 1
def OK_OFF(): GPIO.output(GREEN_LED, GPIO.LOW)
import SUNXI_GPIO as GPIO import time RED_LED = GPIO.PD0 GPIO.init() GPIO.setcfg(RED_LED, GPIO.OUT) while True: GPIO.output(RED_LED, GPIO.HIGH) print 'high' time.sleep(1) GPIO.output(RED_LED, GPIO.LOW) print 'low' time.sleep(1)
def STATUS_OFF(): GPIO.output(YELLOW_LED, GPIO.LOW)
def send_command(self, cmd): GPIO.output(self.stb,GPIO.LOW) self.send_byte(cmd) GPIO.output(self.stb,GPIO.HIGH)
def steering_left(): GPIO.output(steer_left, GPIO.HIGH)
def send_byte(self, data): for i in range(8): GPIO.output(self.clk,GPIO.LOW) GPIO.output(self.dio, (data & 1) == 1) data >>= 1 GPIO.output(self.clk,GPIO.HIGH)
def left(): GPIO.output(turn_left, GPIO.HIGH)
def STATUS_ON(): GPIO.output(YELLOW_LED, GPIO.HIGH)
def reset(): GPIO.output(turn_foward, GPIO.LOW) GPIO.output(turn_back, GPIO.LOW) GPIO.output(turn_left, GPIO.LOW) GPIO.output(turn_right, GPIO.LOW)
def NOK_ON(): GPIO.output(RED_LED, GPIO.HIGH)
def OK_ON(): GPIO.output(GREEN_LED, GPIO.HIGH)
def back(): GPIO.output(turn_back, GPIO.HIGH)
def send_data(self, addr, data): self.send_command(0x44) GPIO.output(self.stb, GPIO.LOW) self.send_byte(0xC0 | addr) self.send_byte(data) GPIO.output(self.stb, GPIO.HIGH)
def right(): GPIO.output(turn_right, GPIO.HIGH)
import SUNXI_GPIO as GPIO import time RED_LEDS = (GPIO.PG0, GPIO.PG1, GPIO.PG2, GPIO.PG3, GPIO.PG4, GPIO.PG5, GPIO.PG6, GPIO.PG7, GPIO.PC19, GPIO.PC20, GPIO.PC21, GPIO.PC22, GPIO.PB18, GPIO.PB19) GPIO.init() GPIO.setcfg(RED_LEDS[0], GPIO.OUT) old = time.time() state = GPIO.LOW for i in range(0, 100000): GPIO.output(RED_LEDS[0], state) time.sleep(0.1) if state == GPIO.LOW: state = GPIO.HIGH else: state = GPIO.LOW print("time: %f" % (time.time() - old))
def send_command(self, cmd): GPIO.output(self.stb, GPIO.LOW) self.send_byte(cmd) GPIO.output(self.stb, GPIO.HIGH)
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
def send_byte(self, data): for i in range(8): GPIO.output(self.clk, GPIO.LOW) GPIO.output(self.dio, (data & 1) == 1) data >>= 1 GPIO.output(self.clk, GPIO.HIGH)
import SUNXI_GPIO as GPIO import time RED_LEDS = (GPIO.PG0, GPIO.PG1, GPIO.PG2, GPIO.PG3, GPIO.PG4, GPIO.PG5, GPIO.PG6, GPIO.PG7, GPIO.PC19, GPIO.PC20, GPIO.PC21, GPIO.PC22, GPIO.PB18, GPIO.PB19) GPIO.init() for led in RED_LEDS: GPIO.setcfg(led, GPIO.OUT) GPIO.output(led, GPIO.HIGH) time.sleep(0.3) GPIO.output(led, GPIO.LOW)