Simple control for ST M5451 LED driver.

Sample usage

#include "mbed.h"
#include "M5451.h"

M5451 m5451(D8, D7);

void toggleOneByOne(){
    for(char i = 0; i < m5451.outputs(); i++){
        m5451.toggleBit(i);
        m5451.update();
        wait_ms(250);
        m5451.toggleBit(i);
    }
}

int main() {
    while(1) {
        m5451.setAllBits(0);
        toggleOneByOne();
        m5451.setAllBits(1);
        toggleOneByOne();
    }
}
Committer:
jirrick
Date:
Sun Mar 01 10:20:26 2020 +0000
Revision:
0:b21b3517a1d6
Child:
1:db1e69c22bc6
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jirrick 0:b21b3517a1d6 1 #include "M5451.h"
jirrick 0:b21b3517a1d6 2 #include "mbed.h"
jirrick 0:b21b3517a1d6 3
jirrick 0:b21b3517a1d6 4 M5451::M5451(PinName dataPin, PinName clockPin):
jirrick 0:b21b3517a1d6 5 _dataPin(dataPin), _clockPin(clockPin) {
jirrick 0:b21b3517a1d6 6 _state = 0ULL;
jirrick 0:b21b3517a1d6 7 }
jirrick 0:b21b3517a1d6 8
jirrick 0:b21b3517a1d6 9 int M5451::outputs() {
jirrick 0:b21b3517a1d6 10 return JIRRICK_M5451_OUTPUTS;
jirrick 0:b21b3517a1d6 11 }
jirrick 0:b21b3517a1d6 12
jirrick 0:b21b3517a1d6 13 void M5451::setState(uint64_t state){
jirrick 0:b21b3517a1d6 14 _state = state;
jirrick 0:b21b3517a1d6 15 }
jirrick 0:b21b3517a1d6 16
jirrick 0:b21b3517a1d6 17 void M5451::setBit(char position, char value){
jirrick 0:b21b3517a1d6 18 if (position < JIRRICK_M5451_OUTPUTS)
jirrick 0:b21b3517a1d6 19 {
jirrick 0:b21b3517a1d6 20 uint64_t value64 = value;
jirrick 0:b21b3517a1d6 21 _state = (_state & ~(1ULL << position)) | (value64 << position);
jirrick 0:b21b3517a1d6 22 }
jirrick 0:b21b3517a1d6 23 }
jirrick 0:b21b3517a1d6 24
jirrick 0:b21b3517a1d6 25 void M5451::setAllBits(char value){
jirrick 0:b21b3517a1d6 26 for(char i = 0; i < JIRRICK_M5451_OUTPUTS; i++){
jirrick 0:b21b3517a1d6 27 setBit(i, value);
jirrick 0:b21b3517a1d6 28 }
jirrick 0:b21b3517a1d6 29 }
jirrick 0:b21b3517a1d6 30
jirrick 0:b21b3517a1d6 31 void M5451::update() {
jirrick 0:b21b3517a1d6 32 _send(1); //START BIT
jirrick 0:b21b3517a1d6 33 uint64_t input = _state;
jirrick 0:b21b3517a1d6 34 for(char i = 0; i < JIRRICK_M5451_OUTPUTS; i++){
jirrick 0:b21b3517a1d6 35 char value = (input & 1ULL);
jirrick 0:b21b3517a1d6 36 _send(value);
jirrick 0:b21b3517a1d6 37 input = input >> 1;
jirrick 0:b21b3517a1d6 38 }
jirrick 0:b21b3517a1d6 39 }
jirrick 0:b21b3517a1d6 40
jirrick 0:b21b3517a1d6 41 uint64_t M5451::getState() {
jirrick 0:b21b3517a1d6 42 return _state;
jirrick 0:b21b3517a1d6 43 }
jirrick 0:b21b3517a1d6 44
jirrick 0:b21b3517a1d6 45 void M5451::_send(char bit) {
jirrick 0:b21b3517a1d6 46 _dataPin = (bit % 2);
jirrick 0:b21b3517a1d6 47 wait_us(JIRRICK_M5451_WAIT);
jirrick 0:b21b3517a1d6 48 _clockPin = 1;
jirrick 0:b21b3517a1d6 49 wait_us(JIRRICK_M5451_WAIT);
jirrick 0:b21b3517a1d6 50 _clockPin = 0;
jirrick 0:b21b3517a1d6 51 wait_us(JIRRICK_M5451_WAIT);
jirrick 0:b21b3517a1d6 52 }
jirrick 0:b21b3517a1d6 53