-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoapps_swflag.cpp
More file actions
366 lines (301 loc) · 14.7 KB
/
aoapps_swflag.cpp
File metadata and controls
366 lines (301 loc) · 14.7 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// aoapps_swflag.cpp - the "swflag" app shows a flag, which can be changed by pressing switches
/*****************************************************************************
* Copyright 2024,2025 by ams OSRAM AG *
* All rights are reserved. *
* *
* IMPORTANT - PLEASE READ CAREFULLY BEFORE COPYING, INSTALLING OR USING *
* THE SOFTWARE. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*****************************************************************************/
#include <Arduino.h> // Serial.printf
#include <aoresult.h> // AORESULT_ASSERT, aoresult_t
#include <aoosp.h> // aoosp_send_clrerror()
#include <aocmd.h> // aocmd_cint_isprefix()
#include <aoui32.h> // aoui32_but_wentdown()
#include <aomw.h> // aomw_topo_build_start()
#include <aoapps_mngr.h> // aoapps_mngr_register
#include <aoapps_swflag.h> // own
/*
SWFLAG - This is one of the stock applications
DESCRIPTION
- Shows one (static) flag at a time, e.g. the Dutch national flag red/white/blue spread over the OSP chain
- Tries to find a SAID with an I2C bridge with a "selector", an I/O-expander with four buttons and four indicator LEDs
- If there is no selector, shows four static flags switching on a time basis
- If there are multiple selectors the first SAIDbasic one is taken, or the first SAIDsense when no SAIDbasic
- When a selector is found the four buttons select which flag to show
- The indicator LEDs indicate which button/flag was selected
BUTTONS
- The X and Y buttons control the dim level (RGB brightness)
NOTES
- When the app quits, the indicator LED switches off
- This app adds a command to configure which four flags will be shown
GOAL
- To show a "sensor" (button) being accessible from the root MCU (the ESP)
*/
// === Animation state machine ===============================================
// We have a list of flag painters (indices)
#define AOAPPS_SWFLAG_ANIM_NUMFLAGS 4 // dictated by number of buttons in IOX
static int aomw_swflag_anim_pix[AOAPPS_SWFLAG_ANIM_NUMFLAGS] = {
AOMW_FLAG_PIX_DUTCH, AOMW_FLAG_PIX_MALI, AOMW_FLAG_PIX_EUROPE, AOMW_FLAG_PIX_ITALY
};
// Time between flags (when not aoapps_swflag_anim_ioxpresent)
#define AOAPPS_SWFLAG_ANIM_MS 2000
static int aoapps_swflag_anim_ioxpresent; // the I/O-expander is present (if not, flags auto change every UMBUT_STEP_MS)
static int aoapps_swflag_anim_flagix; // index of flag being shown
static uint32_t aoapps_swflag_anim_lastms; // last time stamp (in ms) a flag was shown (for auto change)
// Step of the swflag state machine
static aoresult_t aoapps_swflag_anim() {
aoresult_t result;
// New flag to display? Record that in flagix
int flagix= aoapps_swflag_anim_flagix;
if( aoapps_swflag_anim_ioxpresent ) {
// IOX present: switch flags when button is pressed
result= aomw_iox4b4l_but_scan();
if( result!=aoresult_ok ) return result;
if( aomw_iox4b4l_but_wentdown(AOMW_IOX4B4L_BUT0) ) flagix=0;
if( aomw_iox4b4l_but_wentdown(AOMW_IOX4B4L_BUT1) ) flagix=1;
if( aomw_iox4b4l_but_wentdown(AOMW_IOX4B4L_BUT2) ) flagix=2;
if( aomw_iox4b4l_but_wentdown(AOMW_IOX4B4L_BUT3) ) flagix=3;
} else {
// IOX absent: switch flags every AOAPPS_SWFLAG_ANIM_MS
if( millis()-aoapps_swflag_anim_lastms > AOAPPS_SWFLAG_ANIM_MS ) {
aoapps_swflag_anim_lastms= millis();
flagix= (aoapps_swflag_anim_flagix+1) % AOAPPS_SWFLAG_ANIM_NUMFLAGS;
}
}
// Different flag selected? Paint it
if( aoapps_swflag_anim_flagix!=flagix ) {
aoapps_swflag_anim_flagix = flagix;
// Paint the flag
result= aomw_flag_painter(aomw_swflag_anim_pix[aoapps_swflag_anim_flagix])();
if( result!=aoresult_ok ) return result;
// Highlight the associated indicator LED
if( aoapps_swflag_anim_ioxpresent ) {
result= aomw_iox4b4l_led_set( AOMW_IOX4B4L_LED(aoapps_swflag_anim_flagix) );
if( result!=aoresult_ok ) return result;
}
}
return aoresult_ok;
}
// === UI32 Button ===========================================================
#define AOAPPS_SWFLAG_BUTTONS_PERKIBI 256 // if macro has value x, num steps is approx log(1024)/log(1+x/1024)
#define AOAPPS_SWFLAG_BUTTONS_MS 200 // step interval (in ms) for auto dim
// Handling button presses (to dim down/up)
static uint32_t aoapps_swflag_buttons_ms;
static aoresult_t aoapps_swflag_buttons_check() {
if( aoui32_but_wentdown(AOUI32_BUT_X | AOUI32_BUT_Y) ) {
aoapps_swflag_buttons_ms = millis()-AOAPPS_SWFLAG_BUTTONS_MS; // spoof time
}
if( aoui32_but_isdown(AOUI32_BUT_X | AOUI32_BUT_Y) && millis()-aoapps_swflag_buttons_ms> AOAPPS_SWFLAG_BUTTONS_MS) {
aoapps_swflag_buttons_ms = millis();
int dim= aomw_topo_dim_get();
int step= dim*AOAPPS_SWFLAG_BUTTONS_PERKIBI/1024 +1; // +1 ensures step is not 0
if( aoui32_but_isdown(AOUI32_BUT_X) ) dim-=step; else dim+=step;
aomw_topo_dim_set(dim); // function clips (no need to do that here)
// Serial.printf("dim %d\n", aomw_topo_dim_get());
// Repaint the flag
aoresult_t result= aomw_flag_painter(aomw_swflag_anim_pix[aoapps_swflag_anim_flagix])();
if( result!=aoresult_ok ) return result;
}
return aoresult_ok;
}
// === Configuration handler =================================================
// This application actually has a configuration option: which flags to show
// Lookup the named `flag` in the list of flags aomw_flag_name().
// If found, return its index (0..), otherwise return -1.
static int aoapps_swflag_cmd_find( const char * flag ) {
for( int pix=0; pix<aomw_flag_count(); pix++ ) {
if( aocmd_cint_isprefix(aomw_flag_name(pix),flag) )
return pix;
}
return -1;
}
// Show on Serial which are the four flags
static void aoapps_swflag_cmd_show( ) {
for( int flagix=0; flagix<AOAPPS_SWFLAG_ANIM_NUMFLAGS; flagix++ ) {
Serial.printf("SW%d %s\n", flagix, aomw_flag_name(aomw_swflag_anim_pix[flagix]) );
}
}
// The handler for the "apps config swflag" command
static void aoapps_swflag_cmd_main( int argc, char * argv[] ) {
AORESULT_ASSERT( argc>3 );
if( aocmd_cint_isprefix("list",argv[3]) ) {
if( argc!=4 ) { Serial.printf("ERROR: 'swflag' has too many args\n" ); return; }
for( int pix=0; pix<aomw_flag_count(); pix++ ) {
Serial.printf(" %s\n", aomw_flag_name(pix) );
}
return;
} else if( aocmd_cint_isprefix("get",argv[3]) ) {
if( argc!=4 ) { Serial.printf("ERROR: 'swflag' has too many args\n" ); return; }
aoapps_swflag_cmd_show();
return;
} else if( aocmd_cint_isprefix("set",argv[3]) ) {
if( argc!=8 ) { Serial.printf("ERROR: 'swflag' expects <flag1> <flag2> <flag3> <flag4>\n" ); return; }
AORESULT_ASSERT( AOAPPS_SWFLAG_ANIM_NUMFLAGS==4 );
// check if all entered flags exist, if so, copy them over
for( int flagix=0; flagix<4; flagix++ )
if( aoapps_swflag_cmd_find(argv[4+flagix])==-1 ) { Serial.printf("ERROR: 'swflag' expects flag name, not '%s'\n", argv[4+flagix] ); return; }
for( int flagix=0; flagix<4; flagix++ )
aomw_swflag_anim_pix[flagix]= aoapps_swflag_cmd_find(argv[4+flagix]);
if( argv[0][0]!='@' ) aoapps_swflag_cmd_show();
return;
} else {
Serial.printf("ERROR: 'swflag' has unknown argument (%s)\n",argv[3] ); return;
}
}
// The long help text for the "apps config swflag" command.
static const char aoapps_swflag_cmd_help[] =
"SYNTAX: apps config swflag list\n"
"- shows available flags\n"
"SYNTAX: apps config swflag get\n"
"- shows configured flags\n"
"SYNTAX: apps config swflag set <flag1> <flag2> <flag3> <flag4>\n"
"- configures four flags (from list)\n"
;
// Registration does not exist, part of aoapps_swflag_register()
// aoapps_swflag_cmd_register()
// === Top-level state machine ===============================================
// save the topo global dim level
static int aoapps_swflag_dimdft;
// The application manager entry point (start)
static aoresult_t aoapps_swflag_start() {
aoresult_t result;
// Is there a SAIDBbasic (or a SAIDsense, or a SAIDsenseV2) in the OSP chain - with a selector
uint16_t addr;
result= aomw_topo_i2cfind( AOMW_IOX4B4L_DADDR7_SAIDBASIC, &addr );
if( result!=aoresult_ok && result!=aoresult_dev_noi2cdev ) return result;
aoapps_swflag_anim_ioxpresent= result==aoresult_ok;
if( aoapps_swflag_anim_ioxpresent ) {
// Init IOX
Serial.printf("swflag: using I/O-expander %02X (SAIDbasic) on SAID %03X \n",AOMW_IOX4B4L_DADDR7_SAIDBASIC,addr);
result= aomw_iox4b4l_init(addr, AOMW_IOX4B4L_DADDR7_SAIDBASIC, AOMW_IOX4B4L_PINCFG_SAIDBASIC);
if( result!=aoresult_ok ) return result;
} else {
// Is there a SAIDsense instead in the OSP chain?
result= aomw_topo_i2cfind( AOMW_IOX4B4L_DADDR7_SAIDSENSE, &addr );
if( result!=aoresult_ok && result!=aoresult_dev_noi2cdev ) return result;
aoapps_swflag_anim_ioxpresent= result==aoresult_ok;
if( aoapps_swflag_anim_ioxpresent ) {
// Init IOX
Serial.printf("swflag: using I/O-expander %02X (SAIDsense) on SAID %03X \n",AOMW_IOX4B4L_DADDR7_SAIDSENSE,addr);
result= aomw_iox4b4l_init(addr, AOMW_IOX4B4L_DADDR7_SAIDSENSE, AOMW_IOX4B4L_PINCFG_SAIDSENSE);
if( result!=aoresult_ok ) return result;
} else {
// Is there a pre-production SAIDsense V2 instead in the OSP chain?
result= aomw_topo_i2cfind( AOMW_IOX4B4L_DADDR7_SAIDSENSEV2, &addr );
if( result!=aoresult_ok && result!=aoresult_dev_noi2cdev ) return result;
aoapps_swflag_anim_ioxpresent= result==aoresult_ok;
if( aoapps_swflag_anim_ioxpresent ) {
// Init IOX
Serial.printf("swflag: using I/O-expander %02X (SAIDsense V2) on SAID %03X \n",AOMW_IOX4B4L_DADDR7_SAIDSENSEV2,addr);
result= aomw_iox4b4l_init(addr, AOMW_IOX4B4L_DADDR7_SAIDSENSEV2, AOMW_IOX4B4L_PINCFG_SAIDSENSEV2);
if( result!=aoresult_ok ) return result;
} else {
Serial.printf("swflag: no I/O-expander found, cycling flags\n");
}
}
}
// Select first flag
aoapps_swflag_anim_flagix= 0;
// Paint the selected flag
result= aomw_flag_painter(aomw_swflag_anim_pix[aoapps_swflag_anim_flagix])();
if( result!=aoresult_ok ) return result;
// Highlight the associated indicator LED
if( aoapps_swflag_anim_ioxpresent ) {
result= aomw_iox4b4l_led_set( AOMW_IOX4B4L_LED(aoapps_swflag_anim_flagix) );
if( result!=aoresult_ok ) return result;
}
// Record time stamp of painting
aoapps_swflag_anim_lastms= millis();
// Record initial dim level
aoapps_swflag_dimdft= aomw_topo_dim_get();
return aoresult_ok;
}
// The application manager entry point (step)
static aoresult_t aoapps_swflag_step() {
aoresult_t result;
// check ui32 buttons
result= aoapps_swflag_buttons_check();
if( result!=aoresult_ok ) return result;
// actual animation
result= aoapps_swflag_anim();
if( result!=aoresult_ok ) return result;
// return success
return aoresult_ok;
}
// The application manager entry point (stop)
static void aoapps_swflag_stop() {
// Shut down indicator LEDs
aomw_iox4b4l_led_set( AOMW_IOX4B4L_LEDNONE );
// restore original dim level
aomw_topo_dim_set(aoapps_swflag_dimdft);
}
// === Registration ==========================================================
/*!
@brief Registers the swflag app with the app manager.
@note This app shows one of four flags on the OSP chain.
By pressing one of the 4 buttons attached to an I/O-expander
select which flags is shown.
@note The app needs an I/O-expander with 4 buttons and 4 LEDs
attached to a SAID. It tries to find this, if not it just
cycles the flags.
@note A typical board to use is the SAIDbasic demo board.
*/
void aoapps_swflag_register() {
aoapps_mngr_register("swflag", "Switch flag", "dim -", "dim +",
AOAPPS_MNGR_FLAGS_WITHTOPO | AOAPPS_MNGR_FLAGS_WITHREPAIR,
aoapps_swflag_start, aoapps_swflag_step, aoapps_swflag_stop,
aoapps_swflag_cmd_main, aoapps_swflag_cmd_help );
}
// === Extra =================================================================
/*!
@brief Resets the hardware (I/O-expander) controlled by the swflag app.
@return aoresult_ok iff successful
@note The swflag app switches on the indicator LEDs connected to
the I/O-expander. A reboot of the executable restarts the app
manager, which restarts the first app. In case this is not the
swflag app, the indicator LEDs stay on.
This function is supposed to be called in setup() of executables
that contain the swflag app to prevent the indicator LEDs from
staying on after a reboot.
*/
aoresult_t aoapps_swflag_resethw() {
aoresult_t result;
// Init chain and find I2C bridges
result= aomw_topo_build();
if( result!=aoresult_ok ) return result;
// Is there a SAIDbasic selector in the OSP chain?
uint16_t addr;
result= aomw_topo_i2cfind( AOMW_IOX4B4L_DADDR7_SAIDBASIC, &addr );
if( result==aoresult_ok ) {
// Init selector; this switches the indicator LEDs off
result= aomw_iox4b4l_init(addr, AOMW_IOX4B4L_DADDR7_SAIDBASIC, AOMW_IOX4B4L_PINCFG_SAIDBASIC);
if( result!=aoresult_ok ) return result;
}
// Is there a SAIDsense (old V2) selector in the chain?
result= aomw_topo_i2cfind( AOMW_IOX4B4L_DADDR7_SAIDSENSEV2, &addr );
if( result==aoresult_ok ) {
// Init selector; this switches the indicator LEDs off
result= aomw_iox4b4l_init(addr, AOMW_IOX4B4L_DADDR7_SAIDSENSEV2, AOMW_IOX4B4L_PINCFG_SAIDSENSEV2);
if( result!=aoresult_ok ) return result;
}
// Is there a SAIDsense selector in the chain?
result= aomw_topo_i2cfind( AOMW_IOX4B4L_DADDR7_SAIDSENSE, &addr );
if( result==aoresult_ok ) {
// Init selector; this switches the indicator LEDs off
result= aomw_iox4b4l_init(addr, AOMW_IOX4B4L_DADDR7_SAIDSENSE, AOMW_IOX4B4L_PINCFG_SAIDSENSE);
if( result!=aoresult_ok ) return result;
}
return aoresult_ok;
}