Posts

Showing posts with the label Timer

Working with state in MicroPython Timer callbacks

Image
MicroPython supports hardware and software timers with callbacks. Timers can be configured as single-shot or periodic events meaning they trigger only once or on a periodic basis. You can bind a Python function to the timer running in either mode.  Your function will receive a  callback  whenever the timer rolls over or expires.  Hardware timers are bound to the actual CPU timers and typically correlate 1:1 to hardware devices.  This means the hardware timer configuration is hardware specific to the size and capabilities of the hardware timers.  In general, you can only tie one callback to each timer because the callback is bound to the hardware timer interrupt handler.   MicroPython also supports software timers. Software timers are the only option on CPUs like the ESP8266 where hardware timers are scarce or are dedicated to other functions. They have the same callback/handler restrictions as hardware timers. Video Content YouTube:  Working ...

Running timed background tasks on IoT devices the easy way with MicroPython

Image
MicroPython is single-threaded but it turns out it is easy to have it do periodic tasks while doing other work like waiting on network requests when running a web server. Here we demonstrate a simple wrapper class that makes it trivial to trigger actions with a timer while at the same time accepting web requests. Everything discussed here runs on a simple ESP8266 IOT board. The code is on Github https://github.com/freemansoft/ESP8266-MicroPython Web Server gives direct control and enables Timed Events The IoT device runs a small web server written in MicroPython.   It coughs up a web page with a series of device controls.  This image contains a direct servo pin control and a timed operations control.  The latter basically causes some action to be taken on a regular basis, in this case flashing an LED.  This happens continually while the system waits for and receives HTTP requests on this UI. We can move the servo around while the light flashes without any com...

Energia: Code on a Timer with Tiva / Stellaris Launchpads

Image
Do you wish you could smoothly fade LEDs without having to code that into the main loop of your program?  Do you wish you could sample sensors or input ports an a regular basis while doing other activities in the main part of your Energia program?  Are you looking for simple ways of creating periodic activities without having to worry about timing loops or how long other processing takes in your loop() function?  If so, then you want to use a timer interrupts! This post describes how to run time based code in a Tiva/Stellaris Launchpad with 1ms precision. The same code runs on the MSP430 Launchpad with 2ms precision. Your function will be invoked at the interval you register without having to integrate any code in your run loop. The timer is in github along with a simple blinking light demo program. Usage AnyMsTimer calls a function()  on regular intervals independent of work you are doing in the the loop() function or the functions it calls. Register...