Skip to content

Commit

Permalink
Add rounding of numbers in JSON ouput (to match CSV output). (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjko authored Apr 15, 2023
1 parent 575d640 commit 4d54fb4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ target_compile_definitions(fanpico PRIVATE USBD_PRODUCT="FanPico-${FANPICO_BOARD
target_compile_definitions(fanpico PRIVATE PARAM_ASSERTIONS_ENABLE_ALL=1)
target_compile_definitions(fanpico PRIVATE PICO_MALLOC_PANIC=0)
target_compile_definitions(fanpico PRIVATE PICO_DEBUG_MALLOC=0)
target_compile_definitions(fanpico PRIVATE PICO_DEOPTIMIZED_DEBUG=1)
target_compile_definitions(fanpico PRIVATE PICO_DEOPTIMIZED_DEBUG=0)
target_compile_definitions(fanpico PRIVATE PICO_STACK_SIZE=0x1000)
target_compile_definitions(fanpico PRIVATE PICO_USE_STACK_GUARDS=1)
target_compile_definitions(fanpico PRIVATE PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1)
Expand Down
8 changes: 8 additions & 0 deletions src/fanpico.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ void network_status();
void set_pico_system_time(long unsigned int sec);
const char *network_ip();

/* httpd.c */
#if WIFI_SUPPORT
u16_t fanpico_ssi_handler(const char *tag, char *insert, int insertlen,
u16_t current_tag_part, u16_t *next_tag_part);
#endif

/* tls.c */
int read_pem_file(char *buf, uint32_t size, uint32_t timeout);
#ifdef WIFI_SUPPORT
Expand Down Expand Up @@ -309,6 +315,8 @@ const char *pico_serial_str();
const char *mac_address_str(const uint8_t *mac);
int check_for_change(double oldval, double newval, double threshold);
int time_passed(absolute_time_t *t, uint32_t us);
int64_t pow_i64(int64_t x, uint8_t y);
double round_decimal(double val, unsigned int decimal);
char* base64encode(const char *input);
char* base64decode(const char *input);
char *strncopy(char *dst, const char *src, size_t size);
Expand Down
16 changes: 8 additions & 8 deletions src/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ u16_t json_stats(char *insert, int insertlen, u16_t current_tag_part, u16_t *nex

cJSON_AddItemToObject(o, "fan", cJSON_CreateNumber(i+1));
cJSON_AddItemToObject(o, "name", cJSON_CreateString(cfg->fans[i].name));
cJSON_AddItemToObject(o, "rpm", cJSON_CreateNumber(rpm));
cJSON_AddItemToObject(o, "frequency", cJSON_CreateNumber(st->fan_freq[i]));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(st->fan_duty[i]));
cJSON_AddItemToObject(o, "rpm", cJSON_CreateNumber(round_decimal(rpm, 0)));
cJSON_AddItemToObject(o, "frequency", cJSON_CreateNumber(round_decimal(st->fan_freq[i], 2)));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(round_decimal(st->fan_duty[i], 1)));
cJSON_AddItemToArray(array, o);
}
cJSON_AddItemToObject(json, "fans", array);
Expand All @@ -144,9 +144,9 @@ u16_t json_stats(char *insert, int insertlen, u16_t current_tag_part, u16_t *nex

cJSON_AddItemToObject(o, "mbfan", cJSON_CreateNumber(i+1));
cJSON_AddItemToObject(o, "name", cJSON_CreateString(cfg->mbfans[i].name));
cJSON_AddItemToObject(o, "rpm", cJSON_CreateNumber(rpm));
cJSON_AddItemToObject(o, "frequency", cJSON_CreateNumber(st->mbfan_freq[i]));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(st->mbfan_duty[i]));
cJSON_AddItemToObject(o, "rpm", cJSON_CreateNumber(round_decimal(rpm, 0)));
cJSON_AddItemToObject(o, "frequency", cJSON_CreateNumber(round_decimal(st->mbfan_freq[i], 2)));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(round_decimal(st->mbfan_duty[i], 1)));
cJSON_AddItemToArray(array, o);
}
cJSON_AddItemToObject(json, "mbfans", array);
Expand All @@ -161,8 +161,8 @@ u16_t json_stats(char *insert, int insertlen, u16_t current_tag_part, u16_t *nex

cJSON_AddItemToObject(o, "sensor", cJSON_CreateNumber(i+1));
cJSON_AddItemToObject(o, "name", cJSON_CreateString(cfg->sensors[i].name));
cJSON_AddItemToObject(o, "temperature", cJSON_CreateNumber(st->temp[i]));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(pwm));
cJSON_AddItemToObject(o, "temperature", cJSON_CreateNumber(round_decimal(st->temp[i], 1)));
cJSON_AddItemToObject(o, "duty_cycle", cJSON_CreateNumber(round_decimal(pwm, 1)));
cJSON_AddItemToArray(array, o);
}
cJSON_AddItemToObject(json, "mbfans", array);
Expand Down
4 changes: 0 additions & 4 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@

#include "syslog.h"

u16_t fanpico_ssi_handler(const char *tag, char *insert, int insertlen,
u16_t current_tag_part, u16_t *next_tag_part);


static absolute_time_t t_network_initialized;
static bool wifi_initialized = false;
static bool network_initialized = false;
Expand Down
34 changes: 34 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,40 @@ int time_passed(absolute_time_t *t, uint32_t ms)
}


#define SQRT_INT64_MAX 3037000499 // sqrt(INT64_MAX)

int64_t pow_i64(int64_t x, uint8_t y)
{
int64_t res;

/* Handle edge cases. */
if (x == 1 || y == 0)
return 1;
if (x == 0)
return 0;

/* Calculate x^y by squaring method. */
res = (y & 1 ? x : 1);
y >>= 1;
while (y) {
/* Check for overflow. */
if (x > SQRT_INT64_MAX)
return 0;
x *= x;
if (y & 1)
res *= x; /* This may still overflow... */
y >>= 1;
}

return res;
}

double round_decimal(double val, unsigned int decimal)
{
double f = pow_i64(10, decimal);
return round(val * f) / f;
}

char* base64encode(const char *input)
{
base64_encodestate ctx;
Expand Down

0 comments on commit 4d54fb4

Please sign in to comment.