Skip to content

Commit

Permalink
Merge pull request #33 from eamars/configurable-dp
Browse files Browse the repository at this point in the history
Add configurable DP
  • Loading branch information
eamars authored Dec 17, 2023
2 parents 4113997 + 42110ae commit 931cd25
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 35 deletions.
50 changes: 37 additions & 13 deletions src/charge_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

uint8_t charge_weight_digits[] = {0, 0, 0, 0, 0};

// PID related
charge_mode_config_t charge_mode_config;

// Scale related
Expand All @@ -39,6 +38,8 @@ const eeprom_charge_mode_data_t default_charge_mode_data = {
.set_point_sd_margin = 0.02,
.set_point_mean_margin = 0.02,

.decimal_places = DP_2,

// LED related
.neopixel_normal_charge_colour = urgb_u32(0, 0xFF, 0), // green
.neopixel_under_charge_colour = urgb_u32(0xFF, 0xFF, 0), // yellow
Expand All @@ -63,7 +64,7 @@ typedef enum {


void scale_measurement_render_task(void *p) {
char current_weight_string[5];
char current_weight_string[WEIGHT_STRING_LEN];

u8g2_t * display_handler = get_display_handler();

Expand All @@ -83,8 +84,8 @@ void scale_measurement_render_task(void *p) {
// current weight (only show values > -10)
memset(current_weight_string, 0x0, sizeof(current_weight_string));
float scale_measurement = scale_get_current_measurement();
if (scale_measurement > -10) {
sprintf(current_weight_string, "%0.02f", scale_measurement);
if (scale_measurement > -1.0) {
float_to_string(current_weight_string, scale_measurement, charge_mode_config.eeprom_charge_mode_data.decimal_places);
}
else {
strcpy(current_weight_string, "---");
Expand Down Expand Up @@ -165,9 +166,12 @@ ChargeModeState_t charge_mode_wait_for_complete(ChargeModeState_t prev_state) {
);

// Update current status
char target_weight_string[WEIGHT_STRING_LEN];
float_to_string(target_weight_string, charge_mode_config.target_charge_weight, charge_mode_config.eeprom_charge_mode_data.decimal_places);

snprintf(title_string, sizeof(title_string),
"Target: %.02f",
charge_mode_config.target_charge_weight);
"Target: %s",
target_weight_string);

// Read trickling parameter from the current profile
profile_t * current_profile = profile_get_selected();
Expand Down Expand Up @@ -385,11 +389,26 @@ uint8_t charge_mode_menu() {
neopixel_led_set_colour(NEOPIXEL_LED_DEFAULT_COLOUR, NEOPIXEL_LED_DEFAULT_COLOUR, NEOPIXEL_LED_DEFAULT_COLOUR, true);

// Create target weight
charge_mode_config.target_charge_weight = charge_weight_digits[4] * 100 + \
charge_weight_digits[3] * 10 + \
charge_weight_digits[2] + \
charge_weight_digits[1] * 0.1 + \
charge_weight_digits[0] * 0.01;
switch (charge_mode_config.eeprom_charge_mode_data.decimal_places) {
case DP_2:
charge_mode_config.target_charge_weight = charge_weight_digits[4] * 100 + \
charge_weight_digits[3] * 10 + \
charge_weight_digits[2] * 1 + \
charge_weight_digits[1] * 0.1 + \
charge_weight_digits[0] * 0.01;
break;
case DP_3:
charge_mode_config.target_charge_weight = charge_weight_digits[4] * 10 + \
charge_weight_digits[3] * 1 + \
charge_weight_digits[2] * 0.1 + \
charge_weight_digits[1] * 0.01 + \
charge_weight_digits[0] * 0.001;
break;
default:
charge_mode_config.target_charge_weight = 0;
break;
}

printf("Target Charge Weight: %f\n", charge_mode_config.target_charge_weight);

// If the display task is never created then we shall create one, otherwise we shall resume the task
Expand Down Expand Up @@ -502,6 +521,7 @@ bool http_rest_charge_mode_config(struct fs_file *file, int num_params, char *pa
// c6 (float): fine_stop_threshold
// c7 (float): set_point_sd_margin
// c8 (float): set_point_mean_margin
// c9 (int): decimal point enum

// ee (bool): save to eeprom

Expand All @@ -522,6 +542,9 @@ bool http_rest_charge_mode_config(struct fs_file *file, int num_params, char *pa
else if (strcmp(params[idx], "c8") == 0) {
charge_mode_config.eeprom_charge_mode_data.set_point_mean_margin = strtof(values[idx], NULL);
}
else if (strcmp(params[idx], "c9") == 0) {
charge_mode_config.eeprom_charge_mode_data.decimal_places = (decimal_places_t) atoi(values[idx]);
}

// LED related settings
else if (strcmp(params[idx], "c1") == 0) {
Expand Down Expand Up @@ -550,7 +573,7 @@ bool http_rest_charge_mode_config(struct fs_file *file, int num_params, char *pa
snprintf(charge_mode_json_buffer,
sizeof(charge_mode_json_buffer),
"{\"c1\":\"#%06x\",\"c2\":\"#%06x\",\"c3\":\"#%06x\",\"c4\":\"#%06x\","
"\"c5\":%.3f,\"c6\":%.3f,\"c7\":%.3f,\"c8\":%.3f}",
"\"c5\":%.3f,\"c6\":%.3f,\"c7\":%.3f,\"c8\":%.3f,\"c9\":%d}",
charge_mode_config.eeprom_charge_mode_data.neopixel_normal_charge_colour,
charge_mode_config.eeprom_charge_mode_data.neopixel_under_charge_colour,
charge_mode_config.eeprom_charge_mode_data.neopixel_over_charge_colour,
Expand All @@ -559,7 +582,8 @@ bool http_rest_charge_mode_config(struct fs_file *file, int num_params, char *pa
charge_mode_config.eeprom_charge_mode_data.coarse_stop_threshold,
charge_mode_config.eeprom_charge_mode_data.fine_stop_threshold,
charge_mode_config.eeprom_charge_mode_data.set_point_sd_margin,
charge_mode_config.eeprom_charge_mode_data.set_point_mean_margin);
charge_mode_config.eeprom_charge_mode_data.set_point_mean_margin,
charge_mode_config.eeprom_charge_mode_data.decimal_places);

size_t data_length = strlen(charge_mode_json_buffer);
file->data = charge_mode_json_buffer;
Expand Down
7 changes: 6 additions & 1 deletion src/charge_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

#include <stdint.h>
#include "http_rest.h"
#include "common.h"

#define EEPROM_CHARGE_MODE_DATA_REV 6 // 16 byte

#define EEPROM_CHARGE_MODE_DATA_REV 7 // 16 byte

#define WEIGHT_STRING_LEN 8

typedef struct {
uint16_t charge_mode_data_rev;
Expand All @@ -16,6 +19,8 @@ typedef struct {
float set_point_sd_margin;
float set_point_mean_margin;

decimal_places_t decimal_places;

// LED related settings
uint32_t neopixel_normal_charge_colour;
uint32_t neopixel_under_charge_colour;
Expand Down
15 changes: 12 additions & 3 deletions src/cleanup_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
#include "motors.h"
#include "scale.h"
#include "display.h"
#
#include "common.h"
#include "charge_mode.h"


// Memory from other modules
extern QueueHandle_t encoder_event_queue;
extern charge_mode_config_t charge_mode_config;


static char title_string[30];
TaskHandle_t cleanup_render_task_handler = NULL;
Expand Down Expand Up @@ -50,7 +54,12 @@ void cleanup_render_task(void *p) {
// Draw charge weight
float current_weight = scale_get_current_measurement();
memset(buf, 0x0, sizeof(buf));
sprintf(buf, "Weight: %0.02f", current_weight);

// Convert to weight string with given decimal places
char weight_string[WEIGHT_STRING_LEN];
float_to_string(weight_string, current_weight, charge_mode_config.eeprom_charge_mode_data.decimal_places);

sprintf(buf, "Weight: %s", weight_string);
u8g2_SetFont(display_handler, u8g2_font_profont11_tf);
u8g2_DrawStr(display_handler, 5, 25, buf);

Expand All @@ -60,7 +69,7 @@ void cleanup_render_task(void *p) {
float flow_rate = weight_diff / 0.02; // 20 ms per sampling period, see below

memset(buf, 0x0, sizeof(buf));
sprintf(buf, "Flow: %0.2f/s", flow_rate);
sprintf(buf, "Flow: %0.3f/s", flow_rate);
u8g2_SetFont(display_handler, u8g2_font_profont11_tf);
u8g2_DrawStr(display_handler, 5, 35, buf);

Expand Down
20 changes: 20 additions & 0 deletions src/common.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <FreeRTOS.h>
#include <task.h>
#include <string.h>
#include <stdio.h>

#include "common.h"
#include "pico/time.h"

Expand Down Expand Up @@ -29,4 +31,22 @@ bool string_to_boolean(char * s) {
}

return var;
}


int float_to_string(char * output_decimal_str, float var, decimal_places_t decimal_places) {
int return_value = 0;

switch (decimal_places) {
case DP_2:
return_value = sprintf(output_decimal_str, "%0.02f", var);
break;
case DP_3:
return_value = sprintf(output_decimal_str, "%0.03f", var);
break;
default:
break;
}

return return_value;
}
6 changes: 6 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <stdint.h>
#include <FreeRTOS.h>

typedef enum {
DP_2 = 0,
DP_3 = 1,
} decimal_places_t;


#ifdef __cplusplus
extern "C" {
Expand All @@ -18,6 +23,7 @@ void delay_ms(uint32_t ms, BaseType_t scheduler_state);
const char * boolean_to_string(bool var);
bool string_to_boolean(char * s);

int float_to_string(char * output_decimal_str, float var, decimal_places_t decimal_places);

#ifdef __cplusplus
}
Expand Down
9 changes: 9 additions & 0 deletions src/html/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ <h1 class="modal-title fs-5" id="modalLabel">Profile Database</h1>
<input type="number" class="form-control" id="c8" name="c8" step="0.001">
</div>

<div class="form-group mb-3">
<label for="c9">Decimal Places:</label>
<select class="form-select" id="c9" name="c9"
data-bs-toggle="tooltip" data-bs-title="The number of decimal places. Use 2 for unit in grain and 3 for gram">
<option value="0">2</option>
<option value="1">3</option>
</select>
</div>

<div class="form-group mb-3">
<label for="c1">Neopixel LED Normal Charge Colour</label>
<input type="color" class="form-control form-control-color" id="c1" name="c1">
Expand Down
Loading

0 comments on commit 931cd25

Please sign in to comment.