Archived
added new python script for oled display
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
[Unit]
|
||||
Description=Start HAT-server for pibrella
|
||||
DefaultDependencies=no
|
||||
After=sysinit.target local-fs.target
|
||||
Before=basic.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/local/sbin/hat-pibrella-server.py
|
||||
ExecStartPost=/usr/local/sbin/hatctl.py BS
|
||||
|
||||
[Install]
|
||||
WantedBy=basic.target
|
||||
@@ -0,0 +1,54 @@
|
||||
#! /usr/bin/python3
|
||||
# -*- coding: utf8 -*-
|
||||
|
||||
# load needed modules from luma
|
||||
from luma.core.interface.serial import i2c, spi
|
||||
from luma.core.render import canvas
|
||||
from luma.oled.device import sh1106
|
||||
from luma.core import lib
|
||||
from luma.core.virtual import terminal
|
||||
|
||||
# load needed modules from RPi.GPIO
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
# load needed modules from PIL
|
||||
from PIL import ImageFont, ImageDraw, Image
|
||||
|
||||
# load more needed modules
|
||||
import time
|
||||
import sys
|
||||
|
||||
# load randommodules
|
||||
#from random import randint, gauss
|
||||
#from pathlib import Path
|
||||
import pyudev
|
||||
|
||||
|
||||
#GPIO define pins
|
||||
RST_PIN = 25 #Reset
|
||||
CS_PIN = 8
|
||||
DC_PIN = 24
|
||||
|
||||
serial = spi(device=0, port=0, bus_speed_hz = 8000000, transfer_size = 4096, gpio_DC = DC_PIN, gpio_RST = RST_PIN)
|
||||
device_used = sh1106(serial, rotate=2) #sh1106
|
||||
|
||||
context = pyudev.Context()
|
||||
|
||||
def main():
|
||||
while True:
|
||||
font = make_font(None, None)
|
||||
term = terminal(device_used, font)
|
||||
monitor = pyudev.Monitor.from_netlink(context)
|
||||
monitor.filter_by('block')
|
||||
for device in iter(monitor.poll, None):
|
||||
if 'ID_FS_TYPE' in device:
|
||||
term.println('{0} partition {1}'.format(device.action, device.get('ID_FS_LABEL')))
|
||||
time.sleep(5)
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
GPIO.cleanup()
|
||||
@@ -1,116 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
import socket
|
||||
import os, os.path
|
||||
import time
|
||||
|
||||
import pibrella
|
||||
|
||||
SOCKFILE = "/run/hat-server"
|
||||
|
||||
TIME_ON = 0.2
|
||||
TIME_OFF = 0.2
|
||||
FADE_ON = 0.2
|
||||
FADE_OFF = 0.2
|
||||
|
||||
ts_start = 0
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def boot_start():
|
||||
pibrella.light.red.blink(TIME_ON, TIME_OFF)
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def boot_end():
|
||||
pibrella.light.red.off()
|
||||
pibrella.light.green.on()
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def action_start():
|
||||
pibrella.light.green.off()
|
||||
pibrella.light.yellow.pulse(FADE_ON, FADE_OFF, TIME_ON, TIME_OFF)
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def action_end():
|
||||
pibrella.light.green.on()
|
||||
pibrella.light.yellow.off()
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def halt_start():
|
||||
pibrella.light.red.blink(TIME_ON, TIME_OFF)
|
||||
pibrella.light.green.off()
|
||||
pibrella.light.yellow.off()
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def halt_end():
|
||||
pibrella.light.red.off()
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def btn_pressed(pin):
|
||||
global ts_start
|
||||
ts_start = time.time()
|
||||
print "button pressed ..."
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def btn_released(pin):
|
||||
global ts_start
|
||||
ts_end = time.time()
|
||||
print "button released ..."
|
||||
print "ts_start:", ts_start
|
||||
print "duration:", ts_end-ts_start
|
||||
if ts_end - ts_start > 2:
|
||||
pibrella.buzzer.success()
|
||||
os.system("halt -p &")
|
||||
else:
|
||||
pibrella.buzzer.fail()
|
||||
ts_start = 0
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
def run_server():
|
||||
if os.path.exists(SOCKFILE):
|
||||
os.remove(SOCKFILE)
|
||||
print "Opening socket..."
|
||||
|
||||
server = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
|
||||
server.bind(SOCKFILE)
|
||||
server.listen(5)
|
||||
|
||||
print "Listening..."
|
||||
while True:
|
||||
conn, addr = server.accept()
|
||||
print 'accepted connection'
|
||||
while True:
|
||||
data = conn.recv(2)
|
||||
if not data:
|
||||
break
|
||||
elif data == "BS":
|
||||
boot_start()
|
||||
elif data == "BE":
|
||||
boot_end()
|
||||
elif data == "AS":
|
||||
action_start()
|
||||
elif data == "AE":
|
||||
action_end()
|
||||
elif data == "HS":
|
||||
halt_start()
|
||||
elif data == "HE":
|
||||
halt_end()
|
||||
print "Shutting down..."
|
||||
server.close()
|
||||
os.remove(SOCKFILE)
|
||||
return
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
pibrella.button.pressed(btn_pressed)
|
||||
pibrella.button.released(btn_released)
|
||||
run_server()
|
||||
@@ -1,25 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
import socket
|
||||
import os, os.path
|
||||
import time
|
||||
|
||||
SOCKFILE = "/run/hat-server"
|
||||
|
||||
# -------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
if len(sys.argv) == 1:
|
||||
sys.exit(3)
|
||||
else:
|
||||
# we might be called very early during boot from udev-rules and the hat-server might
|
||||
# not be up. This will wait 10s for SOCKFILE - more than enough for a Raspberry Pi
|
||||
for _ in range(100):
|
||||
if os.path.exists(SOCKFILE):
|
||||
break
|
||||
else:
|
||||
time.sleep(0.1)
|
||||
sock = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
|
||||
sock.connect(SOCKFILE)
|
||||
sock.sendall(sys.argv[1])
|
||||
Reference in New Issue
Block a user