/*
 * This file is part of the bme280 project.
 * Copyright 2025 Edward V. Emelianov <edward.emelianoff@gmail.com>.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "hardware.h"
#include "i2c.h"
#include "proto.h"
#include "spi.h"
#include "strfunc.h"
#include "usb_dev.h"
#include "BMP280.h"

#define MAXSTRLEN    RBINSZ

volatile uint32_t Tms = 0;

void sys_tick_handler(void){
    ++Tms;
}

int main(void){
    char inbuff[MAXSTRLEN+1];
    if(StartHSE()){
        SysTick_Config((uint32_t)72000); // 1ms
    }else{
        StartHSI();
        SysTick_Config((uint32_t)48000); // 1ms
    }
    hw_setup();
    USBPU_OFF();
    USB_setup();
    //BMP280_setup(0, 1);
    uint32_t ctr = Tms, Tmeas = 0;
    USBPU_ON();
    while(1){
        if(Tms - ctr > 499){
            ctr = Tms;
            pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0
        }
        int l = USB_receivestr(inbuff, MAXSTRLEN);
        if(l < 0) USB_sendstr("ERROR: USB buffer overflow or string was too long\n");
        else if(l){
            const char *ans = parse_cmd(inbuff);
            if(ans) USB_sendstr(ans);
        }
        BMP280_status s = BMP280_get_status();
        if(s != BMP280_NOTINIT){
            if(s == BMP280_ERR) BMP280_init();
            else{
                BMP280_process();
                s = BMP280_get_status(); // refresh status after processing
                float Temperature, Pressure, Humidity, Dewpoint;
                if(s == BMP280_RDY && BMP280_getdata(&Temperature, &Pressure, &Humidity)){
                    Dewpoint = Tdew(Temperature, Humidity);
                    USB_sendstr("Tdeg="); USB_sendstr(float2str(Temperature, 2)); USB_sendstr("\nPpa=");
                    USB_sendstr(float2str(Pressure, 3));
                    USB_sendstr("\nPmm="); USB_sendstr(float2str(Pressure * 0.00750062f, 2));
                    USB_sendstr("\nH="); USB_sendstr(float2str(Humidity, 2));
                    USB_sendstr("\nTdew="); USB_sendstr(float2str(Dewpoint, 1));
                    newline();
                }
                if(contmeas && (Tms - Tmeas > 999)){
                    if(BMP280_start()) Tmeas = Tms;
                }
            }
        }
        if(i2c_scanmode){
            uint8_t addr;
            int ok = i2c_scan_next_addr(&addr);
            if(addr == I2C_ADDREND) USND("End scan");
            else if(ok){
                USB_sendstr(uhex2str(addr));
                USB_sendstr(" ("); USB_sendstr(u2str(addr));
                USB_sendstr(") - found device\n");
            }
        }
    }
}
