Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major refactor, added argument read, and support for another screen #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
src/__pycache__
spool/
try.sh
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -22,3 +22,10 @@ Run stampalafanza.py

![Console](/console.png)



# Pantalla oled
- Install adafruit library
# https://github.com/adafruit/Adafruit_CircuitPython_Bundle
# https://github.com/adafruit/Adafruit_CircuitPython_SSD1306
- I would recomend to install it in a Python venv not in the native python
3 changes: 1 addition & 2 deletions spoolraw.py
Expand Up @@ -28,7 +28,6 @@ def main():
files = [x for x in sorted(glob.glob(os.path.dirname(os.path.realpath(__file__)) + '/spool/**/*-*', recursive=True))
if not x.endswith('.info') and not x.endswith('.keep') and not x.endswith('.raw')]
for fname in files:
#print(fname)
rawname = fname + "." + ppdstd + ".raw"
if not os.path.isfile(rawname):
print ("Converto " + os.path.basename(rawname))
Expand Down Expand Up @@ -67,7 +66,7 @@ def main():
cmd = CONV_CMD.split(' ')
cmd = [x % {'in': fname, 'out': rawname, 'ppd': ppd, 'copies': copies, 'sides': sides, 'media': media} for x in cmd]
cmd = " ".join(cmd)
#print (cmd)
print (cmd)
os.system(cmd)
else:
print ("Salto " + os.path.basename(rawname))
Expand Down
9 changes: 9 additions & 0 deletions src/cups.py
@@ -0,0 +1,9 @@
import configparser

class CupsManager():
def __init__(self,fname,DEFAULT_COPIES,DEFAULT_SIDES,DEFAULT_MEDIA):
self.config = configparser.ConfigParser()
self.name = "-".join(os.path.basename(fname).split("-")[1:])
self.copies = DEFAULT_COPIES
self.sides = DEFAULT_SIDES
self.media = DEFAULT_MEDIA
37 changes: 37 additions & 0 deletions src/media.py
@@ -0,0 +1,37 @@
import os
from pad4pi import rpi_gpio
import subprocess

def playSound(file):
os.system("mpg123 sounds/" + file + ".mp3 > /dev/null 2>&1 &")

def getPpd():
prn = subprocess.check_output("lpstat -d | awk '{print $NF}'", shell=True).decode("utf-8").strip()
print("prn: " + prn)
ppd = "/etc/cups/ppd/" + prn +".ppd"
print("ppd: " + ppd)
with open(ppd) as ppdfile:
ppdstd = [x for x in ppdfile if x.startswith("*PCFileName:")]
ppdstd = ppdstd[0].lower().split('"')[1].split(".ppd")[0]
return ppdstd

class KeyStore():
def __init__(self):
KEYPAD = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
ROW_PINS = [21, 20,16, 12]# BCM numbering
COL_PINS = [26, 19, 13, 6] # BCM numberin
factory = rpi_gpio.KeypadFactory()
self.keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
self.keypad.registerKeyPressHandler(self.storeKey)
self.keypressed = ""

def storeKey(self,key):
self.keypressed = key

def restartKey(self):
self.keypressed = ""
15 changes: 15 additions & 0 deletions src/parser.py
@@ -0,0 +1,15 @@
import sys
import argparse

def readParameters():
usage_examples = sys.argv[0] + " -digits 4 -i2c 0x3c \n"
parser = argparse.ArgumentParser(epilog=usage_examples,formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-digits",help="Number of digits to identify fanzines", default=3,type=int)
parser.add_argument("-copies", help="Number of copies by default", default=1, type=int)
parser.add_argument("-sides", help="Sides for printing by default two-sided-long-edge, accepted values [two-sided-long-edge, two-sided-short-edge, one-sided]",default="two-sided-long-edge")
parser.add_argument("-media", help="Page size, by default it's A4", default="A4")
parser.add_argument("-i2c", help="I2c address for your device, default value 0x27", default="0x27")
parser.add_argument("-st", help="Screen type, accepted values ['oled', 'lcd'] default oled ", default="oled")
args = parser.parse_args()
args.i2c = int(args.i2c,16)
return args
66 changes: 66 additions & 0 deletions src/screen.py
@@ -0,0 +1,66 @@
import time
from board import SCL, SDA
import busio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306

class ScreenManager:
def __init__(self,screentype,temp_digits = 3,font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf",address=0):
self.screentype = screentype
self.temp_digits = temp_digits

if screentype.lower() == "oled":
self.i2c = busio.I2C(SCL, SDA)

self.disp = adafruit_ssd1306.SSD1306_I2C(128, 32, self.i2c)
self.disp.fill(0)
self.disp.show()

self.width = self.disp.width
self.height = self.disp.height
self.image = Image.new("1", (self.width,self.height))

self.draw = ImageDraw.Draw(self.image)
self.x = 0

self.padding = -2
self.top = self.padding
self.bottom = self.height - self.padding

self.font = ImageFont.truetype(font_path,size=14)
self.draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
elif screentype.lower() == "lcd":
self.mylcd = CharLCD(i2c_expander='PCF8574', address=adress, port=1, cols=16, rows=2, charmap='A02')
else:
print("Error, invalid screen type")
exit(1)

def __displayLcd(self):
r1 = ro1[:16].center(16,' ')
r2 = ro2[:16].center(16,' ')
self.mylcd.write_string(r1 + '\r\n' + r2)

def __displayOled(self,ro1,ro2):
self.draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
self.draw.text((self.x, self.top + 0), ro1, font=self.font,fill=255)
self.draw.text((self.x, self.top + 16), ro2, font=self.font,fill=255)
self.disp.image(self.image)
self.disp.show()
time.sleep(0.1)

def display(self,ro1,ro2):
if self.screentype == "oled":
self.__displayOled(ro1,ro2)
elif self.screentype == "lcd":
self.__displayLcd(ro1,ro2)

def __formatCode(self,code):
return (" " + code + "-"*(self.temp_digits - len(code)))

def displayCode(self,code):
fc = self.__formatCode(code)
self.display("Stampa La Fanza", fc)

def display404(self,code):
fc = self.__formatCode(code)
self.display("No fanza",fc)