Skip to content

Commit 6254a51

Browse files
facchinmaentinger
authored andcommitted
Provide workarounds for inot as classes
1 parent 082d78c commit 6254a51

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed

Diff for: examples/Breaks_1/Thread.inot

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* This fails to compile because it doesn't look like a
2+
* declaration and a definition but rather like an attempt
3+
* to overload myFunc with the same signature (again)
4+
* resulting in a compile error.
5+
*/
6+
7+
/* To fix this in the "class-wrapping" method, don't write
8+
* forward declarations.
9+
*/
10+
11+
//int myFunc(int const a, int const b);
12+
13+
void setup()
14+
{
15+
16+
}
17+
18+
void loop()
19+
{
20+
static int c = 0;
21+
c += myFunc(0,c);
22+
}
23+
24+
int myFunc(int const a, int const b)
25+
{
26+
return (a + b);
27+
}

Diff for: examples/Breaks_2/Thread.inot

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* This fails to compile because myEventHandler has the
2+
* function signature of 'void Thread::myEventHandler(void)'
3+
* and is a member function of 'class Thread' while
4+
* attachInterrupt expects a function with the signature
5+
* 'void myEventHandler(void)'
6+
*/
7+
8+
/* To fix this in the "class-wrapping" method, add `static`
9+
* in front of the callback declaration.
10+
*/
11+
12+
static void myEventHandler()
13+
{
14+
/* Do something. */
15+
}
16+
17+
void setup()
18+
{
19+
attachInterrupt(digitalPinToInterrupt(2), myEventHandler, CHANGE);
20+
}
21+
22+
void loop()
23+
{
24+
25+
}

Diff for: examples/Breaks_3/Thread.inot

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* This fails to compile because in-class-initialisation of
2+
* a static member variable is forbidden.
3+
*/
4+
5+
/* To fix this in the "class-wrapping" method, remove `static`
6+
* or remove the initialization.
7+
*/
8+
9+
/*static*/ int my_global_variable = 0;
10+
11+
#if ANOTHER_WAY
12+
static int my_global_variable;
13+
#endif
14+
15+
void setup()
16+
{
17+
#if ANOTHER_WAY
18+
// my_global_variable = 0;
19+
#endif
20+
}
21+
22+
void loop()
23+
{
24+
25+
}

Diff for: examples/Breaks_4/Thread.inot

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Breaks for all kind of stuff ...
2+
*/
3+
4+
/* To fix this in the "class-wrapping" method, remove `static`
5+
* and change objects declarations
6+
* from `Classname obj(args...)`
7+
* to `Classname obj = Classname{args..}`
8+
*/
9+
10+
/**************************************************************************************
11+
* CONSTANTS
12+
**************************************************************************************/
13+
14+
static byte constexpr LSM6DSOX_ADDRESS = 0x6A;
15+
static byte constexpr LSM6DSOX_WHO_AM_I_REG = 0x0F;
16+
17+
/**************************************************************************************
18+
* GLOBAL VARIABLES
19+
**************************************************************************************/
20+
21+
// BusDevice lsm6dsox(Wire, LSM6DSOX_ADDRESS);
22+
BusDevice lsm6dsox = BusDevice{Wire, LSM6DSOX_ADDRESS};
23+
24+
/**************************************************************************************
25+
* FUNCTIONS
26+
**************************************************************************************/
27+
28+
byte lsm6dsox_read_reg(byte reg_addr)
29+
{
30+
byte read_buf = 0;
31+
lsm6dsox.wire().write_then_read(&reg_addr, 1, &read_buf, 1);
32+
return read_buf;
33+
}
34+
35+
/**************************************************************************************
36+
* SETUP/LOOP
37+
**************************************************************************************/
38+
39+
void setup()
40+
{
41+
Serial.begin(9600);
42+
}
43+
44+
void loop()
45+
{
46+
/* Sleep between 5 and 500 ms */
47+
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
48+
/* Try to read some data from the LSM6DSOX. */
49+
byte const who_am_i = lsm6dsox_read_reg(LSM6DSOX_WHO_AM_I_REG);
50+
/* Print thread id and chip id value to serial. */
51+
char msg[64] = {0};
52+
snprintf(msg, sizeof(msg), "%s: LSM6DSOX[WHO_AM_I] = 0x%X", rtos::ThisThread::get_name(), who_am_i);
53+
Serial.println(msg);
54+
}

Diff for: examples/Breaks_5/Thread.inot

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* This fails to compile because myEventHandler is declared
2+
* after setup/loop and currently there's not automatic prototype
3+
* generation as done for the ino file.
4+
*/
5+
6+
void setup()
7+
{
8+
attachInterrupt(digitalPinToInterrupt(2), myEventHandler, CHANGE);
9+
}
10+
11+
void loop()
12+
{
13+
14+
}
15+
16+
static void myEventHandler()
17+
{
18+
/* Do something. */
19+
}

Diff for: examples/Breaks_6/Thread.inot

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Break here is intentional
2+
*/
3+
4+
void setup()
5+
{
6+
7+
}
8+
9+
void loop()
10+
{
11+
12+
}
13+
14+
int var = 0;

0 commit comments

Comments
 (0)