#include <avr/io.h>
#include <stdbool.h>

/**
 * Display pins should be mapped like this:
 * a  b  c  d  e  f  g  DP
 * 0  1  2  3  4  5  6  7
 * 
 * d1 d2 d3 d4
 * 0  1  2  3
*/

void set_display_raw(volatile uint8_t * header, uint8_t data) {
    *header = data;
}

uint8_t digit_to_binary(bool period, uint8_t digit) {
    uint8_t val = period << 7;

    switch(digit) {
        case 0: return val | 0x3f;
        case 1: return val | 0x06;
        case 2: return val | 0x5b;
        case 3: return val | 0x4f;
        case 4: return val | 0x66;
        case 5: return val | 0x6d;
        case 6: return val | 0x7d;
        case 7: return val | 0x07;
        case 8: return val | 0x7f;
        case 9: return val | 0x6f;
        // by default draw a dash
        default: return 0x01000000;
    }
}

void set_display(volatile uint8_t * header, uint8_t value, bool period) {
    uint8_t byte = digit_to_binary(period, value);

    set_display_raw(header, byte);
}
