Skip to content

Commit 7325d04

Browse files
committed
Merge pull request #35 from Overdrivr/ergonomy
Ergonomy
2 parents f226bac + 2f04aa8 commit 7325d04

File tree

15 files changed

+125
-52
lines changed

15 files changed

+125
-52
lines changed

BUILD.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ cd ../../../../
5454

5555
If all tests passed, mbed library can be released with good confidence.
5656

57+
```shell
58+
gradle packMbed
59+
```
60+
61+
This last command will generate an archive of the distribution inside
62+
`build/distributions/`
63+
5764
## arduino distribution
5865

5966
Generates the Arduino distribution by taking files inside `src/`,

drivers/cpp/arduino/Telemetry.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ void Telemetry::pub_f32(const char * topic, float msg)
8686
void Telemetry::sub(void (*callback)(TM_state * s, TM_msg * m),
8787
TM_state* userData)
8888
{
89-
set_state(userData);
90-
subscribe(callback);
89+
subscribe(callback,userData);
9190
}
9291

9392
void Telemetry::update()

drivers/cpp/base/Telemetry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void Telemetry::pub_f32(const char * topic, float msg)
153153
void Telemetry::sub(void (*callback)(TM_state * s, TM_msg * m),
154154
TM_state* userData)
155155
{
156-
subscribe(callback);
156+
subscribe(callback,userData);
157157
}
158158

159159
void Telemetry::update()

drivers/cpp/mbed/Telemetry.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ void Telemetry::pub_f32(const char * topic, float msg)
9191
void Telemetry::sub(void (*callback)(TM_state * s, TM_msg * m),
9292
TM_state* userData)
9393
{
94-
set_state(userData);
95-
subscribe(callback);
94+
subscribe(callback,userData);
9695
}
9796

9897
void Telemetry::update()

src/telemetry/c/telemetry_core.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,9 @@ void publish_f32(const char * t, float msg)
8080
frame(t,TM_float32,ptr,4);
8181
}
8282

83-
void set_state(TM_state * s)
83+
void subscribe(void (*callback)(TM_state* s, TM_msg* m), TM_state * s)
8484
{
8585
statePtr = s;
86-
}
87-
88-
void subscribe(void (*callback)(TM_state* s, TM_msg* m))
89-
{
9086
userCallback = callback;
9187
}
9288

src/telemetry/c/telemetry_utils.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,27 @@ uint32_t emplace_f32(TM_msg* m, float* dst)
7979
return 1;
8080
}
8181

82+
uint32_t match(TM_msg * m, const char * topicToMatch)
83+
{
84+
if(strcmp(m->topic,topicToMatch) == 0)
85+
return 1;
86+
87+
return 0;
88+
}
89+
90+
uint32_t fullmatch(TM_msg * m, const char * topicToMatch, TM_type typeToMatch)
91+
{
92+
if(strcmp(m->topic,topicToMatch) == 0 && m->type == typeToMatch)
93+
return 1;
94+
95+
return 0;
96+
}
8297

8398
uint32_t update(TM_msg * msg, const char *topic, char *var, size_t bufSize)
8499
{
85100
if(strcmp(topic,msg->topic) == 0)
86101
return emplace(msg, var, bufSize);
87-
102+
88103
return 0;
89104
}
90105

@@ -130,15 +145,15 @@ uint32_t update_i16(TM_msg * msg, const char *topic, int16_t *var)
130145

131146
uint32_t update_i32(TM_msg * msg, const char *topic, int32_t *var)
132147
{
133-
if(strcmp(topic,msg->topic) == 0)
134-
return emplace_i32(msg, var);
148+
if(strcmp(topic,msg->topic) == 0)
149+
return emplace_i32(msg, var);
135150

136151
return 0;
137152
}
138153

