satoshinm / oledterm

Mirror your Linux console terminal output to an OLED display module
35 stars 7 forks source link

Unable to access %s #4

Open peterbmckinley opened 3 years ago

peterbmckinley commented 3 years ago

Hi Im keen to use this with my Orange Pi Zero setup. Luma.oled is installed and working, and can display all the luma.examples. I'm using an SSD1306 controller OLED display connected via the i2c bus. When I run the following command from the oledterm directory:

sudo python3 oledterm.py --display ssd1306 --interface i2c --rotate 2

it blows up as follows:

File "oledterm.py", line 55 print "Unable to access %s, try running as root?" % (VIRTUAL_TERMINAL_DEVICE,)

Any clues what might be wrong?

Peter

peterbmckinley commented 3 years ago

Correction, I'm using a 1.3" OLED with an SH1106 controller.

I discovered this issue was because your code is written for Python 2. I ran it through 2to3 to convert to Python 3 but now when I run:

python3 oledterm.py --i2c-port 0 --display sh1106

I get:

Traceback (most recent call last): File "oledterm.py", line 113, in main() File "oledterm.py", line 100, in main term.putch(char) File "/usr/local/lib/python3.7/dist-packages/luma/core/virtual.py", line 327, in putch w = self.font.getsize(char)[0] File "/usr/local/lib/python3.7/dist-packages/PIL/ImageFont.py", line 262, in getsize size, offset = self.font.getsize(text, False, direction, features, language) TypeError: expected string

Some junk appears on the screen for a few seconds while the above prints, then it goes blank.

Any thoughts?

Perhaps this is an orphan project......

peterbmckinley commented 3 years ago

bump

Krizzel87 commented 3 years ago

Hi peterbmckinley,

I have the same Problem like you and i think, i slowly get it work.

I did a convert with 2to3, so the code can run thrue python3, because luma.oled ONLY runs now in Python3.

like you said it runs in trouble in line 100 term.putch(char) with the reason unexpected type, needed string.

Krizzel87 commented 3 years ago

So i tried to get it work, what would be nicer than a REAL RETRO PI with OLED Terminal ;-)

        for char in data:
            if '\r' in chr(char):
                term.carriage_return()
            elif chr(10) in chr(char):
                #term.newline()
                # no scroll, no flush
                term.carriage_return()
                x = 0
                term._cy += term._ch
            elif '\b' in chr(char):
                term.backspace()
                x =- 1
            elif '\t' in chr(char):
                term.tab()
            else:
                term.putch(chr(char))

So now the Oled (SSH1106) shows up with number per line.

peterbmckinley commented 3 years ago

😳

On Sat, 22 Aug 2020, 18:39 Krizzel87, notifications@github.com wrote:

So i tried to get it work, what would be nicer than a REAL RETRO PI with OLED Terminal ;-)

for char in data: if '\r' in chr(char): term.carriage_return() elif chr(10) in chr(char): #term.newline() # no scroll, no flush term.carriage_return() x = 0 term._cy += term._ch elif '\b' in chr(char): term.backspace() x =- 1 elif '\t' in chr(char): term.tab() else: term.putch(chr(char)) So now the Oled (SSH1106) shows up with number per line.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/satoshinm/oledterm/issues/4#issuecomment-678669905, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFONLDIUXX25QLX75FPJ5HTSB77GZANCNFSM4PADFK5Q .

Krizzel87 commented 3 years ago

oh and you need to edit the go.sh

insert this

#!/bin/sh -x
python3 oledterm.py --display sh1106 --interface i2c --rotate 0

hope you have installed python libs etc

Krizzel87 commented 3 years ago

the only thing I need to learn is how data = subprocess.check_output(["screendump"]) is getting the data

peterbmckinley commented 3 years ago

Good luck! I can't help you with that, but good work 🤞😊

Krizzel87 commented 3 years ago

here is the full new python 3 code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# based on:
# Copyright (c) 2014-17 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

import os
import time
import sys
import subprocess
from luma.core import cmdline
from luma.core.virtual import terminal
from PIL import ImageFont

VIRTUAL_TERMINAL_DEVICE = "/dev/vcsa"
ROWS = 9
COLS = 31

