0

I have a python script to control some LEDS. In a compiled c++ program I have been calling python scripts like so in various places:

system("python /home/pi/leds_flash.py")

system("python /home/pi/leds_sleep.py)

Each of those function controls led lights accordingly and exits(). It's working great, but I really need my python script to keep state.

I am assuming that the best way to set things up is to create an instance of an LED controller script that remains running in the background on which I could invoke functions or send messages.

I've briefly been looking into how to run a script as a service, but still unsure how to invoke functions on this service or if this is even the best way to accomplish what I am after.

I'm a newb to raspberry pi and python so appreciate any guidance on this.

5
  • Would you please elaborate just a bit on what "state(s)" you need to keep track of?
    – Seamus
    Oct 22, 2018 at 16:19
  • I want to keep the state of the led lights - knowing the current status of any animation that they might be playing. Additionally, I have to run some initialization before controlling the light strips (neo-pixels) and my thinking is that it would be more performant to create that 1x and keep it in memory vs creating/destroying it over and over.
    – nwales
    Oct 22, 2018 at 17:26
  • Can't you read the state of the GPIO pin to get state?
    – Seamus
    Oct 22, 2018 at 17:32
  • No, not easily. It's not really a single high/low setting that is controlling the LEDS. I'm using github.com/jgarff/rpi_ws281x
    – nwales
    Oct 22, 2018 at 17:40
  • In general it is possible to run programs as service with systemd and you have very much possibilities to manage it, e.g. by setting the environment and rights or use dependencies to other services (start before/after). But it makes less sense to speculate if we do not have a program. I suggest that you make a simple systemd Unit file just only for starting your program and improve it step by step until it works.
    – Ingo
    Oct 22, 2018 at 23:38

1 Answer 1

0

I was able to solve my problem, though I don't know if there is a better or more efficient way to accomplish this. I created a Flask app to control the LEDs and have the C++ app making GET requests for on/off state.

1
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Jan 27, 2020 at 23:28

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.