Scroll pHAT HD scroll blocking?

A bit of a question and sorry if I ramble as it’s late!

I am writing a program where I am reading values from an accelerometer to try and count steps. I then want my Scroll pHAT HD to scroll the number of steps taken across the screen…

My current thinking for the pseudocode is along these lines…

  1. start loop
  2. read accelerometer axis
  3. if above high threshold mark step
  4. wait until low threshold
  5. increment count to file on hdd
  6. if file count has increased by 5 scroll file count on scroll phat hd
  7. go to 1

My question is; if I start the scroll at step 6, does the scroll and show function of the scrollphathd library block my program, or will I immediately return to step 1 whilst the display scrolls?

I really don’t want to have to use processes as I think my head might explode!

Cheers,
Jon

So, answering my own question… Yes it does block!

Here’s the Python program I ended using to get a basic pedometer with the Scroll pHAT HD and an Adafruit ADXL345 attached to a Pi Zero…

#! /usr/bin/python3

# import our libraries
from adxl345 import ADXL345
from time import sleep
import scrollphathd as shd
from scrollphathd.fonts import font3x5

# setup our variables and objects
adxl345 = ADXL345()
pedCnt = 0
Thresh = 1.15
last_state = 'below'

# define two functions to display messages
def scrollMsgOnce(msg):
    msg = msg + "      " # 6 spaces
    buffer = shd.write_string(msg, x=17, y=0, brightness=0.5)
    for i in range(buffer):
        shd.show()
        shd.scroll(1)
        sleep(0.05)
    shd.clear()
    shd.show()

def showMsgOnce(msg):
    shd.clear()
    shd.show()
    shd.write_string(msg, x=0, y=1, font=font3x5, brightness=0.25)
    shd.show()

# clear our screen and display a welcome message
shd.clear()
shd.show()
scrollMsgOnce("Raspberry Pi Zero Pedometer!")
sleep(0.5)

# start counting our steps
while True:
    # get our axes reading
    axes = adxl345.getAxes(True)
    # set current_sate - above or below
    # starts as below
    current_state = last_state
    if axes['x'] < Thresh:
        # we are still below threshold
        current_state = 'below'
    elif axes['x'] > Thresh:
        # we have gone above threshold
        current_state = 'above'

    if current_state is not last_state:
        # we have a change in state
        if current_state is 'above':
            # that change is to go above
            # so increment count and display it
            pedCnt += 1
            showMsgOnce(str(pedCnt))

    # set last state to current
    last_state = current_state

Using threading in Python actually is quite simple, and often sufficient. I did my first successful steps not long ago, and was surprised how quickly I could achieve my goal. I basicically have now two threads running…

One thread handles rendering the display of the Scroll HD pHAT and scrolling. In a loop I check for certain data value changes, and render the display anew based on this data. Otherwise, I simply scroll the display and sleep for a short period.

The other thread does crazy stuff, such as monitoring some system configuration files and network address configuration. If something changes, this thread gets the interesting data. Nothing more here. In your case, this would be reacing the accelerator sensor and processing its data. No need to worry here about handling the display.

Using two threads allows me to check the incoming data at a completely different rate than scrolling. I even now use the Blue Dot Android app to remotely control scrolling via Bluetooth to my Raspberry Pi Zero W. I can start/stop scrolling and scroll forth and back…

As threading uses the GIL global interpreter lock, for basic data taypes, you don’t need any semaphores to control access to variables. This should give you a smooth and easy introduction to Python’s threading.