Nathan Yonkee / Seeed_Four_Digit_Disp

Dependents:   Seeed_Grove_4_Digit_Display_Clock

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SeeedFourDigitDisp.h Source File

SeeedFourDigitDisp.h

00001 #ifndef SEEED_FOUR_DIGIT_DISP_H
00002 #define SEEED_FOUR_DIGIT_DISP_H
00003 #include "mbed.h"
00004 #include "DataClockPair.h"
00005 
00006 class SeeedFourDigitDisp {
00007   private:
00008     int digitTable_[17] = {0x3f, 0x06, 0x5b, 0x4f,
00009                           0x66, 0x6d, 0x7d, 0x07,
00010                           0x7f, 0x6f, 0x77, 0x7c,
00011                           0x39, 0x5e, 0x79, 0x71,
00012                           0x00
00013                          };
00014     static const int onByte_ = 0x88;
00015     static const int fixedAddrByte_ = 0x44;
00016     static const int positionBit_ = 0xc0;
00017     static const int nullDigit_ = 16;
00018     static const int colonBit_ = 0x80;
00019     void start_cmd();
00020     void stop_cmd();
00021     DigitalOut datPin_;
00022     DigitalOut clkPin_;
00023     void pin_delay(int delay_us = 1);
00024     void send_byte(int byte);
00025   public:
00026     int brightness = 7;
00027     bool colonFlag = false;
00028     void set_digit(int pos, int digit);
00029     void set_integer(int value);
00030     void clear_display();
00031     void turn_on();
00032     SeeedFourDigitDisp (DataClockPair pins);
00033 };
00034 
00035 SeeedFourDigitDisp::SeeedFourDigitDisp(DataClockPair pins) : datPin_(pins.dataPin, 1), clkPin_(pins.clockPin, 1) {
00036     clear_display();
00037 }
00038 
00039 void SeeedFourDigitDisp::pin_delay(int delay_us) {
00040     wait_us(delay_us);
00041 }
00042 
00043 void SeeedFourDigitDisp::start_cmd() {
00044     datPin_ = !datPin_;
00045 }
00046 
00047 void SeeedFourDigitDisp::stop_cmd() {
00048     datPin_ = 0;
00049     clkPin_ = !clkPin_;
00050     pin_delay();
00051     clkPin_ = !clkPin_;
00052     datPin_ = !datPin_;
00053 }
00054 
00055 void SeeedFourDigitDisp::send_byte(int byte) {
00056     byte |= 0x100; // bring data high for ack after 8 bits
00057     for (int i = 0; i < 9; ++i) {
00058         pin_delay();
00059         clkPin_ = !clkPin_;
00060         pin_delay();
00061         datPin_ = byte & 1;
00062         byte >>= 1;
00063         pin_delay();
00064         clkPin_ = !clkPin_;
00065     }
00066 }
00067 
00068 void SeeedFourDigitDisp::set_digit(int pos, int digit) {
00069     int flaggedDigit = digitTable_[digit] | (colonFlag ? colonBit_ : 0);
00070     start_cmd();          //start signal sent to TM1637 from MCU
00071     send_byte(fixedAddrByte_);
00072     stop_cmd();
00073     start_cmd();
00074     send_byte(pos|positionBit_);
00075     send_byte(flaggedDigit);
00076     stop_cmd();
00077     start_cmd();
00078     send_byte(onByte_ + brightness);
00079     stop_cmd();
00080 }
00081 
00082 void SeeedFourDigitDisp::turn_on() {
00083     start_cmd();
00084     send_byte(onByte_+brightness);
00085     stop_cmd();
00086 }
00087 
00088 void SeeedFourDigitDisp::clear_display() {
00089     set_digit(0,nullDigit_);
00090     set_digit(1,nullDigit_);
00091     set_digit(2,nullDigit_);
00092     set_digit(3,nullDigit_);
00093 }
00094 
00095 
00096 void SeeedFourDigitDisp::set_integer(int value) {
00097     clear_display();
00098     if( value < 0 ) {
00099         colonFlag = true;
00100         set_digit(0, 0);
00101         return;
00102     }
00103     if( value > 9999 ) {
00104         colonFlag = true;
00105         set_digit(0, 15);
00106         return;
00107     }
00108     for (int i = 3; i >= 0; --i) {
00109         int digit = value % 10;
00110         set_digit(i, digit);
00111         value -= digit;
00112         if(value < 10) return;
00113         value /= 10;
00114     }
00115 }
00116 
00117 
00118 #endif