# based on demo_opts.py
from luma.core import cmdline, error
def get_device(actual_args=None):
    """
    Create device from command-line arguments and return it.
    """
    if actual_args is None:
        actual_args = sys.argv[1:]
    parser = cmdline.create_parser(description='luma.examples arguments')
    args = parser.parse_args(actual_args)

    if args.config:
        # load config from file
        config = cmdline.load_config(args.config)
        args = parser.parse_args(config + actual_args)

    # create device
    try:
        device = cmdline.create_device(args)
    except error.Error as e:
        parser.error(e)

    #print(display_settings(args))

    return device

# based on luma.examples terminal
def make_font(name, size):
    font_path = os.path.abspath(os.path.join(
        os.path.dirname(__file__), 'fonts', name))
    return ImageFont.truetype(font_path, size)

def main():
    if not os.access(VIRTUAL_TERMINAL_DEVICE, os.R_OK):
       print(("Unable to access %s, try running as root?" % (VIRTUAL_TERMINAL_DEVICE,)))
       raise SystemExit

    fontname = "tiny.ttf"
    size = 6

    font = make_font(fontname, size) if fontname else None
    term = terminal(device, font, animate=False)

    term.clear()
    for i in range(0, ROWS):
        term.puts(str(i) * COLS)
    term.flush()
    #time.sleep(1)

    while True:
        # Get terminal text; despite man page, `screendump` differs from reading vcs dev
        #data = file(VIRTUAL_TERMINAL_DEVICE).read()
        data = subprocess.check_output(["screendump"])
    #print [data]
        # Clear, but don't flush to avoid flashing
        #term.clear()
        term._cx, term._cy = (0, 0)
        #term._canvas.rectangle(term._device.bounding_box, fill=term.bgcolor)
        term._canvas.rectangle(term._device.bounding_box, fill="black")

        # puts() flushes on newline(), so reimplement it ourselves
        #term.puts(data)

        for char in data:
            if '\r' in chr(char):
                term.carriage_return()
            elif chr(10) in chr(char):
                #term.newline()
                # no scroll, no flush
                term.carriage_return()
                x = 0
                term._cy += term._ch
            elif '\b' in chr(char):
                term.backspace()
                x =- 1
            elif '\t' in chr(char):
                term.tab()
            else:
                term.putch(chr(char))

        term.flush()
        time.sleep(0.01)
        #print "refresh"
        #print data

if __name__ == "__main__":
    os.system("stty --file=/dev/console rows %d" % (ROWS,))
    os.system("stty --file=/dev/console cols %d" % (COLS,))
    try:
        device = get_device()
        main()
    except KeyboardInterrupt:
        pass
Krizzel87 commented 3 years ago

but you can test it!

Krizzel87 commented 3 years ago

is luma.oled installed to your python3 and running succesfull? did you start i2c interface etc=?

Now I have Holiday and want to make a cool pi Server. Learnd python with google^^ and i can read and understand. It was horrible the problem seems to be the change from python2 to python3 wit how Chars and Strings were used.

Krizzel87 commented 3 years ago

To run on boot, add to /etc/rc.local:

sudo python3 /home/pi/oledterm/oledterm.py --display sh1106 --interface i2c --rotate 0 &
peterbmckinley commented 3 years ago

is luma.oled installed to your python3 and running succesfull? did you start i2c interface etc=?

Now I have Holiday and want to make a cool pi Server. Learnd python with google^^ and i can read and understand. It was horrible the problem seems to be the change from python2 to python3 wit how Chars and Strings were used.

See OP: "Luma.oled is installed and working, and can display all the luma.examples"

Yes the change from Python 2 to Python 3 is a huge problem for the inexperienced

Krizzel87 commented 3 years ago

but i thing the auto boot will only work when you are the ONLY user on your pi and only log in once.

https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/

Krizzel87 commented 3 years ago

I test it with pi 4 latest raspian OS and SH1106. now it Work. Only issue --rotate 1 und 3 does not work correctly.

peterbmckinley commented 3 years ago

Fantastic! I'll be home soon and will test it. Im using Orange Pi Zero but hopefully it will work too.

On Sat, 22 Aug 2020, 19:53 Krizzel87, notifications@github.com wrote:

