#include "sseg.h"

TM1640* sseg = nullptr;

static const char *TAG = "sseg_driver";

void init_sseg() {
    sseg = new TM1640(SSEG_PIN_DATA, SSEG_PIN_CLK, 8);
}

void set_game_sseg_raw(const uint8_t* digits) {
    sseg->setSegments(digits[0], 0);
    sseg->setSegments(digits[1], 1);
    sseg->setSegments(digits[2], 2);
    sseg->setSegments(digits[3], 3);
}

void set_module_sseg_raw(const uint8_t* digits) {
    sseg->setSegments(digits[0], 4);
    sseg->setSegments(digits[1], 5);
    sseg->setSegments(digits[2], 6);
    sseg->setSegments(digits[3], 7);
}

void set_game_sseg_num(unsigned int value, int dot_pos) {
    for (int i = 0; i < 4; i++) {
        auto idx = value % 10;
        sseg->sendChar(3-i, TM16XX_NUMBER_FONT[idx], i == dot_pos);
        value = value / 10;
    }
}

void set_module_sseg_num(unsigned int value, int dot_pos) {
    for (int i = 0; i < 4; i++) {
        auto idx = value % 10;
        sseg->sendChar(7-i, TM16XX_NUMBER_FONT[idx], i == dot_pos);
        value = value / 10;
    }
}
