yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Lua cpu usage

Wed Nov 20, 2013 9:15 pm

question here for you Pi folks I have installed lua on my Pi to work with the GPIO I need to ask a stupid question I will be monitoring several reed switches so for CPU usage should I use while loops or if statements as about 80% of the time the status of the switches will not change.

Sorry I know this is a odd question,
Yonu

User avatar
DougieLawson
Posts: 36528
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Lua cpu usage

Wed Nov 20, 2013 9:42 pm

There are two ways to deal with a GPIO signal when you don't know when it's due to arrive.
  1. Spin in a loop testing the pin until it changes
  2. Sit in a wait until an interrupt posts you as ready
Clearly the second option is better because while you're in a wait-state your processor can be doing something else that's productive. The first option will tend towards 100% cpu busy. You can include a sleep(...) function but you may miss the signal arriving.

So you need to explore whether your Lua GPIO library can handle interrupt driven processes or not. If not, switch to python or c/c++ which can handle interrupts.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Lua cpu usage

Thu Nov 21, 2013 12:20 am

yonubear wrote:question here for you Pi folks I have installed lua on my Pi to work with the GPIO I need to ask a stupid question I will be monitoring several reed switches so for CPU usage should I use while loops or if statements as about 80% of the time the status of the switches will not change.

Sorry I know this is a odd question,
Yonu
As you state Lua I am going to assume that you are using RISC Lua on RISC OS with the GPIO Module installed and are using the SWI implementation in RISC Lua to call to it. Now with these assumptions I think it would be more effecient to use a callback, though I believe that this would take an exorbant amount of code to set up for doing something so simple.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: Lua cpu usage

Thu Nov 21, 2013 12:29 am

ok actually I am running Raspbian with lua 5.1 installed and I compiled the GPIO module found here http://www.andre-simon.de/doku/rpi_gpio ... o_lua.html
the main reason I am using Lua is for the use of Xpl well that and all my other automation code for other software is in Lua

Heater
Posts: 13868
Joined: Tue Jul 17, 2012 3:02 pm

Re: Lua cpu usage

Thu Nov 21, 2013 7:35 am

As far as I know there is no way to get an interrupt from a pin to execute a Lua function.

That means you will have to poll the pin state at regular intervals. That means your CPU usage will go to 100% unless you have a sleep in there somewhere. Then CPU usage will depend on how often you do the polling.

Presumably your program has a main loop and you can just read the pin somewhere in there. So the answer seems to be that you will have a "while" or other loop construct and "if" to test the pin state.
Memory in C++ is a leaky abstraction .

yonubear
Posts: 90
Joined: Thu Sep 12, 2013 11:11 pm

Re: Lua cpu usage

Thu Nov 21, 2013 1:10 pm

Ok thanks would you know if there is a xpl module for Python? As I would prefer interuppts after reading about them

Heater
Posts: 13868
Joined: Tue Jul 17, 2012 3:02 pm

Re: Lua cpu usage

Thu Nov 21, 2013 3:51 pm

Memory in C++ is a leaky abstraction .

Return to “Other programming languages”