-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
80 lines (73 loc) · 2.1 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @file utils.c
* @author Phil Hilger ([email protected])
* @brief
* @version 0.1
* @date 2023-03-02
*
* CAN-talk. A library for microcontrollers that allows decent comms
* over a CAN bus.
*
* Copyright (C) 2023, PeerGum
*
* 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 <https: //www.gnu.org/licenses/>.
*
*/
#include "utils.h"
// #include "time.h"
#include "esp_sntp.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
/**
* @brief returns milliseconds since boot
*
* @return unsigned long
*/
unsigned long millis() {
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
int64_t time_ms =
(int64_t)tv_now.tv_sec * 1000L + (int64_t)tv_now.tv_usec / 1000L;
return (unsigned long)time_ms;
}
unsigned long micros() {
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
int64_t time_ms =
(int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec / 1000000L;
return (unsigned long)time_ms;
}
void delay(unsigned long ms) { vTaskDelay(pdMS_TO_TICKS(ms)); }
void delayMicroseconds(unsigned long us) { usleep(us); }
char *urlEncode(const char *text) {
static char encoded[256];
int pos = 0;
for (int i = 0; i < strlen(text) && pos < 255; i++) {
switch (text[i]) {
case '&':
strcat(encoded, "&");
pos = strlen(encoded);
break;
default:
encoded[pos++] = text[i];
encoded[pos] = 0;
break;
}
}
return encoded;
}
#ifdef __cplusplus
uint16_t makeWord(uint16_t w) { return w; }
uint16_t makeWord(uint8_t h, uint8_t l) { return (h << 8) | l; }
#endif