MeterLogger
Functions
utils.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ICACHE_FLASH_ATTR uint16_t ccit_crc16 (uint16_t crc16, uint8_t *data_p, unsigned int length)
 
ICACHE_FLASH_ATTR void w_to_kw_str (char *w, char *kw)
 
ICACHE_FLASH_ATTR void kw_to_w_str (char *kw, char *w)
 
ICACHE_FLASH_ATTR void mw_to_w_str (char *mw, char *w)
 
ICACHE_FLASH_ATTR void divide_str_by_10 (char *str, char *decimal_str)
 
ICACHE_FLASH_ATTR void divide_str_by_100 (char *str, char *decimal_str)
 
ICACHE_FLASH_ATTR void divide_str_by_1000 (char *str, char *decimal_str)
 
ICACHE_FLASH_ATTR void cleanup_decimal_str (char *decimal_str, char *cleaned_up_str, unsigned int length)
 
ICACHE_FLASH_ATTR unsigned int decimal_number_length (int n)
 
ICACHE_FLASH_ATTR int int_pow (int x, int y)
 

Function Documentation

◆ ccit_crc16()

ICACHE_FLASH_ATTR uint16_t ccit_crc16 ( uint16_t  crc16,
uint8_t *  data_p,
unsigned int  length 
)

Definition at line 47 of file utils.c.

Referenced by cfg_load(), cfg_save(), and kmp_crc16().

47  {
48  while (length--) {
49  crc16 = (crc16 << 8) ^ ccit_crc16_table[((crc16 >> 8) ^ *data_p++) & 0x00FF];
50  }
51  return crc16;
52 }
Here is the caller graph for this function:

◆ cleanup_decimal_str()

ICACHE_FLASH_ATTR void cleanup_decimal_str ( char *  decimal_str,
char *  cleaned_up_str,
unsigned int  length 
)

Definition at line 151 of file utils.c.

References decimal_number_length(), ICACHE_FLASH_ATTR, memcpy, NULL, strcat, strncpy, strstr, and tfp_snprintf().

Referenced by parse_en61107_frame(), and parse_mc66cde_standard_data_1_frame().

151  {
152  uint32_t value_int, value_frac;
153  char *pos;
154  uint8_t decimals = 0;
155  uint8_t prepend_zeroes;
156  char zeroes[8];
157 
158  memcpy(cleaned_up_str, decimal_str, length);
159  cleaned_up_str[length] = 0; // null terminate
160 
161  pos = strstr(cleaned_up_str, ".");
162  if (pos == NULL) {
163  // non fractional number
164  value_int = atoi(cleaned_up_str);
165  tfp_snprintf(cleaned_up_str, length, "%u", value_int);
166  }
167  else {
168  // parse frac
169  while ((*(pos + 1 + decimals) >= '0') && (*(pos + 1 + decimals) <= '9')) {
170  // count the decimals
171  decimals++;
172  }
173  value_frac = atoi(pos + 1);
174  prepend_zeroes = decimals - decimal_number_length(atoi(pos + 1));
175 
176  zeroes[0] = 0; // null terminate
177  while (prepend_zeroes--) {
178  strcat(zeroes, "0");
179  }
180 
181  // parse int
182  strncpy(cleaned_up_str, decimal_str, (pos - cleaned_up_str));
183  cleaned_up_str[cleaned_up_str - pos] = 0; // null terminate
184  value_int = atoi(cleaned_up_str);
185 
186  tfp_snprintf(cleaned_up_str, length, "%u.%s%u", value_int, zeroes, value_frac);
187  }
188 }
#define NULL
Definition: def.h:47
#define strcat(a, b)
Definition: platform.h:23
ICACHE_FLASH_ATTR unsigned int decimal_number_length(int n)
Definition: utils.c:191
#define strstr(a, b)
Definition: platform.h:24
#define strncpy(a, b, c)
Definition: platform.h:16
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *format,...)
Definition: tinyprintf.c:480
#define memcpy(x, a, b)
Definition: platform.h:22
Here is the call graph for this function:
Here is the caller graph for this function:

◆ decimal_number_length()

ICACHE_FLASH_ATTR unsigned int decimal_number_length ( int  n)

Definition at line 191 of file utils.c.

References ICACHE_FLASH_ATTR.

Referenced by cleanup_decimal_str(), and kmp_value_to_string().

191  {
192  int digits;
193 
194  digits = n < 0; //count "minus"
195  do {
196  digits++;
197  } while (n /= 10);
198 
199  return digits;
200 }
Here is the caller graph for this function:

◆ divide_str_by_10()

ICACHE_FLASH_ATTR void divide_str_by_10 ( char *  str,
char *  decimal_str 
)

Definition at line 110 of file utils.c.

References tfp_snprintf().

Referenced by parse_mc66cde_standard_data_1_frame().

110  {
111  int32_t result_int, result_frac;
112  int32_t value_int;
113 
114  value_int = atoi(str);
115 
116  // ...divide by 10 and prepare decimal string
117  result_int = (int32_t)(value_int / 10);
118  if (result_int < 0) {
119  result_frac = -1 * (value_int % 10);
120  }
121  else {
122  result_frac = value_int % 100;
123  }
124 
125  tfp_snprintf(decimal_str, 8, "%d.%01u", result_int, result_frac);
126 }
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *format,...)
Definition: tinyprintf.c:480
Here is the call graph for this function:
Here is the caller graph for this function:

