aa
>> code python
import RPi.GPIO as GPIO
import time
import os
import subprocess
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# Input pins:
A_pin = 17
B_pin = 27
C_pin = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(A_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(B_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(C_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
# Raspberry Pi pin configuration:
RST = 24
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Load default font.
font = ImageFont.load_default()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
buttons = [0,0,0];
hexes = os.listdir('./data')
position = 0;
def flashWrite():
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0,30), 'writing...', font=font, fill=255);
disp.image(image)
disp.display()
result = subprocess.call(['./leonardoUploader', '/dev/ttyACM0', './data/'+hexes[position]]);
if result == 0:
draw.text((0,30), 'success', font=font, fill=255);
else:
draw.text((0,30), 'error', font=font, fill=255);
disp.image(image)
disp.display()
time.sleep(1)
def scan():
global position
global mode
if not GPIO.input(A_pin):
if buttons[0] == 0:
position = position + 1
if position >= len(hexes):
position = 0;
buttons[0] = 1
else:
buttons[0] = 0
if not GPIO.input(B_pin):
if buttons[1] == 0:
position = position - 1
if position < 0:
position = len(hexes) - 1;
buttons[1] = 1
else:
buttons[1] = 0
if not GPIO.input(C_pin):
if buttons[2] == 0:
flashWrite();
buttons[2] = 1
else:
buttons[2] = 0
def repaint():
draw.rectangle((0,0,width,height), outline=0, fill=0)
if buttons[0]:
draw.rectangle((0,0,10,10), outline=255, fill=1);
else:
draw.rectangle((0,0,10,10), outline=255, fill=0);
if buttons[1]:
draw.rectangle((10,0,20,10), outline=255, fill=1);
else:
draw.rectangle((10,0,20,10), outline=255, fill=0);
if buttons[2]:
draw.rectangle((20,0,30,10), outline=255, fill=1);
else:
draw.rectangle((20,0,30,10), outline=255, fill=0);
draw.text((0,30), hexes[position], font=font, fill=255);
disp.image(image)
disp.display()
scan()
repaint()
dirty = False
def sig(n):
global dirty
scan()
dirty = True
GPIO.add_event_detect(A_pin, GPIO.BOTH, callback=sig, bouncetime=20);
GPIO.add_event_detect(B_pin, GPIO.BOTH, callback=sig, bouncetime=20);
GPIO.add_event_detect(C_pin, GPIO.BOTH, callback=sig, bouncetime=20);
try:
while 1:
if dirty:
repaint()
dirty = False
#time.sleep(.01)
except KeyboardInterrupt:
GPIO.cleanup()
<<
>> code bash
[Unit]
Description=arduboyWriter
After=syslog.target
[Service]
Type=simple
WorkingDirectory=/home/pi/arduboyWriter
ExecStart=/usr/bin/python main.py
TimeoutStopSec=5
StandardOutput=null
[Install]
WantedBy = multi-user.target
<<5643382
wiki
1515150135