-
Notifications
You must be signed in to change notification settings - Fork 4
/
ServoI2C_firmware.h
159 lines (125 loc) · 3.38 KB
/
ServoI2C_firmware.h
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
/*!
@file ServoI2C_firmware.h
@brief Firmware module for the I2Cwrapper firmare.
Provides I2C access to servo motors connected to the target device.
Mimicks the standard Servo library functions.
## Author
Copyright (c) 2022 juh
## License
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, version 2.
@note At the moment, this will only work if controller and target use the same values
for sizeof(int).
@todo Need to typecast the transmission of int values to int16_t.
*/
/*
(1) includes
*/
#if MF_STAGE == MF_STAGE_includes
#if defined(ARDUINO_ARCH_ESP32)
#include <ESP32Servo.h> // ESP32 doesn't come with a native Servo.h
#else
#include <Servo.h>
#endif // defined(ARDUINO_ARCH_ESP32)
#include <ServoI2C.h>
#endif // MF_STAGE_includes
/*
(2) declarations
*/
#if MF_STAGE == MF_STAGE_declarations
const uint8_t maxServos = 4;
Servo servos[maxServos]; // @todo this should be an array of pointers to Servo objects
uint8_t numServos = 0; // number of initialised servos.
bool validServo(int8_t s)
{
return (s >= 0) and (s < numServos);
}
#endif // MF_STAGE_declarations
/*
(3) setup() function
*/
#if MF_STAGE == MF_STAGE_setup
log("ServoI2C module enabled.\n");
#endif // MF_STAGE_setup
/*
(4) main loop() function
*/
#if MF_STAGE == MF_STAGE_loop
#endif // MF_STAGE_loop
/*
(5) processMessage() function
*/
#if MF_STAGE == MF_STAGE_processMessage
/*
Note: servo implementation is dumb. It passes each command over I2C even if only some of the commands
actually do something target related. It would be much smarter to let the controller do commands like
attached() without I2C traffic. The dumb way however is easier and safer.
*/
case servoAttach1Cmd: {
if ((numServos < maxServos) and (i == 2)) { // 1 int
int16_t p; bufferIn->read(p);
servos[numServos].attach(p);
bufferOut->write(numServos++);
log(numServos);
}
}
break;
case servoAttach2Cmd: {
if ((numServos < maxServos) and (i == 6)) { // 3 int
int16_t p; bufferIn->read(p);
int16_t min; bufferIn->read(min);
int16_t max; bufferIn->read(max);
servos[numServos].attach(p, min, max);
bufferOut->write(numServos++);
}
}
break;
case servoDetachCmd: {
if (validServo(unit) and (i == 0)) { // no parameters
servos[unit].detach();
}
}
break;
case servoWriteCmd: {
if (validServo(unit) and (i == 2)) { // 1 int
int16_t value; bufferIn->read(value);
servos[unit].write(value);
}
}
break;
case servoWriteMicrosecondsCmd: {
if (validServo(unit) and (i == 2)) { // 1 int
int16_t value; bufferIn->read(value);
servos[unit].writeMicroseconds(value);
}
}
break;
case servoReadCmd: {
if (validServo(unit) and (i == 0)) { // no parameters
bufferOut->write((int16_t)servos[unit].read());
}
}
break;
case servoReadMicrosecondsCmd: {
if (validServo(unit) and (i == 0)) { // no parameters
bufferOut->write((int16_t)servos[unit].readMicroseconds());
}
}
break;
case servoAttachedCmd: {
if (validServo(unit) and (i == 0)) { // no parameters
bufferOut->write(uint8_t(servos[unit].attached()));
}
}
break;
#endif // MF_STAGE_processMessage
/*
(6) reset event
*/
#if MF_STAGE == MF_STAGE_reset
for (uint8_t i = 0; i < numServos; i++) {
servos[i].detach();
}
numServos = 0;
#endif // MF_STAGE_reset