Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Problem: Detect state changes
#7
(02-05-2021, 10:04 PM)Tim Curtis Wrote:
(02-05-2021, 02:55 PM)steven4build Wrote: Hello everyone,

I'm new to this community and have recently discovered Moode.

My project:
I'm building a smart speaker that works on a Raspberry Pi Zero W. The speaker also has a WS2812b LED-strip and some buttons for like play/pause, next/previous track.

I got the button part working with a python script that executes commands like this:
Code:
import RPi.GPIO as GPIO
import time
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True: # Run forever
   input_state = GPIO.input(16)
   if input_state == GPIO.LOW:
       print("Button was pushed!")
       os.system("mpc toggle")
       time.sleep(0.2)

for the LED-strip part, I want to display some animations when; song changes, raising of lowering volume (with current percentage), changing sources, etc.


So for example when a song ends and it autoplays to the next one, it should display an animation on the LED-strip.

is there a way to check for statechanges for these situations?
I've read some form posts on the subject, but can't really find a clean solution.

Have a look in System Config for the GPIO button handler, Metadata file and LCD update engine.

Regarding the LCD update engine, you would edit the required script /var/local/www/commandw/lcdup.py to parse the metadata file /var/local/www/currentsong.txt and do whatever is needed to control your peripheral device. This file is updated within 3 secs anytime playback state changes. The WebUI does not have to be running.

An important aspect of the lcdup.py script is that it must not contain a while loop since the LCD update engine provides event based execution of the script via an inotify wait on the currentsong.txt file.

Thanks!

I found the setting and will definitetly use that!

So if I understand correctly, you're suggesting to do this in lcdup.py:
Code:
with open("/var/local/www/currentsong.txt") as file1:
    # 1. Decode currentsong.txt (btw. is there a good looking way to do this?)
    # 2. Findout what changed
    # 3. Do something accordingly to that event
    
    with open("/home/pi/lcd.txt", "w") as file2:
        for line in file1:
            file2.write(line)

Thank you for your help!

Regards,

Steven
Reply


Messages In This Thread
Detect state changes - by steven4build - 02-05-2021, 02:55 PM
RE: Detect state changes - by TheOldPresbyope - 02-05-2021, 03:17 PM
RE: Detect state changes - by steven4build - 02-05-2021, 06:29 PM
RE: Detect state changes - by TheOldPresbyope - 02-05-2021, 09:51 PM
RE: Detect state changes - by Tim Curtis - 02-05-2021, 10:04 PM
RE: Detect state changes - by steven4build - 02-07-2021, 11:56 AM
RE: Detect state changes - by bitlab - 02-06-2021, 07:49 PM
RE: Detect state changes - by steven4build - 02-07-2021, 11:58 AM
RE: Detect state changes - by steven4build - 02-10-2021, 06:27 PM
RE: Detect state changes - by Tim Curtis - 02-10-2021, 08:10 PM
RE: Detect state changes - by hindumagic - 02-11-2021, 02:18 AM
RE: Detect state changes - by Tim Curtis - 02-11-2021, 03:26 AM
RE: Detect state changes - by hindumagic - 02-11-2021, 06:28 AM

Forum Jump: