WIP gauge reporting experiment

This commit is contained in:
Lukas F. Hartmann 2022-08-25 20:41:46 +02:00
parent 87e0f29b0e
commit 87acbf173e
2 changed files with 95 additions and 34 deletions

View File

@ -81,8 +81,9 @@ static uint8_t pressed_scancodes[MAX_SCANCODES] = {0,0,0,0,0,0};
static int pressed_keys = 0;
void hid_task(void);
void led_task(uint32_t rgb);
void led_task(uint32_t rgb, int bar_sz);
void led_core(void);
int process_keyboard(uint8_t* resulting_scancodes);
#define UART_ID uart1
#define BAUD_RATE 115200
@ -90,6 +91,30 @@ void led_core(void);
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
static char uart_rx_line[128];
static int uart_rx_i=0;
void on_uart_rx() {
while (uart_is_readable(UART_ID)) {
char c = uart_getc(UART_ID);
if (uart_rx_i<127) {
uart_rx_line[uart_rx_i++] = c;
uart_rx_line[uart_rx_i] = 0;
}
if (c == '\n') {
// TODO hack
if (uart_rx_i>4) {
// cut off after 4 digits
uart_rx_line[4] = 0;
float percentage = ((float)atoi(uart_rx_line))/10.0;
float bar_sz = (percentage/100.0)*12;
led_task(0x008800, bar_sz);
}
uart_rx_i = 0;
}
}
}
/*------------- MAIN -------------*/
int main(void)
{
@ -103,9 +128,9 @@ int main(void)
gpio_set_function(PIN_UART_TX, GPIO_FUNC_UART);
gpio_set_function(PIN_UART_RX, GPIO_FUNC_UART);
int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
//irq_set_exclusive_handler(UART_IRQ, on_uart_rx);
//irq_set_enabled(UART_IRQ, true);
//uart_set_irq_enables(UART_ID, true, false); // bool rx_has_data, bool tx_needs_data
irq_set_exclusive_handler(UART_IRQ, on_uart_rx);
irq_set_enabled(UART_IRQ, true);
uart_set_irq_enables(UART_ID, true, false); // bool rx_has_data, bool tx_needs_data
gpio_pull_up(PIN_COL1);
gpio_init(PIN_LEDS);
@ -179,8 +204,6 @@ int main(void)
led_task(0x000000);
}*/
led_task(0xffff00);
while (1) {
pressed_keys = process_keyboard(pressed_scancodes);
tud_task(); // tinyusb device task
@ -336,12 +359,11 @@ int process_keyboard(uint8_t* resulting_scancodes) {
if (matrix_state[0]) {
// ESC
//remote_turn_som_power_on();
led_task(0x00ffff);
uart_puts(UART_ID, "1p\r\n");
command_sent = 1;
} else if (matrix_state[11]) {
//remote_turn_som_power_off();
led_task(0x000000);
led_task(0x000000, 1);
uart_puts(UART_ID, "0p\r\n");
command_sent = 1;
}
@ -545,7 +567,7 @@ int led_counter = 0;
int rgb_channel = 0;
int rgb_val = 0;
void led_task(uint32_t rgb) {
void led_task(uint32_t rgb, int bar_sz) {
int offset = 0;
uint32_t color = rgb;
@ -555,7 +577,7 @@ void led_task(uint32_t rgb) {
for (int x=0; x<w; x++) {
uint32_t bits = color;
if (x>0 || y>0) bits = 0;
if (x>bar_sz || y>0) bits = 0;
for (int i=23; i>=0; i--) {
if (bits & (1<<23)) {
@ -612,19 +634,6 @@ void hid_task(void)
}
void led_core() {
for (int i=0; i<10; i++) {
sleep_ms(200);
led_task(0x00ff00); //rgb_val<<rgb_channel);
/*rgb_val+=2;
if (rgb_val > 0xff) {
rgb_val = 0;
rgb_channel += 8;
if (rgb_channel > 16) {
rgb_channel = 0;
}
}*/
}
}
// Invoked when sent REPORT successfully to host

View File

@ -43,6 +43,15 @@
// https://www.monolithicpower.com/en/documentview/productdocument/index/version/2/document_type/Datasheet/lang/en/sku/MP2762AGV/document_id/9073/
#define MPS_ADDR 0x5c
#define I2C_TIMEOUT (1000*500)
#define UART_ID uart1
#define BAUD_RATE 115200
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
void i2c_scan() {
printf("\nI2C Scan\n");
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
@ -139,6 +148,24 @@ float max_word_to_ma(uint16_t w)
return result;
}
float max_word_to_time(uint16_t w)
{
float result = ((float)w)*5.625;
return result;
}
float max_word_to_cap(uint16_t w)
{
float result = ((float)w)*1.0; // depends on Rsense, 1.0 @ 5mohms. otherwise 5.0μVh / Rsense
return result;
}
float max_word_to_percentage(uint16_t w)
{
float result = ((float)w)*0.00390625;
return result;
}
uint8_t mps_read_byte(uint8_t addr)
{
uint8_t buf;
@ -456,6 +483,13 @@ void max_dump() {
float temp3 = ((float)((int16_t)max_read_word_100(0x38)))*(1.0/256.0);
float temp4 = ((float)((int16_t)max_read_word_100(0x37)))*(1.0/256.0);
float rep_capacity = max_word_to_cap(max_read_word(0x05));
float rep_percentage = max_word_to_percentage(max_read_word(0x06));
float rep_age = max_word_to_percentage(max_read_word(0x07));
float rep_full_capacity = max_word_to_cap(max_read_word(0x10));
float rep_time_to_empty = max_word_to_time(max_read_word(0x11));
float rep_time_to_full = max_word_to_time(max_read_word(0x20));
printf("[pack info]\n");
printf(" comm_stat: %04x\n", comm_stat);
@ -533,7 +567,28 @@ void max_dump() {
printf(" cell2: %f\n", cell2);
printf(" cell3: %f\n", cell3);
printf(" cell4: %f\n", cell4);
printf(" vpack: %f\n ============\n", vpack);
printf(" vpack: %f\n", vpack);
printf(" rep_capacity: %f mAh\n", rep_capacity);
printf(" rep_percentage: %f %\n", rep_percentage);
printf(" rep_age: %f %\n", rep_age);
printf(" rep_full_capacity: %f mAh\n", rep_full_capacity);
printf(" rep_time_to_empty: %f s\n", rep_time_to_empty);
printf(" rep_time_to_full: %f s\n", rep_time_to_full);
printf(" ============\n");
char report[128];
sprintf(report, "%04d %05d %05d %06d %06d\r\n",
(int)(rep_percentage*10.0),
(int)(rep_capacity),
(int)(rep_full_capacity),
(int)(rep_time_to_empty),
(int)(rep_time_to_full));
uart_puts(UART_ID, report);
printf(" report: %s", report);
printf(" ============\n");
}
void turn_som_power_on() {
@ -566,15 +621,6 @@ void turn_som_power_off() {
gpio_put(PIN_1V1_ENABLE, 0);
}
#define I2C_TIMEOUT (1000*500)
#define UART_ID uart1
#define BAUD_RATE 115200
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
#define ST_EXPECT_DIGIT_0 0
#define ST_EXPECT_DIGIT_1 1
#define ST_EXPECT_DIGIT_2 2
@ -797,6 +843,7 @@ int main() {
gpio_put(PIN_LED_B, 0);
unsigned int t = 0;
unsigned int t_report = 0;
int state = 0;
int request_sent = 0;
@ -952,7 +999,6 @@ int main() {
//i2c_scan();
int renegotiate = charger_dump();
max_dump();
if (renegotiate) {
state = 0;
@ -962,7 +1008,13 @@ int main() {
}
}
if (t_report>2000) {
max_dump();
t_report = 0;
}
t++;
t_report++;
}
return 0;