@@ -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
2227static 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+
4463static 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+ }
0 commit comments