fork of seeed studio 4-digit display for st nucleo board

Dependencies:   Data_Clock_Pair Seeed_Chainable_LED Seeed_Four_Digit_Disp Seeed_IR_Temp_Sensor Seeed_Led_Bar

Fork of Seeed_Grove_4_Digit_Display_Clock by Seeed

Committer:
tulanthoar
Date:
Fri Apr 21 13:47:19 2017 +0000
Revision:
8:09c844708255
Parent:
7:b16b9733d859
Child:
12:a16d86fac131
add fastio library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tulanthoar 7:b16b9733d859 1 /**
tulanthoar 7:b16b9733d859 2 * @file SeeedChainableLED.h
tulanthoar 7:b16b9733d859 3 * @brief control the chainable LED module from Seeed Studios
tulanthoar 7:b16b9733d859 4 * @author Nathan Yonkee
tulanthoar 7:b16b9733d859 5 * @version 1.0
tulanthoar 7:b16b9733d859 6 * @date 2017-04-20
tulanthoar 7:b16b9733d859 7 */
tulanthoar 7:b16b9733d859 8 #ifndef SEEED_CHAINABLE_LED_H
tulanthoar 7:b16b9733d859 9 #define SEEED_CHAINABLE_LED_H
tulanthoar 7:b16b9733d859 10 #include "mbed.h"
tulanthoar 7:b16b9733d859 11
tulanthoar 7:b16b9733d859 12 class SeeedChainableLED {
tulanthoar 7:b16b9733d859 13 private:
tulanthoar 7:b16b9733d859 14 DigitalOut datPin_;
tulanthoar 7:b16b9733d859 15 DigitalOut clkPin_;
tulanthoar 7:b16b9733d859 16 void start_cmd();
tulanthoar 7:b16b9733d859 17 void stop_cmd();
tulanthoar 7:b16b9733d859 18 void pin_delay(int delay_us = 6);
tulanthoar 7:b16b9733d859 19 void send_byte(int byte);
tulanthoar 7:b16b9733d859 20 void send_color(int r, int g, int b);
tulanthoar 7:b16b9733d859 21 public:
tulanthoar 7:b16b9733d859 22 void set_color_rgb(int r, int g, int b, int led = 0);
tulanthoar 7:b16b9733d859 23 void clear_led();
tulanthoar 7:b16b9733d859 24 void turn_on();
tulanthoar 7:b16b9733d859 25 SeeedChainableLED (PinName dataOut, PinName clockOut);
tulanthoar 7:b16b9733d859 26 };
tulanthoar 7:b16b9733d859 27
tulanthoar 7:b16b9733d859 28 SeeedChainableLED::SeeedChainableLED(PinName dataOut, PinName clockOut) : datPin_(dataOut), clkPin_(clockOut) {
tulanthoar 7:b16b9733d859 29 clear_led();
tulanthoar 7:b16b9733d859 30 }
tulanthoar 7:b16b9733d859 31
tulanthoar 7:b16b9733d859 32 void SeeedChainableLED::start_cmd() {
tulanthoar 7:b16b9733d859 33 send_byte(0x00);
tulanthoar 7:b16b9733d859 34 send_byte(0x00);
tulanthoar 7:b16b9733d859 35 send_byte(0x00);
tulanthoar 7:b16b9733d859 36 send_byte(0x00);
tulanthoar 7:b16b9733d859 37 }
tulanthoar 7:b16b9733d859 38
tulanthoar 7:b16b9733d859 39 void SeeedChainableLED::stop_cmd() {
tulanthoar 7:b16b9733d859 40 start_cmd();
tulanthoar 7:b16b9733d859 41 }
tulanthoar 7:b16b9733d859 42
tulanthoar 7:b16b9733d859 43 void SeeedChainableLED::clear_led() {
tulanthoar 7:b16b9733d859 44 set_color_rgb(0,0,0);
tulanthoar 7:b16b9733d859 45 }
tulanthoar 7:b16b9733d859 46
tulanthoar 7:b16b9733d859 47 void SeeedChainableLED::turn_on() {
tulanthoar 7:b16b9733d859 48 set_color_rgb(255,255,255);
tulanthoar 7:b16b9733d859 49 }
tulanthoar 7:b16b9733d859 50
tulanthoar 7:b16b9733d859 51 void SeeedChainableLED::pin_delay(int delay_us) {
tulanthoar 7:b16b9733d859 52 wait_us(delay_us);
tulanthoar 7:b16b9733d859 53 }
tulanthoar 7:b16b9733d859 54
tulanthoar 7:b16b9733d859 55 void SeeedChainableLED::set_color_rgb(int r, int g, int b, int led) {
tulanthoar 7:b16b9733d859 56 start_cmd();
tulanthoar 7:b16b9733d859 57 send_color(r, g, b);
tulanthoar 7:b16b9733d859 58 stop_cmd();
tulanthoar 7:b16b9733d859 59 }
tulanthoar 7:b16b9733d859 60
tulanthoar 7:b16b9733d859 61 void SeeedChainableLED::send_color(int red, int green, int blue) {
tulanthoar 7:b16b9733d859 62 // Start by sending a byte with the format "1 1 /B7 /B6 /G7 /G6 /R7 /R6"
tulanthoar 7:b16b9733d859 63 int prefix = 0b11000000;
tulanthoar 7:b16b9733d859 64 if ((blue & 0x80) == 0) prefix|= 0b00100000;
tulanthoar 7:b16b9733d859 65 if ((blue & 0x40) == 0) prefix|= 0b00010000;
tulanthoar 7:b16b9733d859 66 if ((green & 0x80) == 0) prefix|= 0b00001000;
tulanthoar 7:b16b9733d859 67 if ((green & 0x40) == 0) prefix|= 0b00000100;
tulanthoar 7:b16b9733d859 68 if ((red & 0x80) == 0) prefix|= 0b00000010;
tulanthoar 7:b16b9733d859 69 if ((red & 0x40) == 0) prefix|= 0b00000001;
tulanthoar 7:b16b9733d859 70 send_byte(prefix);
tulanthoar 7:b16b9733d859 71 // Now send the 3 colors
tulanthoar 7:b16b9733d859 72 send_byte(blue);
tulanthoar 7:b16b9733d859 73 send_byte(green);
tulanthoar 7:b16b9733d859 74 send_byte(red);
tulanthoar 7:b16b9733d859 75 }
tulanthoar 7:b16b9733d859 76
tulanthoar 7:b16b9733d859 77 void SeeedChainableLED::send_byte(int byte) {
tulanthoar 7:b16b9733d859 78 for (int i = 0; i < 8; i++) {
tulanthoar 7:b16b9733d859 79 pin_delay();
tulanthoar 7:b16b9733d859 80 datPin_ = byte & 0x80;
tulanthoar 7:b16b9733d859 81 clkPin_ = 0;
tulanthoar 7:b16b9733d859 82 pin_delay();
tulanthoar 7:b16b9733d859 83 clkPin_ = 1;
tulanthoar 7:b16b9733d859 84 byte <<= 1;
tulanthoar 7:b16b9733d859 85 }
tulanthoar 7:b16b9733d859 86 }
tulanthoar 7:b16b9733d859 87
tulanthoar 7:b16b9733d859 88 #endif