#include <ELECHOUSE_CC1101_SRC_DRV.h>
#include <TM1638plus.h>

#define STB_PIN 2
#define CLK_PIN 3
#define DIO_PIN 4

TM1638plus module(STB_PIN, CLK_PIN, DIO_PIN, false);

int S_FIL = 0;
float t = 0.0;
float U = 0.0;
int tr_A = 0;
float Cm = 0.0;

void setup() {
    Serial.begin(1200);

    if (ELECHOUSE_cc1101.getCC1101()) {
        Serial.println("Connection OK");
    } else {
        Serial.println("Connection Error");
    }

    ELECHOUSE_cc1101.Init();
    ELECHOUSE_cc1101.setCCMode(1);
    ELECHOUSE_cc1101.setModulation(0);
    ELECHOUSE_cc1101.setMHZ(433.92);
    ELECHOUSE_cc1101.setDeviation(5.157);
    ELECHOUSE_cc1101.setChannel(0);
    ELECHOUSE_cc1101.setChsp(199.95);
    ELECHOUSE_cc1101.setRxBW(58.03);
    ELECHOUSE_cc1101.setDRate(10.0);
    ELECHOUSE_cc1101.setPA(12);
    ELECHOUSE_cc1101.setSyncMode(2);
    ELECHOUSE_cc1101.setSyncWord(211, 145);
    ELECHOUSE_cc1101.setAdrChk(0);
    ELECHOUSE_cc1101.setAddr(0);
    ELECHOUSE_cc1101.setWhiteData(0);
    ELECHOUSE_cc1101.setPktFormat(0);
    ELECHOUSE_cc1101.setLengthConfig(1);
    ELECHOUSE_cc1101.setPacketLength(0);
    ELECHOUSE_cc1101.setCrc(1);
    ELECHOUSE_cc1101.setCRC_AF(0);
    ELECHOUSE_cc1101.setDcFilterOff(0);
    ELECHOUSE_cc1101.setManchester(0);
    ELECHOUSE_cc1101.setFEC(0);
    ELECHOUSE_cc1101.setPRE(1);
    ELECHOUSE_cc1101.setPQT(0);
    ELECHOUSE_cc1101.setAppendStatus(0);

    Serial.println("Rx Mode");

    module.displayBegin();
    for (int i = 0; i < 8; i++) {
        module.setLED(i, 0);
    }
}

byte buffer[61] = {0};

int mapRssiToLeds(int rssi) {
    if (rssi >= -30) return 8;
    if (rssi <= -90) return 1;
    return map(rssi, -90, -30, 1, 8);
}

void parseData(char* buffer) {
    char* token = strtok(buffer, ",");
    while (token != NULL) {
        if (strstr(token, "S-FIL:") != NULL) {
            sscanf(token, "S-FIL: %d", &S_FIL);
        } else if (strstr(token, "t:") != NULL) {
            sscanf(token, " t: %f", &t);
        } else if (strstr(token, "U:") != NULL) {
            sscanf(token, " U: %f", &U);
        } else if (strstr(token, "tr-A:") != NULL) {
            sscanf(token, " tr-A: %d", &tr_A);
        } else if (strstr(token, "Cm:") != NULL) {
            sscanf(token, " Cm: %f", &Cm);
        }
        token = strtok(NULL, ",");
    }
}

void loop() {
    if (ELECHOUSE_cc1101.CheckRxFifo(100)) {
        if (ELECHOUSE_cc1101.CheckCRC()) {
            int rssi = ELECHOUSE_cc1101.getRssi();
            Serial.print("Rssi: ");
            Serial.println(rssi);

            int numLeds = mapRssiToLeds(rssi);
            for (int i = 0; i < 8; i++) {
                module.setLED(i, i < numLeds ? 1 : 0);
            }

            Serial.print("LQI: ");
            Serial.println(ELECHOUSE_cc1101.getLqi());

            int len = ELECHOUSE_cc1101.ReceiveData(buffer);
            buffer[len] = '\0';

            Serial.print("Raw buffer: ");
            Serial.println((char*)buffer);

            parseData((char*)buffer);

            Serial.print("S-FIL: ");
            Serial.println(S_FIL);
            Serial.print("t: ");
            Serial.println(t);
            Serial.print("U: ");
            Serial.println(U);
            Serial.print("tr-A: ");
            Serial.println(tr_A);
            Serial.print("Cm: ");
            Serial.println(Cm);

            for (int i = 0; i < len; i++) {
                Serial.print(buffer[i]);
                Serial.print(",");
            }
            Serial.println();
        }
    }

    uint8_t buttons = module.readButtons();

    if (buttons & 0x01) {
        char displayBuffer[9];
        snprintf(displayBuffer, sizeof(displayBuffer), "S-FIL %d", S_FIL);
        module.displayText(displayBuffer);
    }

    if (buttons & 0x02) {
        char displayBuffer[9];
        snprintf(displayBuffer, sizeof(displayBuffer), "T:%.1fC", t);
        module.displayText(displayBuffer);
    }

    if (buttons & 0x04) {
        char displayBuffer[9];
        snprintf(displayBuffer, sizeof(displayBuffer), "U:%.1fP", U);
        module.displayText(displayBuffer);
    }

    if (buttons & 0x08) {
        char displayBuffer[9];
        snprintf(displayBuffer, sizeof(displayBuffer), "tr-A:%d", tr_A);
        module.displayText(displayBuffer);
    }

    if (buttons & 0x10) {
        char displayBuffer[9];
        snprintf(displayBuffer, sizeof(displayBuffer), "Cm:%.1f", Cm);
        module.displayText(displayBuffer);
    }
} stampa valori errati, correggi sta cosa: Rssi: -65
LQI: 135
Dati ricevuti: S-FIL: 0, t: 19.8C, U: 70.0P, tr-A: 0, Cm:   0.0
S-FIL: 0
t: 0.00
U: 0.00
tr-A: 0
Cm: 0.00