I test it with pi 4 latest raspian OS and SH1106. now it Work. Only issue --rotate 1 und 3 does not work correctly.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/satoshinm/oledterm/issues/4#issuecomment-678678005, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFONLDKDZGQWAPHS2RSVG6LSCAH2TANCNFSM4PADFK5Q .

Krizzel87 commented 3 years ago

I fail the hole evening, because i Booted to Desktop and not to CLI, it can be changed in raspi-config . To test you can start it sudo sh go.sh and then switch via Ctrl Alt F1 to CLI. Have Fun! The front is very tiny, but you can change Size and Type in oledterm.py in Line 58

fontname = "tiny.ttf"
    size = 6

but then you need to resize in line 17, to resize Display

ROWS = 9
COLS = 31

Until Screen looks like this at programm start

000000000000
111111111111
222222222222
333333333333

Not like this

000000001111
111122222222
333333334444
444455555555

Have Fun

peterbmckinley commented 3 years ago

It works! Thank you :)

peterbmckinley commented 3 years ago

Hi Krizzel87, I've re-opened this issue as I can't get it to autorun at boot.

Adding python3 oledterm/oledterm.py --i2c-port 0 --display sh1106 to /etc/rc.local fails with a Compatibility error.

I tried adding python3 oledterm/oledterm.py --i2c-port 0 --display sh1106 to ~/.profile, which I've used to automatically run other scripts (my Orange Pi Zero is set to automatically login the root account) but the output seems to get stuck in a loop, it's like the oled is trying to display 2 or more processes.

Have you had any success getting oledterm to run automatically at boot?

Krizzel87 commented 3 years ago

Okay.

I created a new go.sh with

#!/bin/sh -x
sudo python3 oledterm.py --display sh1106 --interface i2c --rotate 0

Edit /etc/rc.local


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sudo python3 /home/pi/oledterm3/oledterm.py --display sh1106 --interface i2c --rotate 0 &
exit 0
peterbmckinley commented 3 years ago

I'm getting this during boot:

[ 33.402269] rc.local[1148]: python3: can't open file 'oledterm/oledterm.py': [Errno 2] No such file or directory

The path I added in /etc/rc.local works fine:

python3 oledterm/oledterm.py --i2c-port 0 --display sh1106

Any idea why it fails to execute during runtime?

Krizzel87 commented 3 years ago

You need to Start the script with sudo

SUDO PYTHON3 /home/pi/oledterm/oledterm.py --d sh1106

And you need to use absolute Adress of the oledterm.py

peterbmckinley commented 3 years ago

exactly the same

[ 33.826075] rc.local[1166]: python3: can't open file 'oledterm/oledterm.py': [Errno 2] No such file or directory

Here is the complete /etc/rc.local file:

!/bin/sh -e

#

rc.local

#

This script is executed at the end of each multiuser runlevel.

Make sure that the script will "exit 0" on success or any other

value on error.

#

In order to enable or disable this script just change the execution

bits.

#

By default this script does nothing.

sudo python3 oledterm/oledterm.py --i2c-port 0 --display sh1106 &

exit 0

You need to Start the script with sudo

SUDO PYTHON3 /home/pi/oledterm/oledterm.py --d sh1106

And you need to use absolute Adress of the oledterm.py

It is the absolute path. It works when I paste it into a Command Prompt

peterbmckinley commented 3 years ago

no idea why the formatting is all messed up, sorry about that. #new problems

Krizzel87 commented 3 years ago

Hmm... okay. I is that Problem i had last evening, to auto Boot the script via rc.local

I solved it with sudo raspi-config Boot options Boot to Cli with auto login

Krizzel87 commented 3 years ago

I'm getting this during boot:

[ 33.402269] rc.local[1148]: python3: can't open file 'oledterm/oledterm.py': [Errno 2] No such file or directory

The path I added in /etc/rc.local works fine:

python3 oledterm/oledterm.py --i2c-port 0 --display sh1106

Any idea why it fails to execute during runtime?

You did not used the absolute Adresse sudo python3 /home/pi/oledterm/oledterm.py --i2c-port 0 --display sh1106 Here i correct it for you

Absolute Adresse always begin with /xxx/yyy/fite.date

When you use oledterm/oledterm.py --i2c-port 0 --display sh1106 as User it must be saved in your HOME

peterbmckinley commented 3 years ago

so editing /etc/rc.local only works with users, not root?

