Skip to content

Commit 3d46239

Browse files
ChrisGammellszczys
authored andcommitted
added state counters and handled desired input
1 parent f3cac7a commit 3d46239

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

src/app_state.c

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ LOG_MODULE_REGISTER(app_state, LOG_LEVEL_DBG);
1414
#include "app_state.h"
1515
#include "app_work.h"
1616

17-
#define DEVICE_STATE_FMT "{\"example_int0\":%d,\"example_int1\":%d}"
17+
#define DEVICE_STATE_FMT "{\"counter_up\":%d,\"counter_down\":%d}"
18+
#define BUTTON_STATE_FMT "{\"last_press\":%d,\"this_press\":%d}"
19+
#define MAX_COUNT 10000
20+
#define MIN_COUNT 0
1821

19-
uint32_t _example_int0;
20-
uint32_t _example_int1 = 1;
22+
uint32_t _counter_up = MIN_COUNT;
23+
uint32_t _counter_down = MAX_COUNT - 1;
24+
25+
uint32_t _last_button_press_time=0;
2126

2227
static struct golioth_client *client;
2328

@@ -30,7 +35,7 @@ static int async_handler(struct golioth_req_rsp *rsp)
3035
return rsp->err;
3136
}
3237

33-
LOG_DBG("State successfully set");
38+
//LOG_DBG("State successfully set");
3439

3540
return 0;
3641
}
@@ -41,6 +46,20 @@ void app_state_init(struct golioth_client *state_client)
4146
k_sem_give(&update_actual);
4247
}
4348

49+
50+
void state_counter_change(void)
51+
{
52+
if (_counter_down > MIN_COUNT)
53+
{
54+
_counter_down--;
55+
}
56+
if (_counter_up < MAX_COUNT)
57+
{
58+
_counter_up++;
59+
}
60+
app_state_update_actual();
61+
}
62+
4463
static void reset_desired_state(void)
4564
{
4665
LOG_INF("Resetting \"%s\" LightDB State endpoint to defaults.",
@@ -66,7 +85,7 @@ void app_state_update_actual(void)
6685

6786
char sbuf[strlen(DEVICE_STATE_FMT)+8]; /* small bit of extra space */
6887

69-
snprintk(sbuf, sizeof(sbuf), DEVICE_STATE_FMT, _example_int0, _example_int1);
88+
snprintk(sbuf, sizeof(sbuf), DEVICE_STATE_FMT, _counter_up, _counter_down);
7089

7190
int err;
7291

@@ -104,30 +123,30 @@ int app_state_desired_handler(struct golioth_req_rsp *rsp)
104123
uint8_t state_change_count = 0;
105124

106125
if (ret & 1<<0) {
107-
/* Process example_int0 */
108-
if ((parsed_state.example_int0 >= 0) && (parsed_state.example_int0 < 10000)) {
109-
LOG_DBG("Validated desired example_int0 value: %d", parsed_state.example_int0);
110-
_example_int0 = parsed_state.example_int0;
126+
/* Process counter_up */
127+
if ((parsed_state.counter_up >= MIN_COUNT) && (parsed_state.counter_up < MAX_COUNT)) {
128+
LOG_DBG("Validated desired counter_up value: %d", parsed_state.counter_up);
129+
_counter_up = parsed_state.counter_up;
111130
++desired_processed_count;
112131
++state_change_count;
113-
} else if (parsed_state.example_int0 == -1) {
114-
LOG_DBG("No change requested for example_int0");
132+
} else if (parsed_state.counter_up == -1) {
133+
LOG_DBG("No change requested for counter_up");
115134
} else {
116-
LOG_ERR("Invalid desired example_int0 value: %d", parsed_state.example_int0);
135+
LOG_ERR("Invalid desired counter_up value: %d", parsed_state.counter_up);
117136
++desired_processed_count;
118137
}
119138
}
120139
if (ret & 1<<1) {
121-
/* Process example_int1 */
122-
if ((parsed_state.example_int1 >= 0) && (parsed_state.example_int1 < 10000)) {
123-
LOG_DBG("Validated desired example_int1 value: %d", parsed_state.example_int1);
124-
_example_int1 = parsed_state.example_int1;
140+
/* Process counter_down */
141+
if ((parsed_state.counter_down >= MIN_COUNT) && (parsed_state.counter_down < MAX_COUNT)) {
142+
LOG_DBG("Validated desired counter_down value: %d", parsed_state.counter_down);
143+
_counter_down = parsed_state.counter_down;
125144
++desired_processed_count;
126145
++state_change_count;
127-
} else if (parsed_state.example_int1 == -1) {
128-
LOG_DBG("No change requested for example_int1");
146+
} else if (parsed_state.counter_down == -1) {
147+
LOG_DBG("No change requested for counter_down");
129148
} else {
130-
LOG_ERR("Invalid desired example_int1 value: %d", parsed_state.example_int1);
149+
LOG_ERR("Invalid desired counter_down value: %d", parsed_state.counter_down);
131150
++desired_processed_count;
132151
}
133152
}
@@ -160,5 +179,4 @@ void app_state_observe(void)
160179
if (k_sem_take(&update_actual, K_NO_WAIT) == 0) {
161180
app_state_update_actual();
162181
}
163-
}
164-
182+
}

src/app_state.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
#define APP_STATE_DESIRED_ENDP "desired"
1313
#define APP_STATE_ACTUAL_ENDP "state"
14+
#define APP_STATE_BUTTON_ENDP "button"
1415

1516
void app_state_init(struct golioth_client *state_client);
1617
void app_state_observe(void);
1718
void app_state_update_actual(void);
1819

20+
void state_counter_change(void);
21+
1922
#endif /* __APP_STATE_H__ */

src/json_helper.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#include <zephyr/data/json.h>
55

66
struct app_state {
7-
int32_t example_int0;
8-
int32_t example_int1;
7+
int32_t counter_up;
8+
int32_t counter_down;
99
};
1010

1111
static const struct json_obj_descr app_state_descr[] = {
12-
JSON_OBJ_DESCR_PRIM(struct app_state, example_int0, JSON_TOK_NUMBER),
13-
JSON_OBJ_DESCR_PRIM(struct app_state, example_int1, JSON_TOK_NUMBER)
12+
JSON_OBJ_DESCR_PRIM(struct app_state, counter_up, JSON_TOK_NUMBER),
13+
JSON_OBJ_DESCR_PRIM(struct app_state, counter_down, JSON_TOK_NUMBER)
1414
};
1515

1616
#endif

src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ static void lte_handler(const struct lte_lc_evt *const evt)
9898
void button_pressed(const struct device *dev, struct gpio_callback *cb,
9999
uint32_t pins)
100100
{
101-
LOG_DBG("Button pressed at %d", k_cycle_get_32());
101+
uint32_t button_kernel_time = k_cycle_get_32();
102+
LOG_DBG("Button pressed at %d", button_kernel_time);
103+
102104
play_beep_once();
103105
k_wakeup(_system_thread);
104106
}
@@ -205,6 +207,7 @@ void main(void)
205207
}
206208

207209
app_work_sensor_read();
210+
state_counter_change();
208211

209212
k_sleep(K_SECONDS(get_loop_delay_s()));
210213

0 commit comments

Comments
 (0)