def read(self): GPIO.setup(self.__pin,GPIO.OUT) # send initial high self.__send_and_sleep(GPIO.HIGH, 0.05) # pull down to low self.__send_and_sleep(GPIO.LOW, 0.02) # change to input using pull up RPi.GPIO.setup(self.__pin,GPIO.IN,GPIO.PUD_UP) # collect data into an array data = self.__collect_input() # parse lengths of all data pull up periods pull_up_lengths = self.__parse_data_pull_up_lengths(data) # if bit count mismatch, return error (4 byte data + 1 byte checksum) if len(pull_up_lengths) != 40: return DHTResult(DHTResult.ERR_MISSING_DATA, 0, 0) # calculate bits from lengths of the pull up periods bits = self.__calculate_bits(pull_up_lengths) # we have the bits, calculate bytes the_bytes = self.__bits_to_bytes(bits) # calculate checksum and check checksum = self.__calculate_checksum(the_bytes) if the_bytes[4] != checksum: return DHTResult(DHTResult.ERR_CRC, 0, 0) # ok, we have valid data, return it return DHTResult(DHTResult.ERR_NO_ERROR, the_bytes[2], the_bytes[0])
def __init__(self, bus_id=0, exp_address=-1): self.exp_address = exp_address self.i2c_bus = smbus.SMBus(bus_id) self.sendcommand(0x38) self.sendcommand(0x39) self.sendcommand(0x14) #Internal OSC freq self.sendcommand(0x72) #Set contrast self.sendcommand(0x54) #Power/ICON control/Contrast set self.sendcommand(0x6F) #Follower control self.sendcommand(0x0C) #Display ON self.clear() self.backlight = GPIO('PA28', 'OUTPUT') return
def __init__(self): #Panel size size = 32, 32 #True time fonts self.extra_small_font = ImageFont.truetype("fonts/Ubuntu-B.ttf", 8) self.small_font = ImageFont.truetype("fonts/Ubuntu-B.ttf", 12) self.medium_font = ImageFont.truetype("fonts/Ubuntu-B.ttf", 14) self.large_font = ImageFont.truetype("fonts/Ubuntu-B.ttf", 24) self.arietta_button = GPIO('PC17', 'INPUT') self.scores_order = 1 #Create a 32x32 black image self.im = Image.new("RGB", size, "black")
def __collect_input(self): # collect the data while unchanged found unchanged_count = 0 # this is used to determine where is the end of the data max_unchanged_count = 100 last = -1 data = [] while True: current = GPIO.input(self.__pin) data.append(current) if last != current: unchanged_count = 0 last = current else: unchanged_count += 1 if unchanged_count > max_unchanged_count: break return data
import os from acmepins import GPIO from time import sleep ledIR = GPIO('J4.32','OUTPUT') #photo = GPIO('J4.34','INPUT') data_photo = 0 def mediane(L): L.sort() if len(L)%2: mid=len(L)/2 return (L[mid-1]+L[mid]) / 2.0 else: return(L[mid]) while True: ledIR.off() data_photo_mediane = [] for i in range(7): with open('/sys/bus/iio/devices/iio:device0/in_voltage0_raw') as f: data_photo = int ( f.read() ) data_photo_mediane.append( data_photo ) sleep(0.001) print data_photo_mediane data_photo = mediane(data_photo_mediane) print data_photo """ if data_photo > 512:
from acmepins import GPIO from time import sleep #FOX Board G20 example #led = GPIO('J7.3','OUTPUT') #Aria G25 example #led = GPIO('W9','OUTPUT') #Arietta G25 example led = GPIO('J4.40', 'OUTPUT') #Acqua A5 example #led = GPIO('J3.32','OUTPUT') while True: led.on() sleep(1) led.off() sleep(1)
from acmepins import GPIO from time import sleep import sys print "This is the name of the script: ", sys.argv[0] print "Number of arguments: ", len(sys.argv) print "The arguments are: " , str(sys.argv) #Roadrunner example led = GPIO(sys.argv[1],'OUTPUT') while True: led.on() sleep(1) led.off() sleep(1)
class KEYS(): KEY_NONE = 0 KEY_ESC = 1 KEY_LEFT = 2 KEY_RIGHT = 3 KEY_OK = 4 esc = None left = None right = None ok = None def __init__(self): self.esc = GPIO("J4.39", "INPUT") self.left = GPIO("J4.37", "INPUT") self.right = GPIO("J4.35", "INPUT") self.ok = GPIO("J4.33", "INPUT") def get(self): if self.esc.get_value() == 0: while self.esc.get_value() == 0: time.sleep(0.2) return self.KEY_ESC if self.left.get_value() == 0: while self.left.get_value() == 0: time.sleep(0.2) return self.KEY_LEFT if self.right.get_value() == 0: while self.right.get_value() == 0: time.sleep(0.2) return self.KEY_RIGHT if self.ok.get_value() == 0: while self.ok.get_value() == 0: time.sleep(0.2) return self.KEY_OK return self.KEY_NONE def hit(self): if self.esc.get_value() == 0 or self.left.get_value( ) == 0 or self.right.get_value() == 0 or self.ok.get_value() == 0: return True else: return False
signal.signal(signal.SIGTERM, sigterm_handler) print u"Initiated signals handlers" # Servers list #============== #servers = (("bbbforum", u"bf"), ("bbbdata", u"bd"), ("odroid", u"od"), ("skymule", u"sm"), ("domdroid", u"dd")) servers = (("bbbforum", u"bf"), ("odroid", u"od"), ("skymule", u"sm"), ("domdroid", u"dd")) # Pushbutton configuration #========================== #def pb_handler(): # v.setDisplay(duration=dim_delay) pb = GPIO("PA24", "INPUT") # update button #pb = GPIO("PC17", "INPUT") # on board PB #pb.set_edge("falling", pb_handler) #print u"Started PB edge detection" # Functions #=========== def check_wifi(): """Check if we have an IP address on wlan0. """ output = sub.check_output("/sbin/ifconfig wlan0 | grep inet\ addr | wc -l", shell=True) if output == "1\n": return True else: return False
from acmepins import GPIO from time import sleep import serial import time import sys import getopt import string import datetime import select import termios import sys import tty tasto = GPIO('PC12','INPUT') led_verde = GPIO('PC8','LOW') led_sim1 = GPIO('PC14','LOW') led_sim2 = GPIO('PC16','LOW') modem_power = GPIO('PC13','LOW') sim_select = GPIO('PA4','LOW') def menu(): print "Test ACME-0001 FoxBox mini" print "--------------------------" print "q) Leds ON" print "w) Leds OFF" print "a) Modem ON" print "s) Modem OFF" print "z) SIM 1" print "x) SIM 2"
class lcd(): i2c_bus = -1 lcd_address = 0x3E backlight = None # I2C expansion address can be: # PCF8574T 0x27 # PCF8574AT 0x3F exp_address = 0x27 def __init__(self, bus_id=0, exp_address=-1): self.exp_address = exp_address self.i2c_bus = smbus.SMBus(bus_id) self.sendcommand(0x38) self.sendcommand(0x39) self.sendcommand(0x14) #Internal OSC freq self.sendcommand(0x72) #Set contrast self.sendcommand(0x54) #Power/ICON control/Contrast set self.sendcommand(0x6F) #Follower control self.sendcommand(0x0C) #Display ON self.clear() self.backlight = GPIO('PA28', 'OUTPUT') return def backlight_on(self): self.backlight.on() return def backlight_off(self): self.backlight.off() return def sendcommand(self, value): self.i2c_bus.write_byte_data(self.lcd_address, 0x00, value) return def senddata(self, value): self.i2c_bus.write_byte_data(self.lcd_address, 0x40, value) return def clear(self): self.sendcommand(0x01) time.sleep(0.001) self.setsinglefont() return def home(self): self.sendcommand(0x03) time.sleep(0.001) return def setcontrast(self, value): """ Set the display contrast value = 0 to 15 """ self.sendcommand(0x70 + value) return def setdoublefont(self): self.sendcommand(0x30 + 0x0C + 0x01) return def setsinglefont(self): self.sendcommand(0x30 + 0x08 + 0x01) return def setcurpos(self, x, y): if y < 0 or y > 1: return if x < 0 or x > 15: return if y == 0: self.sendcommand(0x80 + 0x00 + x) else: self.sendcommand(0x80 + 0x40 + x) return def putchar(self, value): self.senddata(value) return def putstring(self, string): if len(string) == 0: return if len(string) > 16: string = string[0:16] for char in string: self.putchar(ord(char)) return def backlighton(self): self.backled.on() return def backlightoff(self): self.backled.off() return
pwm_servo = "J4.34" enabServo = "J4.35" motor_spd = "J4.36" pin1Motor = "J4.37" pin2Motor = "J4.39" print "Pin not properly set <servo> <enable servo> <pin1motor> <pin2motor> <enable>\nDefaults servo:PWM0:34 enabServo:35 pin1motor:PC1:37 pin2motor:PC0:39 enable:PWM1:36\n" print "listening on port 3000" node_script = subprocess.Popen(["nodejs", "interface.js"], stdout=subprocess.PIPE) #launch nodejs wheel = 0 accel = 0 time_deadzone_start = 0.0 time_deadzone = 100.0 try: servo = PWM(pwm_servo, 200) enable_servo = GPIO(enabServo, "OUTPUT") speed = PWM(motor_spd, 200) motor1 = GPIO(pin1Motor, "OUTPUT") motor2 = GPIO(pin2Motor, "OUTPUT") except KeyError: print "Invalid pin name\nFormat: J4.xx, PWMs in 34 36 38 40\n" sys.exit(1) motor1.on() #only forward, for the moment servo.start(0) speed.start(0) while True: try: output = json.loads( node_script.stdout.readline()) #obtain a dict from JSON accel = (1 - output['gamepad_z']) * 50 if output['gamepad_x'] == 0:
def __send_and_sleep(self, output, sleep): GPIO.output(self.__pin, output) time.sleep(sleep)
#!/usr/bin/python import lcd import time import keys from acmepins import GPIO import socket from datetime import timedelta import rs486 import os led = GPIO("PB8", "OUTPUT") link = rs486.Link("/dev/ttyS2") rx_counter = 0 while True: message = link.receive(1) if message == None: print "timeout" continue try: led.on() rx_counter += 1 print "%d] Rx %d %d" % (rx_counter, message.get_target_node(), message.get_frame_type()) led.off() except: if rx_counter > 0: rx_counter -= 1
import os from acmepins import GPIO from time import sleep #Stepper enable = GPIO('J4.23', 'OUTPUT') step = GPIO('J4.28', 'OUTPUT') dir = GPIO('J4.30', 'OUTPUT') ms1 = GPIO('J4.25', 'OUTPUT') ms2 = GPIO('J4.27', 'OUTPUT') ms3 = GPIO('J4.29', 'OUTPUT') #Diodes ledIR = GPIO('J4.32', 'OUTPUT') data_photo = 0 check = False pas = 0 ratio = 1.3072 poids = 0 def check_step(): ms1.off() ms2.off() ms2.off() if 200 < data_photo < 900: step.on() sleep(0.5) step.off() sleep(0.5)
except: return "NO IP" def uptime(): with open('/proc/uptime', 'r') as f: uptime_seconds = float(f.readline().split()[0]) uptime_string = str(timedelta(seconds = uptime_seconds)) return(uptime_string) CONFIG_FILE="ds485.ini" display=lcd.lcd() display.backlight_on() key=keys.KEYS() relay=GPIO("J4.29","OUTPUT") led=GPIO("PB8","OUTPUT") STATE_INIT=0 STATE_WELCOME=1 STATE_MYADDR=2 STATE_TEMPERATURES=3 STATE_MYIP=4 STATE_UPTIME=5 STATE_MESSAGES=6 STATE_TEST_RELAY=7 STATE_BACKLIGHT=8 LAST_STATE=8 current_state=0 next_state=1
#Update MQTT scores def update_mqtt_scores(blue_score, red_score): publish.single("bluescore", "%d" % blue_score, hostname="videowall.local") publish.single("redscore", "%d" % red_score, hostname="videowall.local") #Gestione del gioco. Questa funzione viene lanciata in #un thread separato ed esegue il loop di gestione del gioco crono, blue_score, red_score, last_score #GPIO usati come ingressi dai fotoaccoppiatori IR IR_RED = GPIO("J4.36", "INPUT") IR_BLUE = GPIO("J4.38", "INPUT") IR_PULL = GPIO("J4.40", "INPUT") #Led spia interno LED = GPIO("J4.24", "LOW") update_mqtt_scores(blue_score, red_score) #Loop principale di gestione del gioco while True: if IR_PULL.get_value() == 1: print "Pull" blink(LED, 1, 0.2) counter = 0 while IR_PULL.get_value() == 1:
class LedPanel(): extra_small_font = None small_font = None medium_font = None large_font = None arietta_button = None scores_order = None im = None def __init__(self): #Panel size size = 32, 32 #True time fonts self.extra_small_font = ImageFont.truetype("fonts/Ubuntu-B.ttf", 8) self.small_font = ImageFont.truetype("fonts/Ubuntu-B.ttf", 12) self.medium_font = ImageFont.truetype("fonts/Ubuntu-B.ttf", 14) self.large_font = ImageFont.truetype("fonts/Ubuntu-B.ttf", 24) self.arietta_button = GPIO('PC17', 'INPUT') self.scores_order = 1 #Create a 32x32 black image self.im = Image.new("RGB", size, "black") def refresh(self): #Generate a PPM image (a format very similar to byte array RGB we need) output = StringIO.StringIO() self.im.save(output, format='PPM') buf = output.getvalue() #Discard the first 13 bytes of header and save the rest (the #RGB array) on the ledpanel driver output buffer out_file = open("/sys/class/ledpanel/rgb_buffer", "w") out_file.write(buf[13:]) out_file.close() #Visualizzazione punteggio def show_scores(self, blue_score, red_score, time_lapse, the_winner_is): #Legge il tastino dietro Arietta per vedere se deve #scambiare i risultati if self.arietta_button.digitalRead() == 0: if (self.scores_order == 0): self.scores_order = 1 else: self.scores_order = 0 print "Premuto %d" % self.scores_order #Create a draw object to draw primitives on the new image draw = ImageDraw.Draw(self.im) #No anti-aliasing draw.fontmode = "1" #Disegna un rettangolo nero per coprire scritte precedenti draw.rectangle((0, 0, 31, 31), outline=0, fill=0) #Scrive i punteggi if the_winner_is == "none": blue_color = (0, 0, 5) red_color = (5, 0, 0) if the_winner_is == "blue": blue_color = (0, 0, 31) red_color = (2, 0, 0) if the_winner_is == "red": blue_color = (0, 0, 2) red_color = (31, 0, 0) if self.scores_order == 0: draw.text((-1, 0), blue_score, blue_color, font=self.medium_font) draw.text((17, 0), red_score, red_color, font=self.medium_font) else: draw.text((-1, 0), red_score, red_color, font=self.medium_font) draw.text((17, 0), blue_score, blue_color, font=self.medium_font) #Scrive il tempo if the_winner_is == "none": draw.text((0, 18), time_lapse, (0, 3, 0), font=self.small_font) else: draw.text((0, 18), time_lapse, (15, 15, 0), font=self.small_font) if the_winner_is == "none": #Disegna le linee divisorie draw.line([0, 16, 32, 16], (3, 3, 0), 2) draw.line([15, 0, 15, 16], (3, 3, 0), 2) if the_winner_is == "blue": draw.line([15, 0, 15, 16], (0, 0, 5), 2) draw.line([0, 16, 32, 16], (0, 0, 5), 4) if the_winner_is == "red": draw.line([15, 0, 15, 16], (5, 0, 0), 2) draw.line([0, 16, 32, 16], (5, 0, 0), 4) del draw self.refresh() def nolink(self): draw = ImageDraw.Draw(self.im) draw.fontmode = "1" #No antialias #Disegna prima un rettangolo nero per coprire scritte precedenti draw.rectangle((0, 0, 31, 31), outline=0, fill=0) draw.text((0, 0), "No", (5, 0, 0), font=self.small_font) draw.text((0, 10), "Link", (5, 0, 0), font=self.small_font) draw.text((0, 20), self.get_time(), (0, 5, 0), font=self.small_font) del draw self.refresh() def get_time(self): d = datetime.now() return "{:%H:%M}".format(d)
def __init__(self): self.esc = GPIO("J4.39", "INPUT") self.left = GPIO("J4.37", "INPUT") self.right = GPIO("J4.35", "INPUT") self.ok = GPIO("J4.33", "INPUT")
def game(): global crono, blue_score, red_score, last_score #GPIO usati come ingressi dai fotoaccoppiatori IR IR_RED = GPIO("J4.36", "INPUT") IR_BLUE = GPIO("J4.38", "INPUT") IR_PULL = GPIO("J4.40", "INPUT") #Led spia interno LED = GPIO("J4.24", "LOW") #Loop principale while True: if IR_PULL.get_value() == 1: print "Pull" blink(LED, 1, 0.2) counter = 0 while IR_PULL.get_value() == 1: time.sleep(1) counter += 1 #Se il tirante e' tenuto per piu' di 2 secondi resetta la partita if counter >= 2: crono.reset() crono.start() blue_score = 0 red_score = 0 last_score = "none" telegram_bot.send_alert("Inizio partita") print "Reset" break #Se il tirante e' tenuto per meno di 2 secondi annulla l'ultimo goal if last_score == "red" and red_score > 0: red_score -= 1 last_score = "none" crono.start() telegram_bot.send_alert( "Breaking news ! Annullato il goal dei rossi") telegram_bot.send_alert("Rossi %02d - Blue %02d" % (red_score, blue_score)) if last_score == "blue" and blue_score > 0: blue_score -= 1 last_score = "none" crono.start() telegram_bot.send_alert( "Breaking news ! Annullato il goal dei blu") telegram_bot.send_alert("Blu %02d - Rossi %02d" % (blue_score, red_score)) #Legge gli IR delle porte solo se nessuno ha ancora vinto if scores_check(blue_score, red_score, crono) == "none": #Lettura IR porta dei rossi if IR_RED.get_value() == 1: blue_score = blue_score + 1 last_score = "blue" print "Goal for blue %d" % blue_score telegram_bot.send_alert("Goal dei blu\nBlu %02d - Rossi %02d" % (blue_score, red_score)) if scores_check(blue_score, red_score, crono) == "blue": telegram_bot.send_alert( "Vittoria dei Blue !!\nDurata della partita: %s\nRisultato: Blu %02d - Rossi %02d" % (crono.get(), blue_score, red_score)) blink(LED, 2, 0.2) time.sleep(1) #Lettura IR porta dei blue if IR_BLUE.get_value() == 1: red_score = red_score + 1 last_score = "red" print "Goal for red %d" % red_score telegram_bot.send_alert( "Goal dei rossi\nRossi %02d - Blu %02d" % (red_score, blue_score)) if scores_check(blue_score, red_score, crono) == "red": telegram_bot.send_alert( "Vittoria dei Rossi !!\nDurata della partita: %s\nRisultato: Rossi %02d - Blu %02d" % (crono.get(), red_score, blue_score)) blink(LED, 3, 0.2) time.sleep(1)
#!/usr/bin/python from acmepins import GPIO pin_out = GPIO('J4.39', 'OUTPUT') # PC0 pin_in = GPIO('J4.37', 'INPUT') # PC1 while True: pin_out.digitalWrite(0 if pin_in.digitalRead() else 1)