Coder Social home page Coder Social logo

mausberry-switch's Introduction

mausberry-switch

This is a daemon for Raspberry PI devices that monitors GPIO pins 23 and 24, waiting for a low signal from a Mausberry Circuits switch in order to poweroff the system safely. It is intended to replace the official setup script.

Why not just use the official script available from from their website?

For reference, we're talking about this code:

while [ 1 = 1 ]; do
    cat /sys/class/gpio/gpio$GPIOpin1/value
    sleep 1
done

In my opinion, if there's a wrong way to monitor GPIO pins, it is a bash script. I do have to give credit for strong adherence to the KISS principle - 21 lines of bash is hard to beat.

However, on a Raspberry PI device, we're dealing with a processor that is a close relative to a piece of tinfoil. Just think about all the poor, wasted clock cycles that occur while polling the GPIO state repeatedly.

Alright, what about one of those Python scripts?

With raw access to GPIO and the ability to wait for GPIO events without wasting CPU time, Python is definitely a better way to go.

So let's say you have a Model B rev 2 board with 512MB of RAM. After you give some to the GPU, you only really have 400MB or 500MB left. Each instance of the Python interpreter takes up roughly 4MB, and I don't consider that to be a small loss.

Okay then. Prove that yours really is better!

There's some fine documentation on Linux kernel gpio/sysfs that tells us how the kernel can alert us when the value of a GPIO has changed. This eliminates the need for constant sampling of the GPIO. Here's how it works:

  1. Export the pins to userspace with /sys/class/gpio/export and set their direction with /sys/class/gpio/gpioN/direction.
  2. Set the value of /sys/class/gpio/gpioN/edge to setup an interrupt-generating pin.
  3. poll(2) on /sys/class/gpio/gpioN/value for the events POLLPRI and POLLERR
  4. Wait for poll to return and you've got a winner!

In the context of Linux system calls, poll means "wait for events to occur", as opposed to the traditional definition of polling, which is more along the lines of "actively sampling".

Here's the output of top showing the CPU and RAM usage of this program:

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
22682 root      20   0  1504  284  228 S   0.0  0.1   0:00.00 mausberry-switch

Alright, I'm convinced. How do I install this thing?

Here's a debian binary package of the latest version, compiled for armv6, little endian, hard float. You can install it on your PI by following these instructions.

  1. SSH to your Raspberry PI.
  2. Run these commands.
$ wget https://tomrichards.net/downloads/mausberry-switch_0.4_armhf.deb
$ sudo dpkg -i mausberry-switch_0.4_armhf.deb
$ sudo service mausberry-switch restart
$ sudo insserv mausberry-switch

If you want it on something else, I'll gladly accept a pull request.

mausberry-switch's People

Contributors

t-richards avatar

Watchers

 avatar  avatar

mausberry-switch's Issues

Cancel shutdown on car restart

Short stops (e.g. gas station) require that the shutdown is canceled and the Raspberry resumes normal operation when the car is restarted.

  • call "resume" script (e.g. turn on display)
  • keep GPIO high
  • go back to poll state

Execute script on ignition off

Some stuff needs to be done when the car is turned of. e.g. switching off the display.
For this we need the posibility to execute a script.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.