139154
uint32_t update_f32(TM_msg * msg, const char *topic, float *var)
140155
{
141-
if(strcmp(topic,msg->topic) == 0)
156+
if(strcmp(topic,msg->topic) == 0)
142157
return emplace_f32(msg, var);
143158

144159
return 0;

src/telemetry/headers/telemetry_core.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ void publish_i16(const char * topic, int16_t msg);
2020
void publish_i32(const char * topic, int32_t msg);
2121
void publish_f32(const char * topic, float msg);
2222

23-
void set_state(TM_state * s);
24-
25-
void subscribe(void (*callback)(TM_state * s, TM_msg * m));
23+
// subscribe a function to be called everytime a frame is received
24+
// second argument is a data structure that you can implement to access your program data inside the function
25+
void subscribe(void (*callback)(TM_state * s, TM_msg * m), TM_state * s);
2626

2727
void update_telemetry(float elapsedTime);
2828

src/telemetry/headers/telemetry_utils.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct TM_transport {
4242
};
4343

4444
// Decodes TM_msg buffer and emplaces its value into dst
45-
// Returns 0 if decoding was successful
45+
// Returns 1 (true) if decoding was successful
4646
uint32_t emplace(TM_msg * m, char * buf, size_t bufSize);
4747
uint32_t emplace_u8(TM_msg * m, uint8_t * dst);
4848
uint32_t emplace_u16(TM_msg * m, uint16_t * dst);
@@ -52,9 +52,16 @@ uint32_t emplace_i16(TM_msg * m, int16_t * dst);
5252
uint32_t emplace_i32(TM_msg * m, int32_t * dst);
5353
uint32_t emplace_f32(TM_msg * m, float * dst);
5454

55+
// Returns 1 if topicToMatch matches m->topic
56+
// 0 otherwise
57+
uint32_t match(TM_msg * m, const char * topicToMatch);
58+
59+
// Returns 1 if topicToMatch matches m->topic and typeToMatch matches m->type,
60+
// 0 otherwise
61+
uint32_t fullmatch(TM_msg * m, const char * topicToMatch, TM_type typeToMatch);
5562

5663
// Decodes TM_msg buffer and update its value into dst if matching topic
57-
// Returns 0 if decoding was successful
64+
// Returns 1 (true) if decoding was successful
5865
uint32_t update(TM_msg * msg, const char *topic, char *var, size_t bufSize);
5966
uint32_t update_u8(TM_msg * msg, const char *topic, uint8_t *var);
6067
uint32_t update_u16(TM_msg * msg, const char *topic, uint16_t *var);

src/tests/c/match_suite.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "test.h"
2+
3+
TEST match_simple()
4+
{
5+
TM_msg dummy;
6+
char topic[] = "foo";
7+
dummy.topic = topic;
8+
9+
ASSERT_EQ_FMT(match(&dummy, topic),1,"%u");
10+
ASSERT_EQ_FMT(match(&dummy, "foo"),1,"%u");
11+
ASSERT_EQ_FMT(match(&dummy, "bar"),0,"%u");
12+
PASS();
13+
}
14+
15+
TEST match_with_spaces()
16+
{
17+
TM_msg dummy;
18+
char topic[] = "foo with spaces";
19+
dummy.topic = topic;
20+
21+
ASSERT_EQ_FMT(match(&dummy, topic),1,"%u");
22+
ASSERT_EQ_FMT(match(&dummy, "foo with spaces"),1,"%u");
23+
ASSERT_EQ_FMT(match(&dummy, "foo"),0,"%u");
24+
ASSERT_EQ_FMT(match(&dummy, "bar"),0,"%u");
25+
PASS();
26+
}
27+
28+
TEST match_with_special_chars()
29+
{
30+
TM_msg dummy;
31+
char topic[] = "/:@#";
32+
dummy.topic = topic;
33+
34+
ASSERT_EQ_FMT(match(&dummy, topic),1,"%u");
35+
ASSERT_EQ_FMT(match(&dummy, "/:@#"),1,"%u");
36+
ASSERT_EQ_FMT(match(&dummy, "$#@:/"),0,"%u");
37+
ASSERT_EQ_FMT(match(&dummy, "bar"),0,"%u");
38+
PASS();
39+
}
40+
41+
TEST fullmatch_test()
42+
{
43+
TM_msg dummy;
44+
char topic[] = "foo";
45+
dummy.topic = topic;
46+
dummy.type = TM_uint8;
47+
48+
ASSERT_EQ_FMT(fullmatch(&dummy, topic, TM_uint8),1,"%u");
49+
ASSERT_EQ_FMT(fullmatch(&dummy, topic, TM_int8),0,"%u");
50+
ASSERT_EQ_FMT(fullmatch(&dummy, "foo", TM_uint8),1,"%u");
51+
ASSERT_EQ_FMT(fullmatch(&dummy, "foo", TM_int8),0,"%u");
52+
ASSERT_EQ_FMT(fullmatch(&dummy, "bar", TM_uint8),0,"%u");
53+
ASSERT_EQ_FMT(fullmatch(&dummy, "bar", TM_int16),0,"%u");
54+
PASS();
55+
}
56+
57+
SUITE(match_suite) {
58+
RUN_TEST(match_simple);
59+
RUN_TEST(match_with_spaces);
60+
RUN_TEST(match_with_special_chars);
61+
RUN_TEST(fullmatch_test);
62+
}

src/tests/c/pub_sub_float_suite.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ TEST publish_float()
8181
float value = 1.23E4;
8282

8383
init_telemetry(&transport);
84-
set_state(&state);
8584

86-
subscribe(callback_float);
85+
subscribe(callback_float,&state);
8786

8887
publish_f32(topic, value);
8988

@@ -120,9 +119,8 @@ TEST publish_float_neg()
120119
float value = -1.23E4;
121120

122121
init_telemetry(&transport);
123-
set_state(&state);
124122

125-
subscribe(callback_float);
123+
subscribe(callback_float,&state);
126124

127125
publish_f32(topic, value);
128126

0 commit comments

Comments
 (0)