Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PinDetect Data_Clock_Pair Seeed_Chainable_LED
SeeedChainableLED.h@7:018155c80657, 2017-05-21 (annotated)
- Committer:
- tulanthoar
- Date:
- Sun May 21 13:03:32 2017 -0600
- Revision:
- 7:018155c80657
Add classes for chainable LEDs
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tulanthoar | 7:018155c80657 | 1 | /** |
| tulanthoar | 7:018155c80657 | 2 | * @file SeeedChainableLED.h |
| tulanthoar | 7:018155c80657 | 3 | * @brief control the chainable LED module from Seeed Studios |
| tulanthoar | 7:018155c80657 | 4 | * @author Nathan Yonkee |
| tulanthoar | 7:018155c80657 | 5 | * @version 1.0 |
| tulanthoar | 7:018155c80657 | 6 | * @date 2017-04-20 |
| tulanthoar | 7:018155c80657 | 7 | */ |
| tulanthoar | 7:018155c80657 | 8 | #ifndef SEEED_CHAINABLE_LED_H |
| tulanthoar | 7:018155c80657 | 9 | #define SEEED_CHAINABLE_LED_H |
| tulanthoar | 7:018155c80657 | 10 | #include "mbed.h" |
| tulanthoar | 7:018155c80657 | 11 | #include "DataClockPair.h" |
| tulanthoar | 7:018155c80657 | 12 | |
| tulanthoar | 7:018155c80657 | 13 | class SeeedChainableLED { |
| tulanthoar | 7:018155c80657 | 14 | private: |
| tulanthoar | 7:018155c80657 | 15 | DigitalOut datPin_; |
| tulanthoar | 7:018155c80657 | 16 | DigitalOut clkPin_; |
| tulanthoar | 7:018155c80657 | 17 | void start_cmd(); |
| tulanthoar | 7:018155c80657 | 18 | void stop_cmd(); |
| tulanthoar | 7:018155c80657 | 19 | void pin_delay(int delay_us = 6); |
| tulanthoar | 7:018155c80657 | 20 | void send_byte(int byte); |
| tulanthoar | 7:018155c80657 | 21 | void send_color(int r, int g, int b); |
| tulanthoar | 7:018155c80657 | 22 | public: |
| tulanthoar | 7:018155c80657 | 23 | void set_color_rgb(int r, int g, int b, int led = 0); |
| tulanthoar | 7:018155c80657 | 24 | void clear_led(); |
| tulanthoar | 7:018155c80657 | 25 | void turn_on(); |
| tulanthoar | 7:018155c80657 | 26 | SeeedChainableLED (DataClockPair pins); |
| tulanthoar | 7:018155c80657 | 27 | }; |
| tulanthoar | 7:018155c80657 | 28 | |
| tulanthoar | 7:018155c80657 | 29 | SeeedChainableLED::SeeedChainableLED(DataClockPair pins) : datPin_(pins.dataPin), clkPin_(pins.clockPin) { |
| tulanthoar | 7:018155c80657 | 30 | clear_led(); |
| tulanthoar | 7:018155c80657 | 31 | } |
| tulanthoar | 7:018155c80657 | 32 | |
| tulanthoar | 7:018155c80657 | 33 | void SeeedChainableLED::start_cmd() { |
| tulanthoar | 7:018155c80657 | 34 | send_byte(0x00); |
| tulanthoar | 7:018155c80657 | 35 | send_byte(0x00); |
| tulanthoar | 7:018155c80657 | 36 | send_byte(0x00); |
| tulanthoar | 7:018155c80657 | 37 | send_byte(0x00); |
| tulanthoar | 7:018155c80657 | 38 | } |
| tulanthoar | 7:018155c80657 | 39 | |
| tulanthoar | 7:018155c80657 | 40 | void SeeedChainableLED::stop_cmd() { |
| tulanthoar | 7:018155c80657 | 41 | start_cmd(); |
| tulanthoar | 7:018155c80657 | 42 | } |
| tulanthoar | 7:018155c80657 | 43 | |
| tulanthoar | 7:018155c80657 | 44 | void SeeedChainableLED::clear_led() { |
| tulanthoar | 7:018155c80657 | 45 | set_color_rgb(0,0,0); |
| tulanthoar | 7:018155c80657 | 46 | } |
| tulanthoar | 7:018155c80657 | 47 | |
| tulanthoar | 7:018155c80657 | 48 | void SeeedChainableLED::turn_on() { |
| tulanthoar | 7:018155c80657 | 49 | set_color_rgb(255,255,255); |
| tulanthoar | 7:018155c80657 | 50 | } |
| tulanthoar | 7:018155c80657 | 51 | |
| tulanthoar | 7:018155c80657 | 52 | void SeeedChainableLED::pin_delay(int delay_us) { |
| tulanthoar | 7:018155c80657 | 53 | wait_us(delay_us); |
| tulanthoar | 7:018155c80657 | 54 | } |
| tulanthoar | 7:018155c80657 | 55 | |
| tulanthoar | 7:018155c80657 | 56 | void SeeedChainableLED::set_color_rgb(int r, int g, int b, int led) { |
| tulanthoar | 7:018155c80657 | 57 | start_cmd(); |
| tulanthoar | 7:018155c80657 | 58 | send_color(r, g, b); |
| tulanthoar | 7:018155c80657 | 59 | stop_cmd(); |
| tulanthoar | 7:018155c80657 | 60 | } |
| tulanthoar | 7:018155c80657 | 61 | |
| tulanthoar | 7:018155c80657 | 62 | void SeeedChainableLED::send_color(int red, int green, int blue) { |
| tulanthoar | 7:018155c80657 | 63 | // Start by sending a byte with the format "1 1 /B7 /B6 /G7 /G6 /R7 /R6" |
| tulanthoar | 7:018155c80657 | 64 | int prefix = 0b11000000; |
| tulanthoar | 7:018155c80657 | 65 | if ((blue & 0x80) == 0) prefix|= 0b00100000; |
| tulanthoar | 7:018155c80657 | 66 | if ((blue & 0x40) == 0) prefix|= 0b00010000; |
| tulanthoar | 7:018155c80657 | 67 | if ((green & 0x80) == 0) prefix|= 0b00001000; |
| tulanthoar | 7:018155c80657 | 68 | if ((green & 0x40) == 0) prefix|= 0b00000100; |
| tulanthoar | 7:018155c80657 | 69 | if ((red & 0x80) == 0) prefix|= 0b00000010; |
| tulanthoar | 7:018155c80657 | 70 | if ((red & 0x40) == 0) prefix|= 0b00000001; |
| tulanthoar | 7:018155c80657 | 71 | send_byte(prefix); |
| tulanthoar | 7:018155c80657 | 72 | // Now send the 3 colors |
| tulanthoar | 7:018155c80657 | 73 | send_byte(blue); |
| tulanthoar | 7:018155c80657 | 74 | send_byte(green); |
| tulanthoar | 7:018155c80657 | 75 | send_byte(red); |
| tulanthoar | 7:018155c80657 | 76 | } |
| tulanthoar | 7:018155c80657 | 77 | |
| tulanthoar | 7:018155c80657 | 78 | void SeeedChainableLED::send_byte(int byte) { |
| tulanthoar | 7:018155c80657 | 79 | for (int i = 0; i < 8; i++) { |
| tulanthoar | 7:018155c80657 | 80 | pin_delay(); |
| tulanthoar | 7:018155c80657 | 81 | datPin_ = byte & 0x80; |
| tulanthoar | 7:018155c80657 | 82 | byte <<= 1; |
| tulanthoar | 7:018155c80657 | 83 | clkPin_ = 0; |
| tulanthoar | 7:018155c80657 | 84 | pin_delay(); |
| tulanthoar | 7:018155c80657 | 85 | clkPin_ = 1; |
| tulanthoar | 7:018155c80657 | 86 | } |
| tulanthoar | 7:018155c80657 | 87 | } |
| tulanthoar | 7:018155c80657 | 88 | |
| tulanthoar | 7:018155c80657 | 89 | #endif |