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

SeeedFourDigitDisp.h

Committer:
tulanthoar
Date:
2017-04-21
Revision:
8:09c844708255
Parent:
7:b16b9733d859
Child:
12:a16d86fac131

File content as of revision 8:09c844708255:

#ifndef SEEED_FOUR_DIGIT_DISP_H
#define SEEED_FOUR_DIGIT_DISP_H
#include "mbed.h"

class SeeedFourDigitDisp {
  private:
    int digitTable_[17];
    static const int onByte_ = 0x88;
    static const int fixedAddrByte_ = 0x44;
    static const int positionBit_ = 0xc0;
    static const int nullDigit_ = 16;
    static const int colonBit_ = 0x80;
    void start_cmd();
    void stop_cmd();
    DigitalOut datPin_;
    DigitalOut clkPin_;
    void pin_delay(int delay_us = 1);
    void send_byte(int byte);
  public:
    int brightness;
    bool colonFlag;
    void set_digit(int pos, int digit);
    void set_integer(int value);
    void clear_display();
    void turn_on();
    SeeedFourDigitDisp (PinName dataOut, PinName clockOut);
};

SeeedFourDigitDisp::SeeedFourDigitDisp(PinName dataOut, PinName clockOut) : datPin_(dataOut), clkPin_(clockOut) {
    brightness = 7;
    colonFlag = false;
    const int digits[] = {0x3f, 0x06, 0x5b, 0x4f,
                          0x66, 0x6d, 0x7d, 0x07,
                          0x7f, 0x6f, 0x77, 0x7c,
                          0x39, 0x5e, 0x79, 0x71,
                          0x00
                         }; //0~9,A,b,C,d,E,F,null
    for (int i = 0; i < 17; i++) {
        digitTable_[i] = digits[i];
    }
    clear_display();
}

void SeeedFourDigitDisp::start_cmd() {
    datPin_ = 0;
}

void SeeedFourDigitDisp::stop_cmd() {
    clkPin_ = 0;
    datPin_ = 0;
    pin_delay();
    clkPin_ = 1;
    datPin_ = 1;
}

void SeeedFourDigitDisp::clear_display() {
    set_digit(0,nullDigit_);
    set_digit(1,nullDigit_);
    set_digit(2,nullDigit_);
    set_digit(3,nullDigit_);
}

void SeeedFourDigitDisp::set_digit(int pos, int digit) {
    int flaggedDigit = digitTable_[digit] | (colonFlag ? colonBit_ : 0);
    start_cmd();          //start signal sent to TM1637 from MCU
    send_byte(fixedAddrByte_);
    stop_cmd();
    start_cmd();
    send_byte(pos|positionBit_);
    send_byte(flaggedDigit);
    stop_cmd();
    start_cmd();
    send_byte(onByte_ + brightness);
    stop_cmd();

}

void SeeedFourDigitDisp::set_integer(int value) {
    clear_display();
    if( value < 0 ) {
        colonFlag = true;
        set_digit(0, 0);
        return;
    }
    if( value > 9999 ) {
        colonFlag = true;
        set_digit(0, 15);
        return;
    }
    for (int i = 3; i >= 0; --i) {
        int digit = value % 10;
        set_digit(i, digit);
        value -= digit;
        if(value < 10) return;
        value /= 10;
    }
}

void SeeedFourDigitDisp::turn_on() {
    start_cmd();
    send_byte(onByte_+brightness);
    stop_cmd();
}

void SeeedFourDigitDisp::pin_delay(int delay_us) {
    wait_us(delay_us);
}

void SeeedFourDigitDisp::send_byte(int byte) {
    byte |= 0x100; // bring data high for ack after 8 bits
    for (uint8_t i = 0; i < 9; i++) {
        pin_delay();
        clkPin_ = 0;
        pin_delay();
        datPin_ = byte & 1;
        pin_delay();
        byte >>= 1;
        clkPin_ = 1;
    }
}

#endif