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 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 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 Init (clock = SPICLK, miso = SPIMISO, mosi = SPIMOSI, cs = SPICS, min_channel = 0, max_channel = 2, vref = VREF, apply_voltage_divider = APPLY_VOLTAGE_DIVIDER, r = RESISTANCES): ''' clock: indicates clock pin miso: indicates master input slave output pin mosi: indicates master input slave output pin cs: chip selection pin min_channel: indicates the minimum channel where receive the analog inputs min_channel: indicates the maximum channel where receive the analog inputs apply_voltage_divider: indicates wether to apply the voltage divider in the calc of the read voltage r1: value of the R1 resistance in the voltage divider r2: value of the R2 resistance in the voltage divider ''' ADC = {} ADC['clk'] = clock ADC['miso'] = miso ADC['mosi'] = mosi ADC['cs'] = cs ADC['vref'] = vref ADC['voltage_div'] = apply_voltage_divider ADC['r'] = r ADC['error'] = (False, None) if ((max_channel > 7) or (min_channel < 0)): ADC['error'] = (True, "Unable to create the ADC. The values for the channels are not in the range (0,7)") return ADC ADC['maxch'] = max_channel ADC['minch'] = min_channel # set up the SPI interface pins GPIO.init () GPIO.setcfg(mosi, GPIO.OUT) GPIO.setcfg(miso, GPIO.IN) GPIO.setcfg(clock, GPIO.OUT) GPIO.setcfg(cs, GPIO.OUT) return ADC
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 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 setup_leds(): global RED_LED global GREEN_LED global YELLOW_LED RED_LED = GPIO.PG5 GREEN_LED = GPIO.PG1 YELLOW_LED = GPIO.PG2 GPIO.init() GPIO.setcfg(RED_LED, GPIO.OUT) GPIO.setcfg(GREEN_LED, GPIO.OUT) GPIO.setcfg(YELLOW_LED, GPIO.OUT) NOK_ON() OK_ON() STATUS_ON() time.sleep(1) NOK_OFF() OK_OFF()
def initLcd(): # setup for d in DATA_IO: GPIO.setcfg(d, GPIO.OUT) GPIO.output(d, GPIO.LOW) GPIO.setcfg(LCD_RD, GPIO.OUT) GPIO.setcfg(LCD_WR, GPIO.OUT) GPIO.setcfg(LCD_RS, GPIO.OUT) GPIO.setcfg(LCD_CS, GPIO.OUT) GPIO.setcfg(LCD_REST, GPIO.OUT) GPIO.output(LCD_RD, GPIO.HIGH) GPIO.output(LCD_WR, GPIO.HIGH) GPIO.output(LCD_RS, GPIO.HIGH) GPIO.output(LCD_CS, GPIO.HIGH) GPIO.output(LCD_REST, GPIO.HIGH) # init GPIO.output(LCD_REST, GPIO.HIGH) time.sleep(0.005) GPIO.output(LCD_REST, GPIO.LOW) time.sleep(0.015) GPIO.output(LCD_REST, GPIO.HIGH) time.sleep(0.015) GPIO.output(LCD_CS, GPIO.HIGH) GPIO.output(LCD_WR, GPIO.HIGH) GPIO.output(LCD_CS, GPIO.LOW) #CS Lcd_Write_Com(0xCB) Lcd_Write_Data(0x39) Lcd_Write_Data(0x2C) Lcd_Write_Data(0x00) Lcd_Write_Data(0x34) Lcd_Write_Data(0x02) Lcd_Write_Com(0xCF) Lcd_Write_Data(0x00) Lcd_Write_Data(0XC1) Lcd_Write_Data(0X30) Lcd_Write_Com(0xE8) Lcd_Write_Data(0x85) Lcd_Write_Data(0x00) Lcd_Write_Data(0x78) Lcd_Write_Com(0xEA) Lcd_Write_Data(0x00) Lcd_Write_Data(0x00) Lcd_Write_Com(0xED) Lcd_Write_Data(0x64) Lcd_Write_Data(0x03) Lcd_Write_Data(0X12) Lcd_Write_Data(0X81) Lcd_Write_Com(0xF7) Lcd_Write_Data(0x20) Lcd_Write_Com(0xC0) #Power control Lcd_Write_Data(0x23) #VRH[5:0] Lcd_Write_Com(0xC1) #Power control Lcd_Write_Data(0x10) #SAP[2:0];BT[3:0] Lcd_Write_Com(0xC5) #VCM control Lcd_Write_Data(0x3e) #Contrast Lcd_Write_Data(0x28) Lcd_Write_Com(0xC7) #VCM control2 Lcd_Write_Data(0x86) #-- Lcd_Write_Com(0x36) # Memory Access Control Lcd_Write_Data(0x48) Lcd_Write_Com(0x3A) Lcd_Write_Data(0x55) Lcd_Write_Com(0xB1) Lcd_Write_Data(0x00) Lcd_Write_Data(0x18) Lcd_Write_Com(0xB6) # Display Function Control Lcd_Write_Data(0x08) Lcd_Write_Data(0x82) Lcd_Write_Data(0x27) Lcd_Write_Com(0x11) # Exit Sleep time.sleep(0.120) Lcd_Write_Com(0x29) # Display on Lcd_Write_Com(0x2c)
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)
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) DATA = GPIO.PG GPIO.init() for led in RED_LEDS: GPIO.setcfg(led, GPIO.OUT) for i in range(0, 256): GPIO.outputBank(DATA, i) time.sleep(0.02)
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 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 setData(b): GPIO.outputBank(DATA, b)
def OK_OFF(): GPIO.output(GREEN_LED, GPIO.LOW)
def STATUS_OFF(): GPIO.output(YELLOW_LED, GPIO.LOW)
def left(): GPIO.output(turn_left, 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 back(): GPIO.output(turn_back, GPIO.HIGH)
def forward(): GPIO.output(turn_foward, 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 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 deinit_pins(self): if Led.PIN_INITED and Led.PIN_DEINITED == False : GPIO.cleanup() Led.PIN_INITED = False Led.PIN_DEINITED = True
def send_command(self, cmd): GPIO.output(self.stb,GPIO.LOW) self.send_byte(cmd) GPIO.output(self.stb,GPIO.HIGH)
#!/usr/bin/env python import SUNXI_GPIO as GPIO turn_foward = GPIO.PD0 turn_back = GPIO.PD1 turn_left = GPIO.PD2 turn_right = GPIO.PD3 steer_up = GPIO.PD5 steer_down = GPIO.PD6 steer_left = GPIO.PD8 steer_right = GPIO.PD9 steer_center = GPIO.PD4 GPIO.init() GPIO.setcfg(turn_foward, GPIO.OUT) GPIO.setcfg(turn_back, GPIO.OUT) GPIO.setcfg(turn_left, GPIO.OUT) GPIO.setcfg(turn_right, GPIO.OUT) GPIO.setcfg(steer_up, GPIO.OUT) GPIO.setcfg(steer_down, GPIO.OUT) GPIO.setcfg(steer_left, GPIO.OUT) GPIO.setcfg(steer_right, GPIO.OUT) GPIO.setcfg(steer_center, GPIO.OUT) 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 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 NOK_ON(): GPIO.output(RED_LED, GPIO.HIGH)
def STATUS_ON(): GPIO.output(YELLOW_LED, GPIO.HIGH)
def OK_ON(): GPIO.output(GREEN_LED, GPIO.HIGH)
res = "" for list in ans.json()[u'list']: res = res +'\n' + list[u'flight']+ ' ' +list[u'route']+ ' ' +list[u'startime']+ ' ' +list[u'endtime']+ ' ' +list[u'state']+ ' ' +list[u'detailurl']+'\n' return ans.json()[u'text']+'\n'+res elif ans.json()['code'] == 308000: res = "" for list in ans.json()[u'list']: res = res +'\n' + list[u'name']+ ' ' +list[u'info']+ ' ' +list[u'detailurl']+'\n' return ans.json()[u'text']+'\n'+res 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)
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 NOK_OFF(): GPIO.output(RED_LED, GPIO.LOW)
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 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 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 deinit_pins(): GPIO.cleanup()
Lcd_Write_Com(0x2a) Lcd_Write_Data(x1 >> 8) Lcd_Write_Data(x1) Lcd_Write_Data(x2 >> 8) Lcd_Write_Data(x2) Lcd_Write_Com(0x2b) Lcd_Write_Data(y1 >> 8) Lcd_Write_Data(y1) Lcd_Write_Data(y2 >> 8) Lcd_Write_Data(y2) Lcd_Write_Com(0x2c) 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) GPIO.init() initLcd() LCD_Clear(0xf800) LCD_Clear(0x07E0) LCD_Clear(0x001F)