Future Electronics
/
sequana-rgb-led-example
Example program to blink RGB LEDs on Sequana board in a rotating pattern.
main.cpp@0:da2de84c3c04, 2019-02-05 (annotated)
- Committer:
- lru
- Date:
- Tue Feb 05 13:58:47 2019 +0000
- Revision:
- 0:da2de84c3c04
- Child:
- 2:a83100cd9c9a
Initial revision for online IDE.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lru | 0:da2de84c3c04 | 1 | /* |
lru | 0:da2de84c3c04 | 2 | * Mbed OS example program for Future Electronics Sequana board. |
lru | 0:da2de84c3c04 | 3 | * |
lru | 0:da2de84c3c04 | 4 | * Copyright (c) 2018 Future Electronics |
lru | 0:da2de84c3c04 | 5 | * |
lru | 0:da2de84c3c04 | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lru | 0:da2de84c3c04 | 7 | * you may not use this file except in compliance with the License. |
lru | 0:da2de84c3c04 | 8 | * You may obtain a copy of the License at |
lru | 0:da2de84c3c04 | 9 | * |
lru | 0:da2de84c3c04 | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
lru | 0:da2de84c3c04 | 11 | * |
lru | 0:da2de84c3c04 | 12 | * Unless required by applicable law or agreed to in writing, software |
lru | 0:da2de84c3c04 | 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
lru | 0:da2de84c3c04 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lru | 0:da2de84c3c04 | 15 | * See the License for the specific language governing permissions and |
lru | 0:da2de84c3c04 | 16 | * limitations under the License. |
lru | 0:da2de84c3c04 | 17 | */ |
lru | 0:da2de84c3c04 | 18 | |
lru | 0:da2de84c3c04 | 19 | // This example demonstrates used of ROHM BD2808 24-channel LED Controller |
lru | 0:da2de84c3c04 | 20 | // and its driver library on Sequana board. |
lru | 0:da2de84c3c04 | 21 | |
lru | 0:da2de84c3c04 | 22 | #include "mbed.h" |
lru | 0:da2de84c3c04 | 23 | #include "BD2808.h" |
lru | 0:da2de84c3c04 | 24 | |
lru | 0:da2de84c3c04 | 25 | BD2808 leds; |
lru | 0:da2de84c3c04 | 26 | |
lru | 0:da2de84c3c04 | 27 | BGR24_color_t dim(BGR24_color_t color, uint8_t percent) |
lru | 0:da2de84c3c04 | 28 | { |
lru | 0:da2de84c3c04 | 29 | BGR24_color_t new_color; |
lru | 0:da2de84c3c04 | 30 | |
lru | 0:da2de84c3c04 | 31 | new_color.r = color.r * percent / 100; |
lru | 0:da2de84c3c04 | 32 | new_color.g = color.g * percent / 100; |
lru | 0:da2de84c3c04 | 33 | new_color.b = color.b * percent / 100; |
lru | 0:da2de84c3c04 | 34 | |
lru | 0:da2de84c3c04 | 35 | return new_color; |
lru | 0:da2de84c3c04 | 36 | } |
lru | 0:da2de84c3c04 | 37 | |
lru | 0:da2de84c3c04 | 38 | #define sat(x) (uint8_t)((x) > 255? 255 : (x) < 0? 0 : (x)) |
lru | 0:da2de84c3c04 | 39 | |
lru | 0:da2de84c3c04 | 40 | BGR24_color_t gen_color() |
lru | 0:da2de84c3c04 | 41 | { |
lru | 0:da2de84c3c04 | 42 | const uint8_t step = 20; |
lru | 0:da2de84c3c04 | 43 | const uint8_t max = 200; |
lru | 0:da2de84c3c04 | 44 | static uint32_t i = 0; |
lru | 0:da2de84c3c04 | 45 | static uint32_t j = 0; |
lru | 0:da2de84c3c04 | 46 | static int32_t r = max; |
lru | 0:da2de84c3c04 | 47 | static int32_t g = max; |
lru | 0:da2de84c3c04 | 48 | static int32_t b = 0; |
lru | 0:da2de84c3c04 | 49 | static uint8_t pull = 0; |
lru | 0:da2de84c3c04 | 50 | |
lru | 0:da2de84c3c04 | 51 | if (++j == 6) |
lru | 0:da2de84c3c04 | 52 | { |
lru | 0:da2de84c3c04 | 53 | j = 0; |
lru | 0:da2de84c3c04 | 54 | if ( i < 10) |
lru | 0:da2de84c3c04 | 55 | { |
lru | 0:da2de84c3c04 | 56 | b += step; |
lru | 0:da2de84c3c04 | 57 | r -= step; |
lru | 0:da2de84c3c04 | 58 | } |
lru | 0:da2de84c3c04 | 59 | else if (i < 20) |
lru | 0:da2de84c3c04 | 60 | { |
lru | 0:da2de84c3c04 | 61 | r += step; |
lru | 0:da2de84c3c04 | 62 | g -= step; |
lru | 0:da2de84c3c04 | 63 | } |
lru | 0:da2de84c3c04 | 64 | else if (i < 30) |
lru | 0:da2de84c3c04 | 65 | { |
lru | 0:da2de84c3c04 | 66 | g += step; |
lru | 0:da2de84c3c04 | 67 | b -= step; |
lru | 0:da2de84c3c04 | 68 | } |
lru | 0:da2de84c3c04 | 69 | |
lru | 0:da2de84c3c04 | 70 | if (++i >= 30) { |
lru | 0:da2de84c3c04 | 71 | i = 0; |
lru | 0:da2de84c3c04 | 72 | } |
lru | 0:da2de84c3c04 | 73 | |
lru | 0:da2de84c3c04 | 74 | if (i == 0) { |
lru | 0:da2de84c3c04 | 75 | if ((pull += 20) > 180) { |
lru | 0:da2de84c3c04 | 76 | pull = 0; |
lru | 0:da2de84c3c04 | 77 | } |
lru | 0:da2de84c3c04 | 78 | g = max - pull; |
lru | 0:da2de84c3c04 | 79 | r = max ;//+ pull/2; |
lru | 0:da2de84c3c04 | 80 | b = pull / 2; |
lru | 0:da2de84c3c04 | 81 | } else if (i == 10) { |
lru | 0:da2de84c3c04 | 82 | b = max - pull; |
lru | 0:da2de84c3c04 | 83 | r = pull/2; |
lru | 0:da2de84c3c04 | 84 | g = max ;//+ pull/2; |
lru | 0:da2de84c3c04 | 85 | } else if (i == 20) { |
lru | 0:da2de84c3c04 | 86 | r = max - pull; |
lru | 0:da2de84c3c04 | 87 | g = pull / 2; |
lru | 0:da2de84c3c04 | 88 | b = max ;//+ pull/2; |
lru | 0:da2de84c3c04 | 89 | } |
lru | 0:da2de84c3c04 | 90 | } |
lru | 0:da2de84c3c04 | 91 | |
lru | 0:da2de84c3c04 | 92 | return BGR24_color_t(sat(b), sat(g), sat(r)); |
lru | 0:da2de84c3c04 | 93 | } |
lru | 0:da2de84c3c04 | 94 | |
lru | 0:da2de84c3c04 | 95 | |
lru | 0:da2de84c3c04 | 96 | #define NUM_LEDS_LIGHTING 3 |
lru | 0:da2de84c3c04 | 97 | |
lru | 0:da2de84c3c04 | 98 | uint32_t led_index[NUM_LEDS_LIGHTING] = {0, 1, 2}; |
lru | 0:da2de84c3c04 | 99 | BGR24_color_t current_col[NUM_LEDS_LIGHTING]; |
lru | 0:da2de84c3c04 | 100 | |
lru | 0:da2de84c3c04 | 101 | // main() runs in its own thread in the OS |
lru | 0:da2de84c3c04 | 102 | int main() { |
lru | 0:da2de84c3c04 | 103 | |
lru | 0:da2de84c3c04 | 104 | leds.set_dma_usage(DMA_USAGE_ALWAYS); |
lru | 0:da2de84c3c04 | 105 | |
lru | 0:da2de84c3c04 | 106 | while (true) { |
lru | 0:da2de84c3c04 | 107 | for (int i = 0; i < NUM_LEDS_LIGHTING; ++i) { |
lru | 0:da2de84c3c04 | 108 | leds.set_color(led_index[i], current_col[i]); |
lru | 0:da2de84c3c04 | 109 | } |
lru | 0:da2de84c3c04 | 110 | leds.refresh(); |
lru | 0:da2de84c3c04 | 111 | |
lru | 0:da2de84c3c04 | 112 | wait_ms(50); |
lru | 0:da2de84c3c04 | 113 | leds.set_color(led_index[0], BGR24_color_t(0,0,0)); |
lru | 0:da2de84c3c04 | 114 | |
lru | 0:da2de84c3c04 | 115 | for (int i = 0; i < NUM_LEDS_LIGHTING; ++i) { |
lru | 0:da2de84c3c04 | 116 | if (++led_index[i] == 8) led_index[i] = 0; |
lru | 0:da2de84c3c04 | 117 | } |
lru | 0:da2de84c3c04 | 118 | current_col[2] = gen_color(); |
lru | 0:da2de84c3c04 | 119 | current_col[1] = dim(current_col[2], 50); |
lru | 0:da2de84c3c04 | 120 | current_col[0] = dim(current_col[2], 15); |
lru | 0:da2de84c3c04 | 121 | } |
lru | 0:da2de84c3c04 | 122 | } |
lru | 0:da2de84c3c04 | 123 |