LCD connected check

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ulrich
Posts: 14
Joined: Thu Nov 27, 2014 7:17 pm

LCD connected check

Post by ulrich » Sat Feb 28, 2015 6:50 pm

Good evening,

Playing with my shiny new LCD skin, I wonder if there is a way for a script to actually check if a LCD skin is attached or not.

If it's not attached, the a class instance can be instanciated without problem and functions like .write() be called as well without error.

My motivation: After a reset, I'd like to have the pyboard check if there is a LCD and react depedinginly.

Any hints are very much apprecheated!

Greetings from Hamburg,

Ulrich

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: LCD connected check

Post by dhylands » Sun Mar 01, 2015 5:11 am

With the LCD skin attached you should see a device with an address of 90 when you do i2c.scan():

Code: Select all

>>> i2c = pyb.I2C(2, pyb.I2C.MASTER)
>>> i2c.scan()
[90]
This is actually the device for dealing with the touch buttons, but it is part of the LCD shield.

ulrich
Posts: 14
Joined: Thu Nov 27, 2014 7:17 pm

Re: LCD connected check

Post by ulrich » Sun Mar 01, 2015 9:33 am

Hi Dave,

Thanks a lot - once again, you helped me a lot!

I wonder if I may bother you (or another pyboard-expert) with another somewhat related issue:

- I have the LCD-skin attached in the Y-configuration
- I want the board to boot into different modes (+ do some other stuff) depending on if a button is pressed or not. For this purpose, I've added a boot.py file on the SD-card:

Code: Select all

import pyb

pyb.LED(3).on()                 # indicate we are waiting for switch press

try:
    import mpr121
    touchbuttons = mpr121.MPR121(pyb.I2C(2, pyb.I2C.MASTER))
    lcd = pyb.LCD('Y')
    lcd.light(True)
except:
    pass

pyb.delay(2000)                 # wait for user to maybe press the switch
switch_value = pyb.Switch()()   # sample the switch at end of delay
pyb.LED(3).off()                # indicate that we finished waiting for the switch

pyb.LED(4).on()                 # indicate that we are selecting the mode

try:
    touchstatus = touchbuttons.touch_status()
except:
    touchstatus = False    

if switch_value or touchstatus:
    pyb.usb_mode('CDC+HID')
    lcd.write('Button was pressed \nTouch: %s'%str(touchstatus))
else:
    pyb.usb_mode('CDC+MSC')
    lcd.write('Touch: %s'%str(touchstatus))

pyb.LED(4).off()                # indicate that we finished selecting the mode
Now this works nicely if I use the boards reset button (which is quite hard to reach with the board in Y-position...), but does not work when I power the board up by connecting it to a power source. While the script is running nicely (LEDS work, display light is turned on etc), but the button-touch is simply ignored.

Anyone got a clue what to do?

Thanks a lot in advance

hansel
Posts: 13
Joined: Wed Feb 11, 2015 7:34 pm
Location: Rotterdam, NL
Contact:

Re: LCD connected check

Post by hansel » Sun Mar 01, 2015 12:36 pm

Just guessing here... maybe the lcd module is not ready yet? You could try a small delay at the start of the script...

ulrich
Posts: 14
Joined: Thu Nov 27, 2014 7:17 pm

Re: LCD connected check

Post by ulrich » Sun Mar 01, 2015 5:42 pm

HI Hansel,

I had in my script a delay of 2 seconds for this reason, but it turns out that this is not sufficient.

To see how much delay I need, I've timed it:
import pyb
pyb.LED(3).on() # indicate we are waiting for switch press

import mpr121
touchbuttons = mpr121.MPR121(pyb.I2C(2, pyb.I2C.MASTER))
lcd = pyb.LCD('Y')
lcd.light(True)

bikeboot = False
for i in range(0, 1000):
switch_value = pyb.Switch()() # sample the switch at end of delay
if switch_value:
break
touchstatus = touchbuttons.touch_status()
if touchstatus:
break
pyb.delay(10) # wait for user to maybe press the switch

pyb.LED(3).off() # indicate that we finished waiting for the switch

if switch_value or touchstatus:
lcd.write('i: %s'%str(i))
else:
lcd.write('Touch: %s'%str(touchstatus))
Running this multiple times, I find
- the switch is available pretty fast
- the buttons take more than 8(!) seconds to get reactive

Having no clue about the internals, this still seems a long time for me.

Cheers,

Ulrich

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: LCD connected check

Post by kfricke » Sun Mar 01, 2015 9:42 pm

I have no LCD skin, so this are only my stolen 50 cent...

The datasheet of the touch sensing chip tells something about long duration for the baseline calibration of the touch sensing electrode. It can initially take some time to enter "run mode" for the touch sensing electrodes. See page 16 and especially the last paragraph.
http://micropython.org/resources/datasheets/MPR121.pdf

I'd suggest to play around with the upper two bits of the register (CL field, bits 6 and 7) when initializing the LCD touch sensors on the I²C bus. The examples do use the default value which needs some time for the baseline calibration. Those can be preset with vaules from some touch sensor values on startup.

ulrich
Posts: 14
Joined: Thu Nov 27, 2014 7:17 pm

Re: LCD connected check

Post by ulrich » Mon Mar 02, 2015 5:03 pm

Good evening,

Thanks for the hint - your post confirms me that I'm not simply making a stupid mistake. Unfortunately I'm not fit enough to play around and follow up your comments - That's why the pyboard is so attractive for me - being able to use my normal python knowlege and being able to program a micro-controller! But one day, I'll hopefully find the time to learn it. Till then I'll simply wait 9 seconds.

Cheers,
Ulrich

Post Reply