RGB Led¶
Description¶
SensorIO has one high brightness, user programmable RGB Led (LD2
). Each color can be switched separately via toggling a pin or via PWM.
Code example¶
Compiling example
This example is based on Mbed OS and requires a simple setup. For full description check Code Setup page.
RGB Led driver¶
RGB Led driver via PWM can be downloaded here (header only library): link. Led control via pin toggling can be done with Mbed OS built-in DigitalOut
class.
Code¶
Pin definitions¶
1 2 3 4 5 6 7 8 | RGB_RED = PF_3, // TIM5_CH1 RGB_GREEN = PF_4, // TIM5_CH2 RGB_BLUE = PF_5, // TIM5_CH3 LED1 = RGB_GREEN, // Green LED2 = RGB_BLUE, // Blue LED3 = RGB_RED, // Red LED4 = LED1, LED_RED = LED3, |
Example¶
In this example user button is used to turn on and off RGB Led which toggles colors in HSV palette.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include "mbed_events.h" #include "InterruptIn.h" #include "Serial.h" #include "rgbled.h" #include "Ticker.h" mbed::Serial console(SERIAL_TX, SERIAL_RX, 115200); events::EventQueue scheduler; mbed::Ticker tickColor; RGBLed rgb(RGB_RED, RGB_GREEN, RGB_BLUE); bool ledOn = false; // Color toggling with HSV palette float h = 0.0f; float s = 0.75f; float v = 0.005f; void changeColor() { if(ledOn) { rgb.set(h, s, v); h += 0.44f; if(h > 360.0f) { h = 0.0f; } } else { rgb.set(0.0f, 0.0f, 0.0f); } } void scheduleColorChange() { scheduler.call(&changeColor); } void rgbLedStatus(int status) { console.printf("RGB Led is %s\r\n", (status == 1) ? "ON" : "OFF"); } void buttonPressed() { // toggle RGB Led state ledOn = !ledOn; // log RGB Led state via Serial (via EventLoop) scheduler.call(&rgbLedStatus, ledOn); } int main(int argc, char **argv) { console.printf("This is SensorIO\r\n"); mbed::InterruptIn button(USER_BUTTON); // attach function to button press button.fall(&buttonPressed); tickColor.attach(&scheduleColorChange, 1.0f/60.0f); // running event loop scheduler.dispatch_forever(); } |
Example main.cpp
file can be downloaded from here.