Krizzel87 commented 3 years ago

The script rc.local is done durring local loginprogress.

Sudo is to make you root that means

Root is path / Userpath is /home/pi/

So if you write

Oledterm/oledterm.py

It Will not executed, but if you put this code to rc.local

Sudo python3 /home/pi/oledterm/oledterm.py --display sh1106 --interface i2c &

It will run

peterbmckinley commented 3 years ago

Getting anything to run automatically has always been a nightmare in Linux, because developers write code to make things happen, and Administrators write code to prevent anything happening

by the way I dont have pi anything, im not using a raspberry pi

Krizzel87 commented 3 years ago

Sry plz add

Sudo python3 /home/pi/oledterm/oledterm.py --display sh1106 --interface i2c &

To rc.local

peterbmckinley commented 3 years ago

Ok now it's working.

Oledterm doesn't like running under the root account, and my root account was set to autologon. I disabled all that and set oledterm up under the user account (which I had named "admin").

For me the absolute path is:

sudo python3 /home/admin/oledterm/oledterm.py --i2c-port 0 --display sh1106 &

:+1:

peterbmckinley commented 3 years ago

Hi Krizzel87, I'm having problems with rendering and random scrolling. When you modified the script to use Python 3, did you use satoshinm's original script, or the oledterm.py which fixes printing to terminal?

peterbmckinley commented 3 years ago

You have it ALMOST useable, congratulations on that! But did you use the code listed under " Fix printing to terminal output which causes it to be displayed ad nauseum" on the main oledterm github page?

peterbmckinley commented 3 years ago

https://github.com/satoshinm/oledterm/commit/b8b7f89b08d59ddeac146176982de720033fbd14

Krizzel87 commented 3 years ago

I debug that with print(data) But this is raw bytes So if you want to see output print(str(data))

Krizzel87 commented 3 years ago

On my Pi 4 it Works now fine, starts At Boot, before Login in

peterbmckinley commented 3 years ago

After you log in, does it display ok?

peterbmckinley commented 3 years ago

On mine its flashing and scrolling, its hard to get to the prompt. It looks like satoshinm provided a fix for that

Krizzel87 commented 3 years ago

When flashing and popping like an old TV, the program may be started 2 times? Or you startet wrong drivers. I uploaded the wrorking code on https://github.com/Krizzel87/Oledterm_SH1106

Plz check your code, look at rc.local

Check if pi boots to CLI automaticly

peterbmckinley commented 3 years ago

Please upload a photo of your oled display immediately after you log in. Thanks!

peterbmckinley commented 3 years ago

On mine its hard to use, I only get to the prompt when I press enter many times. Plz upload a photo. Thanks

Krizzel87 commented 3 years ago

20200823_153800.jpg

Krizzel87 commented 3 years ago

Oh sry you have an ssd1306 So you need to change the go.sh sudo phython3 /home/pi/oledterm/oledterm.py --display ssd1306 --interface i2c

Tty this in Cmd

Krizzel87 commented 3 years ago

This flashing and popping is because ssd1306 vs sh1106 ist the registersize and mich more

peterbmckinley commented 3 years ago

Thats the login screen. Can you go ahead and log in and show me the NEXT screen

On Sun, 23 Aug 2020, 23:48 Krizzel87, notifications@github.com wrote:

Oh sry you have an ssd1306 So you need to change the go.sh --display ssd1306 --interface i2c

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/satoshinm/oledterm/issues/4#issuecomment-678834716, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFONLDODDXPBGWSPDZ5PHB3SCGMEJANCNFSM4PADFK5Q .

Krizzel87 commented 3 years ago

20200823_210734_1.gif

Krizzel87 commented 3 years ago

Plz one Moment i try to test it with Kali, and Ubuntu , so pi4 is in Setup

peterbmckinley commented 3 years ago

I have SSD1306 and also SH1106, its the same problem for both (after editing for the correct display)

ezgif com-video-to-gif

This is SSD1306

Krizzel87 commented 3 years ago

It Work now for you? No sh1106 ist not the Same SSD1306 both are oled 1.3 inch. Try to play with arduino to see Differenz. Ssd1306 is better

Krizzel87 commented 3 years ago

If your TFT run you of screen all the time correct the in oled.py

rows= 9

Try 8 or 7