-1

I've just written small python script to operate adafruit's lcd with additional module. Almost every lcd's operation is handled by module. But when I try to print a message on screen or in console, nothing happens. The only thing I can do is to clear the lcd.

python code:

#!/usr/bin/python
import math
import time
import sys
from time import time, sleep
import screen_manager
frameperiod=1.0/10
now=time()
nextframe=now+frameperiod
lcd = screen_manager.screen()
lcd.init
lcd.frameperiod = 10
while True:
#fps limiter
#    print (lcd.frame)
    while now<nextframe:
        sleep(nextframe-now)
        now=time()
    nextframe+=frameperiod
   # lcd.process #debug 
    lcd.frame=lcd.frame+1
    lcd.frame=lcd.frame%lcd.frameperiod

module:

#!/usr/bin/python
import math
import time
import thread
import socket
import json
import os
import sys
from time import gmtime, strftime
import Adafruit_CharLCD as LCD
# Initialize the LCD using the pins

class screen:
    lcd = LCD.Adafruit_CharLCDPlate()

    buttons = ( (LCD.SELECT, 0),
                (LCD.LEFT,   1),
                (LCD.UP,     2),
                (LCD.DOWN,   3),
                (LCD.RIGHT,  4) )

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    WS_address = ('172.16.1.2', 14500)
    temp_data = [("",-1,-1),("",-1,-1),("",-1,-1),("",-1,-1)]
    bitbay_data = -1
    button = 0
    is_pressed = 0
    pos = (0,0) # x,y
    menu_height = (0,4)
    frame = 0
    frameperiod = 0
    def Temp_update():
        sock.connect(WS_address)
        temp_data=sock.recv(2048)
        sock.close()
    def Clock():
        print "Clock"
        lcd.clear()
        lcd.message('Zegar\n')
        lcd.message(strftime("%H:%M:%S"))
        #Force update!
    def Temperature(temp_pos):
        thread.start_new_thread(Temp_update,())
        if temp_pos==0:
            lcd.clear()
            lcd.message('Temperatura')
        else:
            lcd.clear()
            lcd.message(temp_data[temp_pos][0]+'\n')
            lcd.message('T: ' + temp_data[temp_pos][1])
            if temp_data[temp_pos][2]!=-1:
                lcd.message(" H: "+ temp_data[temp_pos][2])
    menu = {
        (0,0) : Clock,
        (1,0) : Temperature,
        (1,1) : Temperature,
        (1,2) : Temperature,
        (1,3) : Temperature,
        (1,4) : Temperature,


    }
    def process():
        print "Process"
     #   lcd.message("debug")
        sleep(100)
        for tbutton in buttons:
            if lcd.is_pressed(tbutton[0]):
                is_pressed=1
        if is_pressed==1: #len(array)
            #move
            if button[1] == 1 and pos[1] == 0 and pos[0]>0:
                pos[0]=pos[0]-1 #left
            if button[1] == 4 and pos[1] == 0 and pos[0]<len(menu_height):
                pos[0]=pos[0]+1 #right
            if button[1] == 2 and pos[1]>0:
                pos[1]=pos[1]-1 #up
            if button[1] == 3 and pos[1]<=menu_height[pos[0]]:
                pos[1]=pos[1]+1
            is_pressed=0
        print pos
        if frame==0:
            menu[pos](pos[1])
        #todo button[1]=1?
        is_pressed=0
    def init():
        lcd.set_color(0.0, 1.0, 1.0)
        lcd.clear()
        lcd.message("Init")
        sleep(1000)
        print "Init"

If it's my mistake, I sorry, i'm green in python.

0

If you are unfamiliar with Python you are being ambitious to try to develop a big block of code without testing as you go along.

Note the difference between lcd.init and what you want which is lcd.init().

  • Ok, it turned out there was missing (self) in fuctions definition But now when it tries to launch function via dictionary (menu[pos](pos[1])) it says that 'menu' is not defined. So I moved menu outside the class, but then it doesn't find functions inside dictionary :/ – tyrtyrtyrtyr Jan 28 '17 at 10:15
  • @tyrtyrtyrtyr As I said I think you are being too ambitious. I would dump all the existing code and start again from scratch, testing each new feature you add. – joan Jan 28 '17 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.