Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1269

Python • MIDI host PI with SSD1603

$
0
0
Hi there,

I'm trying to convert an existing project (based on this) to work using newer libraries. The "connectall.rb" script is still working as expected, but I'm trying to add an oled ssd1306 to the project, and all the Adafruit libs that are used in the old scripts seem to be depreciated, and I can't seem to get my screen to work at all using them. So I forked an existing repository to my own with some minor changes so far.

https://github.com/oermenz/midihub

There are two files that do the display part of the script, lcd-show.py (old, adafruit library) and midioled.py (newer, luma.oled library). The old one I can't get to work at all, so I'm trying to replace that one with a new script file. Using luma.oled library I can get the display started and working. Now the big problem here is that I'm a total newb when it comes to coding and I'm probably not smart and willing enough to learn it to the extend for what I want to accomplish.

Code:

#!/usr/bin/python3import timeimport sysimport Adafruit_GPIO.SPI as SPIimport Adafruit_SSD1306from PIL import Imagefrom PIL import ImageDrawfrom PIL import ImageFontimport subprocessimport fcntlimport errnodef acquireLock():    while True:      try:        ''' acquire exclusive lock file access '''        locked_file_descriptor = open('/tmp/lockfile.LOCK', 'w+')        fcntl.lockf(locked_file_descriptor, fcntl.LOCK_EX)        return locked_file_descriptor      except IOError as e:        if e.errno != errno.EAGAIN:            raise        else:            time.sleep(2)def releaseLock(locked_file_descriptor):    ''' release exclusive lock file access '''    locked_file_descriptor.close()lock_fd = acquireLock()# Raspberry Pi pin configuration:RST = None     # on the PiOLED this pin isnt used# Note the following are only used with SPI:DC = 23SPI_PORT = 0SPI_DEVICE = 0# 128x64 display with hardware I2C:disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)disp.begin()# Clear display.disp.clear()disp.display()# Create blank image for drawing.width = disp.widthheight = disp.heightimage = 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)padding = -2top = paddingbottom = height-paddingx = 0#font = ImageFont.load_default()height = 12font = ImageFont.truetype('/usr/share/fonts/truetype/lato/Lato-Semibold.ttf', height)for y in range(0, len(sys.argv)-1):    draw.text((x, top+y*height), sys.argv[y+1], font=font, fill=255)disp.image(image)disp.display()releaseLock(lock_fd)
So my goal here is to get the above code working in the exact same manner except using luma.oled library to achive it, and below is how far I've gotten on my own. But the problem already occurs when I run "python3 midioled.py "line1" "line2" "line3"" It does show on the display, but only the last line is visible. So in this case I only get "line3" on the 3rd row but 1st and 2nd row being empty.

Code:

#!/usr/bin/python3from luma.core.interface.serial import i2cfrom luma.core.render import canvasfrom luma.oled.device import ssd1306import sysimport timeimport fcntlimport errnoimport subprocessserial = i2c(port=1, address=0x3C)device = ssd1306(serial, rotate=0)def acquireLock():    while True:      try:        ''' acquire exclusive lock file access '''        locked_file_descriptor = open('/tmp/lockfile.LOCK', 'w+')        fcntl.lockf(locked_file_descriptor, fcntl.LOCK_EX)        return locked_file_descriptor      except IOError as e:        if e.errno != errno.EAGAIN:            raise        else:            time.sleep(2)def releaseLock(locked_file_descriptor):    ''' release exclusive lock file access '''    locked_file_descriptor.close()lock_fd = acquireLock()padding = -2top = paddingheight = 12bottom = height-paddingx = 0font = ImageFont.truetype('/usr/share/fonts/truetype/lato/Lato-Semibold.ttf', height)for y in range(0, len(sys.argv)-1):  with canvas(device) as draw:    draw.rectangle(device.bounding_box, outline="white", fill="black")    draw.text((x, top+y*height), sys.argv[y+1], font=font, fill="white")time.sleep(3)releaseLock(lock_fd)
It also seems I have to use "sleep" to display the text for 3 second (probably need an constant loop instead?) Apart from the old and new code supplied above, there is also a part that is added to the "connectall.rb" script, as follows:

Code:

cmd = "/usr/local/bin/lcd-show.py"if names.length>1 then  command = "#{cmd} #{names.map(&:inspect).join(' ')} "else  command = "#{cmd} '' 'No MIDI' 'connections' "endpid = spawn(command)Process.detach(pid)
Since I'm just doing some "trial and error" copy/paste work and getting impatient with it at this point, can someone with more expertise help me out on this one? Would appreciate it a lot!

Statistics: Posted by oermens — Sun Oct 27, 2024 4:59 pm



Viewing all articles
Browse latest Browse all 1269

Trending Articles