pimoroni / scroll-phat-hd

Python library for Scroll pHAT HD
https://shop.pimoroni.com/products/scroll-phat-hd
MIT License
159 stars 64 forks source link

Update cpu.py #38

Closed raspberrycoulis closed 6 years ago

raspberrycoulis commented 7 years ago

Added options to turn off display when script is stopped, and added additional comments to the code for learning purposes.

Gadgetoid commented 7 years ago

While I appreciate the effort to share your tweaks, I don't think they quite work as changes to the current example, since that's trying to demonstrate one thing clearly without conflating it with techniques to run as a system service.

I wonder if there should be a contributed folder for examples that meet rather more specific use-cases.

I could think of a few tweaks to your modified example that would make it even better as a system daemon and then that, in its own right, would make a great little advanced example.

raspberrycoulis commented 7 years ago

Hi @Gadgetoid,

Thanks for that - to be honest, I wasn't sure whether it was worth me submitting this PR in the first place, but maybe having a contributions folder would be good moving forward - maybe even having the option to install this when your customers first install the code with a Y/N option?

My initial PR here was to tidy things up, rather than leaving the example a little unfinished in my opinion. Hope this helps.

Gadgetoid commented 6 years ago

Honestly- if in doubt, always submit as a PR! Even if it doesn't get approved, it's great to see what people are doing with these libraries and what sorts of things it might be worth demonstrating in examples.

Submissions, suggestions and so on are always greatly appreciated!

dglaude commented 6 years ago

Above and beyond a contribution folder, some place at the end of the readme.md in example that list/link other use of the library in project could be an option.

If I had a complex or specific use of @pimoroni hardware XXX and it's library, I would love to share and give it exposure while maintaining that on github or a blog or else.

My PR would then be just a link and short description of what is behind the link.

Over time a big list of usage example could exist. Current owner of the XXX could use that for ideas on how to use it, future owner will have reason to acquire one XXX.

Gadgetoid commented 6 years ago

This example isn't far from being a good self-contained pattern for running a "daemon" style version of a Python script. I'd probably make a couple of changes though:

As a simple pattern:

import time
import signal

import scrollphathd

running = True

def signal_handler(signum, frame):
    global running
    running = False

signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)

while running:
    # do stuff
    time.sleep(0.2)

scrollphathd.clear()

There's no need to have try / except KeyboardInterrupt since a KeyboardInterrupt will trigger SIGINT, hand off to signal_handler, set running = False and the loop will break anyway.

You could probably get creative with SIGUSR1 and SIGUSR2 if you wanted!