-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
217 lines (180 loc) · 8.24 KB
/
util.h
File metadata and controls
217 lines (180 loc) · 8.24 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef __UTIL_H__
#define __UTIL_H__
#include <stdint.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#ifndef _AVR_IOXXX_H_
#include <avr/iousbxx6_7.h>
#endif
#include "usb_serial.h"
#include "print.h"
#define UTL_MAX_STR_RECV_LEN 32
// Included in the end to give the application the possibility to override some settings.
#include "modules.h"
#define bool char
#define true 1
#define false 0
#define UTL_PRINT_ERROR_FILE_LINENO(module_no, ec) do { \
int _lineno_ec = (ec); \
print_P(PSTR("\r\nerror @ ")); \
utl_p_var_int_next(PSTR("module_no"), module_no); \
utl_p_var_int_next(PSTR("ec"), _lineno_ec); \
utl_p_var_int_next(PSTR("lineno"), __LINE__); \
utl_p_var_str_nl(PSTR("file"), PSTR(__FILE__)); \
} while (0)
#define UTL_LED_FAIL(module_no, ec) do { \
int _fail_ec = (ec); \
UTL_PRINT_ERROR_FILE_LINENO(module_no, _fail_ec); \
msc_inf_loop_led_num2(module_no, _fail_ec); \
} while(0)
#define UTL_LED_ASSERT(module_no, expected, ec) do { \
int _assert_ec = (ec); \
if (_assert_ec != expected) { \
UTL_LED_FAIL(module_no, _assert_ec); \
} \
} while(0)
#define UTL_LED_UNIMPLEMENTED(module_no, ec) do { \
msc_inf_loop_led_num2(module_no, ec); \
return ec; \
} while(0)
#define _UTL_OK_OR_RETURN_EC(module_no, ec) do { \
int _ok_or_ret_ec = (ec); \
if (_ok_or_ret_ec) { \
UTL_PRINT_ERROR_FILE_LINENO(module_no, _ok_or_ret_ec); \
return _ok_or_ret_ec; \
} \
} while (0)
#define _UTL_OK_OR_RETURN_NEW_EC(module_no, ec, new_module_no, new_ec) do { \
int _ok_or_ret_new_ec1 = (ec); \
int _ok_or_ret_new_ec2 = (new_ec); \
if (_ok_or_ret_new_ec1) { \
UTL_PRINT_ERROR_FILE_LINENO(module_no, _ok_or_ret_new_ec1); \
UTL_PRINT_ERROR_FILE_LINENO(new_module_no, _ok_or_ret_new_ec2); \
return _ok_or_ret_new_ec2; \
} \
} while (0)
#define UTL_ASSERT_OK(ec) UTL_LED_ASSERT(UTL_MODULE_NO, UTL_EC_OK, ec)
#define CLI_ASSERT_OK(ec) UTL_LED_ASSERT(CLI_MODULE_NO, CLI_EC_OK, ec)
#define ADC_ASSERT_OK(ec) UTL_LED_ASSERT(ADC_MODULE_NO, ADC_EC_OK, ec)
#define T08_ASSERT_OK(ec) UTL_LED_ASSERT(T08_MODULE_NO, T08_EC_OK, ec)
#define KPD_ASSERT_OK(ec) UTL_LED_ASSERT(KPD_MODULE_NO, KPD_EC_OK, ec)
#define UTL_OK_OR_RETURN_EC(ec) _UTL_OK_OR_RETURN_EC(UTL_MODULE_NO, ec)
#define CLI_OK_OR_RETURN_EC(ec) _UTL_OK_OR_RETURN_EC(CLI_MODULE_NO, ec)
#define ADC_OK_OR_RETURN_EC(ec) _UTL_OK_OR_RETURN_EC(ADC_MODULE_NO, ec)
#define T08_OK_OR_RETURN_EC(ec) _UTL_OK_OR_RETURN_EC(T08_MODULE_NO, ec)
#define KPD_OK_OR_RETURN_EC(ec) _UTL_OK_OR_RETURN_EC(KPD_MODULE_NO, ec)
#define UTL_OK_OR_RETURN_NEW_EC(module_no, ec, new_ec) _UTL_OK_OR_RETURN_NEW_EC(module_no, ec, UTL_MODULE_NO, new_ec)
#define CLI_OK_OR_RETURN_NEW_EC(module_no, ec, new_ec) _UTL_OK_OR_RETURN_NEW_EC(module_no, ec, CLI_MODULE_NO, new_ec)
#define ADC_OK_OR_RETURN_NEW_EC(module_no, ec, new_ec) _UTL_OK_OR_RETURN_NEW_EC(module_no, ec, ADC_MODULE_NO, new_ec)
#define T08_OK_OR_RETURN_NEW_EC(module_no, ec, new_ec) _UTL_OK_OR_RETURN_NEW_EC(module_no, ec, T08_MODULE_NO, new_ec)
#define KPD_OK_OR_RETURN_NEW_EC(module_no, ec, new_ec) _UTL_OK_OR_RETURN_NEW_EC(module_no, ec, KPD_MODULE_NO, new_ec)
#define UTL_FAIL(ec) UTL_LED_FAIL(UTL_MODULE_NO, ec)
#define CLI_FAIL(ec) UTL_LED_FAIL(CLI_MODULE_NO, ec)
#define ADC_FAIL(ec) UTL_LED_FAIL(ADC_MODULE_NO, ec)
#define T08_FAIL(ec) UTL_LED_FAIL(T08_MODULE_NO, ec)
#define KPD_FAIL(ec) UTL_LED_FAIL(KPD_MODULE_NO, ec)
// #define CLI_ASSERT_OK(ec) msc_inf_loop_led_num2(2, 2)
#define DEBUG_FAIL() do { \
print_P(PSTR("\r\n --- DEBUG_FAIL ---\r\n")); \
UTL_PRINT_ERROR_FILE_LINENO(4, 4); \
msc_inf_loop_led_num2(4, 4); \
} while(0)
#define UTL_DEBUG_PIN UTL_IO_PIN_PB0
typedef enum {
UTL_EC_OK = 0,
UTL_EC_UNIMPLEMENTED = 1,
// utl_split
UTL_EC_SPLIT_OVER_TOKEN_LENGTH = 2,
UTL_EC_SPLIT_OVER_TOKEN_LIST_LENGTH = 3,
// async_recv_str
UTL_EC_ASYNC_RECV_STR_DONE = 4,
UTL_EC_ASYNC_RECV_STR_ONGOING = 5,
UTL_EC_ASYNC_RECV_STR_USER_NOT_CONNECTED = 6,
UTL_EC_ASYNC_RECV_STR_RESOURCE_LOCKED = 7,
// utl_queue_...
UTL_EC_FIFO_PARAM_OUT_OF_RANGE = 8,
UTL_EC_FIFO_POP_ON_EMPTY = 9,
UTL_EC_FIFO_PUSH_ON_FULL = 10,
// utl_pin_...
UTL_EC_PIN_INVALID_STR = 11,
UTL_EC_PIN_INVALID_PIN = 12,
}UTL_ErrorCode;
void utl_p_var_int(const char *var_name, int var_val);
void utl_p_var_int_next(const char *var_name, int var_val);
void utl_p_var_int_nl(const char *var_name, int var_val);
void utl_p_var_u8(const char *var_name, int var_val);
void utl_p_var_u8_next(const char *var_name, int var_val);
void utl_p_var_u8_nl(const char *var_name, int var_val);
void utl_p_var_char(const char *var_name, char var_val);
void utl_p_var_char_next(const char *var_name, char var_val);
void utl_p_var_char_nl(const char *var_name, char var_val);
void utl_p_var_str(const char *var_name, const char *var_val);
void utl_p_var_str_next(const char *var_name, const char *var_val);
void utl_p_var_str_nl(const char *var_name, const char *var_val);
void utl_p_var_str_nonprog(const char *var_name, const char *var_val);
void utl_p_var_str_next_nonprog(const char *var_name, const char *var_val);
void utl_p_var_str_nl_nonprog(const char *var_name, const char *var_val);
void utl_p_memory_map_u8(const char *buf, int size);
int utl_strlen(const char *buf);
int utl_pgm_strlen(const char *buf);
bool utl_pgm_streq(const char *buf, const char *pgm_buf);
bool utl_both_pgm_streq(const char *pgm_buf_1, const char *pgm_buf_2);
bool utl_char_to_digit(int *out, char c);
bool utl_str_to_int(int *out, const char *buf);
bool utl_char_to_hex(int *out, char c);
bool utl_str_to_hex(int *out, const char *buf);
int utl_power(int base, int exp);
bool utl_lock_read_resource(int module_no);
bool utl_unlock_read_resource(int module_no);
UTL_ErrorCode utl_fifo_push(char *queue, char *elem, int elem_size, int num_elements, int *push_idx, int *pop_idx);
UTL_ErrorCode utl_fifo_peek(char *queue, char *out_elem, int elem_size, int num_elements, int *push_idx, int *pop_idx);
UTL_ErrorCode utl_fifo_pop(char *queue, char *out_elem, int elem_size, int num_elements, int *push_idx, int *pop_idx);
UTL_ErrorCode utl_fifo_len(int num_elements, int *push_idx, int *pop_idx);
// Received a string from the USB serial port using a resource lock to ensure only one module
// is receiving data at a time in a non-blocking environment.
UTL_ErrorCode utl_async_recv_str(uint8_t *out_num_recv, char *buf, uint8_t size, int module_no);
/// This splits a given input string into tokens in `outArgv`. The size of `outArgv` is determined
/// by `token_size` and `token_list_size`, which will flag an error if they are exceeded.
/// The split_token is the character to split by.
UTL_ErrorCode utl_split(int *out_argc, char **out_argv, const char *buf, char split_token, int token_size, int token_list_size);
typedef enum {
UTL_IO_PIN_PB0,
UTL_IO_PIN_PB1,
UTL_IO_PIN_PB2,
UTL_IO_PIN_PB3,
UTL_IO_PIN_PB4,
UTL_IO_PIN_PB5,
UTL_IO_PIN_PB6,
UTL_IO_PIN_PB7,
UTL_IO_PIN_PC6,
UTL_IO_PIN_PC7,
UTL_IO_PIN_PD0,
UTL_IO_PIN_PD1,
UTL_IO_PIN_PD2,
UTL_IO_PIN_PD3,
UTL_IO_PIN_PD4,
UTL_IO_PIN_PD5,
UTL_IO_PIN_PD6,
UTL_IO_PIN_PD7,
UTL_IO_PIN_PF0,
UTL_IO_PIN_PF1,
UTL_IO_PIN_PF4,
UTL_IO_PIN_PF5,
UTL_IO_PIN_PF6,
UTL_IO_PIN_PF7,
}UTL_IO_PIN;
UTL_ErrorCode utl_pin_get_port_char(char *out_port, UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_get_port_bit(int *out_bit, UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_from_str(UTL_IO_PIN *out_pin, const char *buf);
UTL_ErrorCode utl_pin_from_pgm_str(UTL_IO_PIN *out_pin, const char *buf);
UTL_ErrorCode utl_pin_set_port(UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_clear_port(UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_toggle_port(UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_read_port(char *out_val, UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_set_ddr(UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_clear_ddr(UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_read_ddr(char *out_val, UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_set_pin(UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_clear_pin(UTL_IO_PIN pin);
UTL_ErrorCode utl_pin_read_pin(char *out_val, UTL_IO_PIN pin);
#endif // __UTIL_H__