◆ divide_str_by_100()

ICACHE_FLASH_ATTR void divide_str_by_100 ( char *  str,
char *  decimal_str 
)

Definition at line 128 of file utils.c.

References tfp_snprintf().

Referenced by parse_mc66cde_inst_values_frame(), and parse_mc66cde_standard_data_1_frame().

128  {
129  int32_t result_int, result_frac;
130  int32_t value_int;
131 
132  value_int = atoi(str);
133 
134  // ...divide by 100 and prepare decimal string
135  result_int = (int32_t)(value_int / 100);
136  if (result_int < 0) {
137  result_frac = -1 * (value_int % 100);
138  }
139  else {
140  result_frac = value_int % 100;
141  }
142 
143  tfp_snprintf(decimal_str, 8, "%d.%02u", result_int, result_frac);
144 }
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *format,...)
Definition: tinyprintf.c:480
Here is the call graph for this function:
Here is the caller graph for this function:

◆ divide_str_by_1000()

ICACHE_FLASH_ATTR void divide_str_by_1000 ( char *  str,
char *  decimal_str 
)

Definition at line 146 of file utils.c.

References w_to_kw_str().

146  {
147  // just a wrapper
148  w_to_kw_str(str, decimal_str);
149 }
ICACHE_FLASH_ATTR void w_to_kw_str(char *w, char *kw)
Definition: utils.c:54
Here is the call graph for this function:

◆ int_pow()

ICACHE_FLASH_ATTR int int_pow ( int  x,
int  y 
)

Definition at line 203 of file utils.c.

Referenced by kmp_value_to_double(), kmp_value_to_string(), and kw_to_w_str().

203  {
204  int i;
205  int result;
206 
207  if (y == 0) {
208  return 1;
209  }
210 
211  result = x;
212  for (i = 1; i < y; i++) {
213  result *= x;
214  }
215  return result;
216 }
Here is the caller graph for this function:

◆ kw_to_w_str()

ICACHE_FLASH_ATTR void kw_to_w_str ( char *  kw,
char *  w 
)

Definition at line 67 of file utils.c.

References int_pow(), strlen, and tfp_snprintf().

Referenced by cgiSetup(), and mw_to_w_str().

67  {
68  uint32_t result_int, result_frac;
69  uint32_t i;
70  uint32_t len;
71 
72  bool dec_separator;
73 
74  char result_int_str[32 + 1];
75 
76  uint32_t pos_int;
77  uint32_t pos_frac;
78 
79  len = strlen(kw);
80 
81  result_frac = 0;
82  pos_int = 0;
83  pos_frac = 0;
84  dec_separator = false;
85  for (i = 0; i < len && pos_frac < 3; i++) {
86  if (kw[i] == '.') {
87  dec_separator = 1;
88  }
89  else if (!dec_separator) {
90  result_int_str[pos_int++] = kw[i];
91  }
92  else {
93  //result_frac_str[pos_frac++] = kw[i];
94  result_frac += (kw[i] - '0') * int_pow(10, 2 - pos_frac);
95  pos_frac++;
96  }
97  }
98  result_int_str[pos_int] = 0; // null terminate
99  result_int = 1000 * atoi(result_int_str); // multiply by 1000
100 
101  result_int += result_frac;
102  tfp_snprintf(w, 11, "%u", result_int);
103 }
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *format,...)
Definition: tinyprintf.c:480
ICACHE_FLASH_ATTR int int_pow(int x, int y)
Definition: utils.c:203
#define strlen(a)
Definition: platform.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mw_to_w_str()

ICACHE_FLASH_ATTR void mw_to_w_str ( char *  mw,
char *  w 
)

Definition at line 105 of file utils.c.

References kw_to_w_str().

Referenced by en61107_get_received_energy_kwh(), and kmp_get_received_energy_kwh().

105  {
106  // just a wrapper
107  kw_to_w_str(mw, w);
108 }
ICACHE_FLASH_ATTR void kw_to_w_str(char *kw, char *w)
Definition: utils.c:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ w_to_kw_str()

ICACHE_FLASH_ATTR void w_to_kw_str ( char *  w,
char *  kw 
)

Definition at line 54 of file utils.c.

References tfp_snprintf().

Referenced by divide_str_by_1000(), and tplSetup().

54  {
55  uint32_t result_int, result_frac;
56  uint32_t w_int;
57 
58  w_int = atoi(w);
59 
60  // ...divide by 1000 and prepare decimal string in kWh
61  result_int = (int32_t)(w_int / 1000);
62  result_frac = w_int % 1000;
63 
64  tfp_snprintf(kw, 15, "%u.%03u", result_int, result_frac);
65 }
ICACHE_FLASH_ATTR int tfp_snprintf(char *str, size_t size, const char *format,...)
Definition: tinyprintf.c:480
Here is the call graph for this function:
Here is the caller graph for this function: