Class to control the 10 segment LED bar from Seeed Studios

Dependents:   Seeed_Grove_4_Digit_Display_Clock

Committer:
tulanthoar
Date:
Sun May 21 19:45:26 2017 +0000
Revision:
0:786a9861af5c
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tulanthoar 0:786a9861af5c 1 #ifndef SEEED_LED_BAR_H
tulanthoar 0:786a9861af5c 2 #define SEEED_LED_BAR_H
tulanthoar 0:786a9861af5c 3 #include "mbed.h"
tulanthoar 0:786a9861af5c 4 #include "DataClockPair.h"
tulanthoar 0:786a9861af5c 5
tulanthoar 0:786a9861af5c 6 class SeeedLedBar {
tulanthoar 0:786a9861af5c 7 private:
tulanthoar 0:786a9861af5c 8 DigitalOut datPin_;
tulanthoar 0:786a9861af5c 9 DigitalOut clkPin_;
tulanthoar 0:786a9861af5c 10 void pin_delay(int delay_us = 2);
tulanthoar 0:786a9861af5c 11 static const int glbCmdMode_ = 0x00;
tulanthoar 0:786a9861af5c 12 void send_sixtn_bits(int sixtnBits);
tulanthoar 0:786a9861af5c 13 void set_leds(int (&ledPower )[10]);
tulanthoar 0:786a9861af5c 14 public:
tulanthoar 0:786a9861af5c 15 SeeedLedBar(DataClockPair pins);
tulanthoar 0:786a9861af5c 16 void ten_on();
tulanthoar 0:786a9861af5c 17 void ten_off();
tulanthoar 0:786a9861af5c 18 void ten_set(int (&ledPower )[10]);
tulanthoar 0:786a9861af5c 19 };
tulanthoar 0:786a9861af5c 20
tulanthoar 0:786a9861af5c 21 SeeedLedBar::SeeedLedBar(DataClockPair pins) : datPin_(pins.dataPin), clkPin_(pins.clockPin) { }
tulanthoar 0:786a9861af5c 22
tulanthoar 0:786a9861af5c 23 void SeeedLedBar::pin_delay(int delay_us) {
tulanthoar 0:786a9861af5c 24 wait_us(delay_us);
tulanthoar 0:786a9861af5c 25 }
tulanthoar 0:786a9861af5c 26
tulanthoar 0:786a9861af5c 27 void SeeedLedBar::send_sixtn_bits(int sixtnBits) {
tulanthoar 0:786a9861af5c 28 for (int i = 0; i < 16; i++) {
tulanthoar 0:786a9861af5c 29 pin_delay();
tulanthoar 0:786a9861af5c 30 datPin_ = sixtnBits & 0x8000;
tulanthoar 0:786a9861af5c 31 sixtnBits <<= 1;
tulanthoar 0:786a9861af5c 32 pin_delay();
tulanthoar 0:786a9861af5c 33 clkPin_ = !clkPin_;
tulanthoar 0:786a9861af5c 34 }
tulanthoar 0:786a9861af5c 35 }
tulanthoar 0:786a9861af5c 36
tulanthoar 0:786a9861af5c 37 void SeeedLedBar::set_leds(int (&ledPower )[10]) {
tulanthoar 0:786a9861af5c 38 send_sixtn_bits(glbCmdMode_);
tulanthoar 0:786a9861af5c 39 for (auto& i : ledPower) send_sixtn_bits(i);
tulanthoar 0:786a9861af5c 40 // Two extra empty bits for padding the command to the correct length
tulanthoar 0:786a9861af5c 41 send_sixtn_bits(0x00);
tulanthoar 0:786a9861af5c 42 send_sixtn_bits(0x00);
tulanthoar 0:786a9861af5c 43 pin_delay();
tulanthoar 0:786a9861af5c 44 datPin_ = 0;
tulanthoar 0:786a9861af5c 45 pin_delay();
tulanthoar 0:786a9861af5c 46 for (int i = 0; i < 4; i++) {
tulanthoar 0:786a9861af5c 47 pin_delay();
tulanthoar 0:786a9861af5c 48 datPin_ = 1;
tulanthoar 0:786a9861af5c 49 pin_delay();
tulanthoar 0:786a9861af5c 50 datPin_ = 0;
tulanthoar 0:786a9861af5c 51 }
tulanthoar 0:786a9861af5c 52 }
tulanthoar 0:786a9861af5c 53
tulanthoar 0:786a9861af5c 54 void SeeedLedBar::ten_on() {
tulanthoar 0:786a9861af5c 55 int all_on[10] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
tulanthoar 0:786a9861af5c 56 set_leds(all_on);
tulanthoar 0:786a9861af5c 57 }
tulanthoar 0:786a9861af5c 58
tulanthoar 0:786a9861af5c 59 void SeeedLedBar::ten_off() {
tulanthoar 0:786a9861af5c 60 int all_off[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
tulanthoar 0:786a9861af5c 61 set_leds(all_off);
tulanthoar 0:786a9861af5c 62 }
tulanthoar 0:786a9861af5c 63
tulanthoar 0:786a9861af5c 64 void SeeedLedBar::ten_set(int (&ledPower )[10]) {
tulanthoar 0:786a9861af5c 65 set_leds(ledPower);
tulanthoar 0:786a9861af5c 66 }
tulanthoar 0:786a9861af5c 67
tulanthoar 0:786a9861af5c 68 #endif