|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * SPDX-License-Identifier: BSD-3-Clause |
| 3 | + ****************************************************************************** |
| 4 | + * |
| 5 | + * Copyright (c) 2021 STMicroelectronics. |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * This software component is licensed by ST under BSD 3-Clause license, |
| 9 | + * the "License"; You may not use this file except in compliance with the |
| 10 | + * License. You may obtain a copy of the License at: |
| 11 | + * opensource.org/licenses/BSD-3-Clause |
| 12 | + * |
| 13 | + ****************************************************************************** |
| 14 | + */ |
| 15 | + |
| 16 | +/* application example |
| 17 | +
|
| 18 | +#include "mbed.h" |
| 19 | +
|
| 20 | +int main() |
| 21 | +{ |
| 22 | + while (true) { |
| 23 | + rgb_led_off(); |
| 24 | + ThisThread::sleep_for(2s); |
| 25 | +
|
| 26 | + rgb_led_red(); |
| 27 | + ThisThread::sleep_for(2s); |
| 28 | +
|
| 29 | + rgb_led_green(); |
| 30 | + ThisThread::sleep_for(2s); |
| 31 | +
|
| 32 | + rgb_led_blue(); |
| 33 | + ThisThread::sleep_for(2s); |
| 34 | +
|
| 35 | + rgb_led_on(0x41, 0x41, 0); // yellow |
| 36 | + ThisThread::sleep_for(2s); |
| 37 | +
|
| 38 | + rgb_led_on(0x41, 0, 0x41); // magenta |
| 39 | + ThisThread::sleep_for(2s); |
| 40 | +
|
| 41 | + rgb_led_on(0, 0x41, 0x41); // cyan |
| 42 | + ThisThread::sleep_for(2s); |
| 43 | +
|
| 44 | + rgb_led_on(0x41, 0x41, 0x41); // white |
| 45 | + ThisThread::sleep_for(2s); |
| 46 | + } |
| 47 | +} |
| 48 | +*/ |
| 49 | + |
| 50 | +#include "drivers/DigitalOut.h" |
| 51 | +#include "platform/mbed_wait_api.h" |
| 52 | + |
| 53 | + |
| 54 | +#define DELAY 1 |
| 55 | +#define T_CYCLE_0 4 |
| 56 | +#define T_CYCLE_1 1 |
| 57 | +#define WRITE_COMMAND 0x3A |
| 58 | + |
| 59 | +#define rgb_led_wait(NbCycles) wait_us(NbCycles * 10) |
| 60 | + |
| 61 | + |
| 62 | +void rgb_led_send_bit(uint8_t bit) |
| 63 | +{ |
| 64 | + mbed::DigitalOut RGB_LED_SDI(RGB_LED); |
| 65 | + |
| 66 | + /* Start next cycle */ |
| 67 | + RGB_LED_SDI = 1; |
| 68 | + rgb_led_wait(DELAY); |
| 69 | + RGB_LED_SDI = 0; |
| 70 | + rgb_led_wait(DELAY); |
| 71 | + |
| 72 | + if (bit) { |
| 73 | + RGB_LED_SDI = 1; |
| 74 | + rgb_led_wait(DELAY); |
| 75 | + RGB_LED_SDI = 0; |
| 76 | + rgb_led_wait(T_CYCLE_1); |
| 77 | + } else { |
| 78 | + rgb_led_wait(T_CYCLE_0); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +void rgb_led_send_byte(uint8_t byte) |
| 84 | +{ |
| 85 | + rgb_led_send_bit(byte & (1 << 7)); |
| 86 | + rgb_led_send_bit(byte & (1 << 6)); |
| 87 | + rgb_led_send_bit(byte & (1 << 5)); |
| 88 | + rgb_led_send_bit(byte & (1 << 4)); |
| 89 | + rgb_led_send_bit(byte & (1 << 3)); |
| 90 | + rgb_led_send_bit(byte & (1 << 2)); |
| 91 | + rgb_led_send_bit(byte & (1 << 1)); |
| 92 | + rgb_led_send_bit(byte & (1 << 0)); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +void rgb_led_on(uint8_t RedValue, uint8_t GreenValue, uint8_t BlueValue) |
| 97 | +{ |
| 98 | + mbed::DigitalOut RGB_SELECT(GPIO_SELECT2); |
| 99 | + mbed::DigitalOut RGB_LED_SDI(RGB_LED); |
| 100 | + |
| 101 | + RGB_SELECT = 1; |
| 102 | + |
| 103 | + /* TCycle measurement sequence */ |
| 104 | + RGB_LED_SDI = 1; |
| 105 | + rgb_led_wait(DELAY); |
| 106 | + RGB_LED_SDI = 0; |
| 107 | + rgb_led_wait(T_CYCLE_0); |
| 108 | + |
| 109 | + /* Write command */ |
| 110 | + rgb_led_send_byte(WRITE_COMMAND); |
| 111 | + |
| 112 | + /* Write the GS data */ |
| 113 | + rgb_led_send_byte(RedValue); |
| 114 | + rgb_led_send_byte(GreenValue); |
| 115 | + rgb_led_send_byte(BlueValue); |
| 116 | + |
| 117 | + rgb_led_wait(T_CYCLE_0 * 2); |
| 118 | + |
| 119 | + RGB_SELECT = 0; |
| 120 | +} |
| 121 | + |
| 122 | +void rgb_led_off(void) |
| 123 | +{ |
| 124 | + rgb_led_on(0, 0, 0); |
| 125 | +} |
| 126 | + |
| 127 | +void rgb_led_red(void) |
| 128 | +{ |
| 129 | + rgb_led_on(0x41, 0, 0); |
| 130 | +} |
| 131 | + |
| 132 | +void rgb_led_green(void) |
| 133 | +{ |
| 134 | + rgb_led_on(0, 0x41, 0); |
| 135 | +} |
| 136 | + |
| 137 | +void rgb_led_blue(void) |
| 138 | +{ |
| 139 | + rgb_led_on(0, 0, 0x41); |
| 140 | +} |
| 141 | + |
0 commit comments