Interfacing with Python
From Pinguino-Wiki
Contents |
Example - USB
On Pinguino
/* */ void setup() { } void loop() { USB.send("hello", 5); delay(1000); }
On Computer Side
#!/usr/bin/env python # import usb busses = usb.busses() # Search pinguino between all the usb devices for bus in busses: devices = bus.devices for dev in devices: if dev.idVendor==0x04d8 and dev.idProduct==0xfeaa: pingu = dev # Get a device handler for th usb device dh = pingu.open() # Set configuration 3 an interface 0 dh.setConfiguration(3) dh.claimInterface(0) # Read 5 bytes in Bulk mode, convert them to # a string and print it while 1 == 1: cadena = "" for i in dh.bulkRead(0x82, 5, 10000): cadena += chr(i) print cadena
Example - CDC
Using Pynguino, a Python package that provides an interface to the Pinguino development board.
On Pinguino
On Computer Side
#! /usr/bin/python #-*- coding: utf-8 -*- #This is a simple example to blink 2 leds #Yeison Cardona <yeison.eng@gmail.com> from pynguino import PinguinoProcessing import time pinguino=PinguinoProcessing() pinguino.RecursiveConect() led1=1 led2=7 tempo=0.2 pinguino.pinMode(led1,'output') pinguino.pinMode(led2,'output') while True: time.sleep(tempo) pinguino.digitalWrite(led1,"high") pinguino.digitalWrite(led2,"low") time.sleep(tempo) pinguino.digitalWrite(led1,"low") pinguino.digitalWrite(led2,"high")
More detalis in: author's site (spanish)
Example - ds18b20
On Pinguino
/* Pinguino example to read ds18b20 1wire temperature sensor Result is sent on USB bus and can be read with temp18b20.py author Régis Blanchot first release 14/09/2010 last update 06/10/2010 IDE Pinguino b9.2 or sup. DS18B20 Connection ------------------ pin 1: GND pin 2: DQ (Data in/out) must be connected to the PIC pin 3: VDD (+5V) NB : 1-wire bus (DQ line) must have 4K7 pull-up resistor (connected to +5V) */ #define ONEWIREBUS 0 // 1-wire bus is on pin 0 (RB0), just change it to suit you #define RUNLED PORTAbits.RA4 // for visual debug purposes void setup() { // // find all sensors on the bus and get their Rom Code // //DS18B20.find(ONEWIREBUS); } void loop() { TEMPERATURE t; u8 temp[4]; // if you want to read temperature from sensor #1 with max. resolution : // if (DS18B20.read(ONEWIREBUS, 1, RES12BIT, &t)) // if you have only one sensor on the bus, just skip rom detection if (DS18B20.read(ONEWIREBUS, SKIPROM, RES12BIT, &t)) { // send temperature on usb bus temp[0] = t.sign; // t.sign is unsigned char (1 byte) temp[1] = t.integer; // t.integer is unsigned char (1 byte) temp[2] = high8(t.fraction); // t.fraction is unsigned int (2 bytes) temp[3] = low8(t.fraction); USB.send(temp, 4); // send 4-bit temp on usb bus RUNLED = RUNLED ^ 1; // blinked led for visual debug delay(1000); // wait for 1 sec. before next reading } }
On Computer Side
#! /usr/bin/python # -*- coding: utf-8 -*- #------------------------------------------------------------------------------- # File: temp18b20.py # # display time and temperature # temperature is sent by pinguino device though usb bus # # regis blanchot, september 2010 # # rblanchot@gmail.com # #------------------------------------------------------------------------------- import usb # requires pyusb available at https://sourceforge.net/projects/pyusb/files/ import sys import time import Tkinter # requires python-tk (apt-get install python-tk) from Tkinter import * curtime = '' curtemp = '' timeline = '' templine = '' #------------------------------------------------------------------------------- # Program window #------------------------------------------------------------------------------- class Window: def __init__(self,root): self.root = root self.canvas = Tkinter.Canvas(self.root) self.canvas.pack() self.canvas.config(background='black') self.canvas.config(width=400) self.canvas.config(height=200) #------------------------------------------------------------------------------- # Pinguino Class by Marin Purgar (marin.purgar@gmail.com) #------------------------------------------------------------------------------- class Pinguino(): VENDOR = 0x04D8 PRODUCT = 0xFEAA CONFIGURATION = 3 INTERFACE = 0 ENDPOINT_IN = 0x82 ENDPOINT_OUT = 0x01 device = None handle = None def __init__(self,): for bus in usb.busses(): for dev in bus.devices: if dev.idVendor == self.VENDOR and dev.idProduct == self.PRODUCT: self.device = dev return None def open(self): if not self.device: print >> sys.stderr, "Unable to find device!" return None try: self.handle = self.device.open() self.handle.setConfiguration(self.CONFIGURATION) self.handle.claimInterface(self.INTERFACE) except usb.USBError, err: print >> sys.stderr, err self.handle = None return self.handle def close(self): try: self.handle.releaseInterface() except Exception, err: print >> sys.stderr, err self.handle, self.device = None, None def read(self, length, timeout = 0): return self.handle.bulkRead(self.ENDPOINT_IN, length, timeout) def write(self, buffer, timeout = 0): return self.handle.bulkWrite(self.ENDPOINT_OUT, buffer, timeout) #------------------------------------------------------------------------------- # update #------------------------------------------------------------------------------- def update(): INTERVAL = 100 global curtime global curtemp global timeline global templine deg = unichr(176).encode("utf-8") t = '' # get temperature try: t = pinguino.read(4, INTERVAL) except usb.USBError as err: pass if len(t) > 0: #print 't =',t # debug if t[0] == 1: sign = '-' else: sign = '' newtemp = '%s%d%s%d%s%s' % (sign,t[1], '.', t[2]*256+t[3], deg, 'C') if newtemp != curtemp: curtemp = newtemp if templine: Temp18b20.canvas.delete(templine) templine = Temp18b20.canvas.create_text(200, 150, text=curtemp, font=("Helvetica", "48", "bold"), fill='red') # get time newtime = time.strftime('%H:%M:%S') if newtime != curtime: curtime = newtime if timeline: Temp18b20.canvas.delete(timeline) timeline = Temp18b20.canvas.create_text(200, 050, text=curtime, font=("Helvetica", "24", "bold"), fill='red') # recall every 500ms Temp18b20.canvas.after(INTERVAL, update) #------------------------------------------------------------------------------- # main #------------------------------------------------------------------------------- if __name__ == "__main__": root = Tkinter.Tk() root.title('Temp18B20') Temp18b20 = Window(root) pinguino = Pinguino() if pinguino.open() == None: print >> sys.stderr, "Unable to open Pinguino device!" sys.exit() update() root.mainloop() pinguino.close()

