-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcord_hw.c
190 lines (150 loc) · 4.83 KB
/
cord_hw.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
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
/*
* This file is part of the LibreRVAC project
*
* Copyright © 2015-2016
* Aleks-Daniel Jakimenko-Aleksejev <alex.jakimenko@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "cord_hw.h"
// TODO This is getting annoying. Write a macro?
// TODO We should probably get rid of these if statements. That is, instead
// of checking if the value is pointing to a function we should default to
// some dummy function that does nothing. This way we will get rid of
// redundant checks in hot paths without sacrificing anything because
// most of these callbacks are going to be set (and if they're not set, then
// why call them in hot paths?)
// hw_setup
void (*callback_setup)(void) = 0;
void set_callback_setup(void (*callback)(void) ) {
callback_setup = callback;
}
void hw_setup() {
if (callback_setup == 0)
return;
callback_setup();
}
// hw_step
void (*callback_step)(void) = 0;
void set_callback_step(void (*callback)(void) ) {
callback_step = callback;
}
void hw_step() {
if (callback_step == 0)
return;
callback_step();
}
#ifndef __SDCC
#define __reentrant
#endif
// hw_toggle_interrupts
void (*callback_toggle_interrupts)(bool) __reentrant = 0;
void set_callback_toggle_interrupts(void (*callback)(bool) ) {
callback_toggle_interrupts = callback;
}
void hw_toggle_interrupts(bool enable) {
if (callback_toggle_interrupts == 0)
return;
callback_toggle_interrupts(enable);
}
// hw_beep
void (*callback_process_beeps)() = 0;
void set_callback_process_beeps(void (*callback)()) {
callback_process_beeps = callback;
}
void hw_process_beeps() {
if (callback_process_beeps == 0)
return;
callback_process_beeps();
}
// hw_set_motor_throttle
void (*callback_set_motor_throttle)(cord_motor_type, float) __reentrant = 0;
void set_callback_set_motor_throttle(void (*callback)(cord_motor_type, float) ) {
callback_set_motor_throttle = callback;
}
void hw_set_motor_throttle(cord_motor_type motor, float throttle) {
if (callback_set_motor_throttle == 0)
return;
callback_set_motor_throttle(motor, throttle);
}
// hw_get_time
float (*callback_get_time)(void) = 0;
void set_callback_get_time(float (*callback)(void) ) {
callback_get_time = callback;
}
float hw_get_time() {
if (callback_get_time == 0)
return 0.0;
return callback_get_time();
}
// hw_get_time_ms
uint_fast32_t (*callback_get_time_ms)(void) = 0;
void set_callback_get_time_ms(uint_fast32_t (*callback)(void) ) {
callback_get_time_ms = callback;
}
uint_fast32_t hw_get_time_ms() {
if (callback_get_time_ms == 0)
return 0;
return callback_get_time_ms();
}
// hw_get_battery_voltage
float (*callback_get_battery_voltage)(void) = 0;
void set_callback_get_battery_voltage(float (*callback)(void) ) {
callback_get_battery_voltage = callback;
}
float hw_get_battery_voltage() {
if (callback_get_battery_voltage == 0)
return 0.0;
return callback_get_battery_voltage();
}
// hw_get_battery_status
float (*callback_get_battery_status)(void) = 0;
void set_callback_get_battery_status(float (*callback)(void) ) {
callback_get_battery_status = callback;
}
float hw_get_battery_status() {
if (callback_get_battery_status == 0)
return 0.0;
return callback_get_battery_status();
}
// hw_get_battery_current
float (*callback_get_battery_current)(void) = 0;
void set_callback_get_battery_current(float (*callback)(void) ) {
callback_get_battery_current = callback;
}
float hw_get_battery_current() {
if (callback_get_battery_current == 0)
return 0.0;
return callback_get_battery_current();
}
// hw_get_battery_temperature
float (*callback_get_battery_temperature)(void) = 0;
void set_callback_get_battery_temperature(float (*callback)(void) ) {
callback_get_battery_temperature = callback;
}
float hw_get_battery_temperature() {
if (callback_get_battery_temperature == 0)
return 0.0;
return callback_get_battery_temperature();
}
// hw_get_version
const char* (*callback_get_version)(void) = 0;
void set_callback_get_version(const char* (*callback)(void) ) {
callback_get_version = callback;
}
const char* hw_get_version() {
if (callback_get_version == 0)
return "N/A";
return callback_get_version();
}