Hi,
I'm extremely new to Nordic (and embedded development in general), but I'm writing some code that turns on various LED colors based on input from an Android app (BLE). This app may send multiple channels right after the other (e.g. R=100,G=90,B=80), so multiple things have to be updated. It also has a transition period so that colors are slowly scaled up, rather than immediately jumping.
Currently I'm using event handlers to set a global variable when a new channel value comes in. Then there's a busy loop in main() that updates each channel accordingly.
while(1){
if (red_now < red_goal)
*(color_channels.red) = ++red_now;
if (red_now > red_goal)
*(color_channels.red) = --red_now;
...
nrf_delay_us(200);
}
This works well enough, but I'd like to learn how to do this without a busy loop. If I update everything within each event handler, it will do only one channel at a time - so it would turn red, then red+green, and finally red+green+blue.
So ideally, I'd like to interrupt this event handler when new data comes in, and restart the update_levels() function, rather than waiting for update_levels() to complete before updating global variables.
I've tried writing an update_levels() like this but it has the same results as original (transitioning each channel separately).
void update_levels(){
while (red_now != red_goal ||
blue_now != blue_goal ||
green_now != green_goal) {
if (red_now < red_goal)
*(color_channels.red) = ++red_now;
if (red_now > red_goal)
*(color_channels.red) = --red_now;
...
nrf_delay_us(200);
}
}
Development setup:
OS: Linux
Devkit: PCA10040 3.0.0
Segger version: SES for ARM V4.18
SDK version: 17.02
Sorry if this is super basic, just trying to learn :)