| ... | ... |
@@ -1,13 +1,14 @@ |
| 1 | 1 |
#include <pebble.h> |
| 2 |
+#define ACCEL_STEP_MS 300000 |
|
| 2 | 3 |
|
| 3 | 4 |
static Window *s_window; |
| 4 |
-static StatusBarLayer *s_status_text_layer; |
|
| 5 |
+static TextLayer *s_main_text_layer; |
|
| 6 |
+/*static StatusBarLayer *s_status_text_layer; |
|
| 5 | 7 |
static TextLayer *s_upperleft_text_layer; |
| 6 | 8 |
static TextLayer *s_upperright_text_layer; |
| 7 |
-static TextLayer *s_main_text_layer; |
|
| 8 | 9 |
static TextLayer *s_lowerleft_text_layer; |
| 9 | 10 |
static TextLayer *s_lowerright_text_layer; |
| 10 |
-static TextLayer *s_lower_text_layer; |
|
| 11 |
+static TextLayer *s_lower_text_layer;*/ |
|
| 11 | 12 |
|
| 12 | 13 |
// Largest expected inbox and outbox message sizes |
| 13 | 14 |
const uint32_t inbox_size = 150; |
| ... | ... |
@@ -15,16 +16,49 @@ const uint32_t outbox_size = 64; |
| 15 | 16 |
|
| 16 | 17 |
static uint32_t size ; |
| 17 | 18 |
static char s_status[4]; |
| 18 |
-static char s_msg[50]; |
|
| 19 |
+//static char s_msg[50]; |
|
| 19 | 20 |
|
| 20 | 21 |
static bool s_js_ready; |
| 21 | 22 |
|
| 22 | 23 |
typedef enum {
|
| 23 | 24 |
status, |
| 24 |
- event |
|
| 25 |
+ x, |
|
| 26 |
+ y, |
|
| 27 |
+ z |
|
| 25 | 28 |
} AppKey; |
| 26 | 29 |
|
| 27 | 30 |
|
| 31 |
+static void accel_data_handler(void *data) {
|
|
| 32 |
+ AccelData accel = (AccelData) { .x = 0, .y = 0, .z = 0 };
|
|
| 33 |
+ accel_service_peek(&accel); |
|
| 34 |
+ APP_LOG(APP_LOG_LEVEL_INFO, "t: %llu, x: %d, y: %d, z: %d",accel.timestamp, accel.x , accel.y, accel.z); |
|
| 35 |
+ |
|
| 36 |
+ APP_LOG(APP_LOG_LEVEL_INFO, "size of timestamp : %u",sizeof(accel.timestamp)); |
|
| 37 |
+ |
|
| 38 |
+ // Declare the dictionary's iterator |
|
| 39 |
+ DictionaryIterator *out_iter; |
|
| 40 |
+ |
|
| 41 |
+ // Prepare the outbox buffer for this message |
|
| 42 |
+ AppMessageResult result = app_message_outbox_begin(&out_iter); |
|
| 43 |
+ |
|
| 44 |
+ if(result == APP_MSG_OK) {
|
|
| 45 |
+ dict_write_cstring(out_iter, status, "position"); |
|
| 46 |
+ dict_write_int16(out_iter, x, accel.x); |
|
| 47 |
+ dict_write_int16(out_iter, y, accel.y); |
|
| 48 |
+ dict_write_int16(out_iter, z, accel.z); |
|
| 49 |
+ |
|
| 50 |
+ // Send this message |
|
| 51 |
+ result = app_message_outbox_send(); |
|
| 52 |
+ if(result != APP_MSG_OK) {
|
|
| 53 |
+ APP_LOG(APP_LOG_LEVEL_ERROR, "Error sending the outbox: %d", (int)result); |
|
| 54 |
+ } |
|
| 55 |
+ } else {
|
|
| 56 |
+ // The outbox cannot be used right now |
|
| 57 |
+ APP_LOG(APP_LOG_LEVEL_ERROR, "Error preparing the outbox: %d", (int)result); |
|
| 58 |
+ } |
|
| 59 |
+ app_timer_register(ACCEL_STEP_MS, accel_data_handler, NULL); |
|
| 60 |
+} |
|
| 61 |
+ |
|
| 28 | 62 |
static void send_message(char * msg){
|
| 29 | 63 |
|
| 30 | 64 |
// Declare the dictionary's iterator |
| ... | ... |
@@ -187,6 +221,11 @@ static void prv_init(void) {
|
| 187 | 221 |
app_message_register_outbox_sent(outbox_sent_callback); |
| 188 | 222 |
// Register to be notified about outbox failed events |
| 189 | 223 |
app_message_register_outbox_failed(outbox_failed_callback); |
| 224 |
+ |
|
| 225 |
+ accel_data_service_subscribe(0, NULL); |
|
| 226 |
+ |
|
| 227 |
+ app_timer_register(1000, accel_data_handler, NULL); |
|
| 228 |
+ |
|
| 190 | 229 |
|
| 191 | 230 |
|
| 192 | 231 |
s_window = window_create(); |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,213 @@ |
| 1 |
+#include <pebble.h> |
|
| 2 |
+ |
|
| 3 |
+static Window *s_window; |
|
| 4 |
+static StatusBarLayer *s_status_text_layer; |
|
| 5 |
+static TextLayer *s_upperleft_text_layer; |
|
| 6 |
+static TextLayer *s_upperright_text_layer; |
|
| 7 |
+static TextLayer *s_main_text_layer; |
|
| 8 |
+static TextLayer *s_lowerleft_text_layer; |
|
| 9 |
+static TextLayer *s_lowerright_text_layer; |
|
| 10 |
+static TextLayer *s_lower_text_layer; |
|
| 11 |
+ |
|
| 12 |
+// Largest expected inbox and outbox message sizes |
|
| 13 |
+const uint32_t inbox_size = 150; |
|
| 14 |
+const uint32_t outbox_size = 64; |
|
| 15 |
+ |
|
| 16 |
+static uint32_t size ; |
|
| 17 |
+static char s_status[4]; |
|
| 18 |
+static char s_msg[50]; |
|
| 19 |
+ |
|
| 20 |
+static bool s_js_ready; |
|
| 21 |
+ |
|
| 22 |
+typedef enum {
|
|
| 23 |
+ status, |
|
| 24 |
+ event |
|
| 25 |
+} AppKey; |
|
| 26 |
+ |
|
| 27 |
+ |
|
| 28 |
+static void send_message(char * msg){
|
|
| 29 |
+ |
|
| 30 |
+ // Declare the dictionary's iterator |
|
| 31 |
+ DictionaryIterator *out_iter; |
|
| 32 |
+ |
|
| 33 |
+ // Prepare the outbox buffer for this message |
|
| 34 |
+ AppMessageResult result = app_message_outbox_begin(&out_iter); |
|
| 35 |
+ |
|
| 36 |
+ if(result == APP_MSG_OK) {
|
|
| 37 |
+ dict_write_cstring(out_iter, status, msg); |
|
| 38 |
+ |
|
| 39 |
+ // Send this message |
|
| 40 |
+ result = app_message_outbox_send(); |
|
| 41 |
+ if(result != APP_MSG_OK) {
|
|
| 42 |
+ APP_LOG(APP_LOG_LEVEL_ERROR, "Error sending the outbox: %d", (int)result); |
|
| 43 |
+ } |
|
| 44 |
+ } else {
|
|
| 45 |
+ // The outbox cannot be used right now |
|
| 46 |
+ APP_LOG(APP_LOG_LEVEL_ERROR, "Error preparing the outbox: %d", (int)result); |
|
| 47 |
+ } |
|
| 48 |
+} |
|
| 49 |
+ |
|
| 50 |
+static void prv_up_click_handler(ClickRecognizerRef recognizer, void *context) {
|
|
| 51 |
+ text_layer_set_text(s_main_text_layer, "wakeup"); |
|
| 52 |
+ send_message("wakeup");
|
|
| 53 |
+} |
|
| 54 |
+static void prv_select_click_handler(ClickRecognizerRef recognizer, void *context) {
|
|
| 55 |
+ text_layer_set_text(s_main_text_layer, "awake"); |
|
| 56 |
+ send_message("awake");
|
|
| 57 |
+} |
|
| 58 |
+static void prv_down_click_handler(ClickRecognizerRef recognizer, void *context) {
|
|
| 59 |
+ text_layer_set_text(s_main_text_layer, "gotosleep"); |
|
| 60 |
+ send_message("gotosleep");
|
|
| 61 |
+} |
|
| 62 |
+ |
|
| 63 |
+static void prv_click_config_provider(void *context) {
|
|
| 64 |
+ window_single_click_subscribe(BUTTON_ID_SELECT, prv_select_click_handler); |
|
| 65 |
+ window_single_click_subscribe(BUTTON_ID_UP, prv_up_click_handler); |
|
| 66 |
+ window_single_click_subscribe(BUTTON_ID_DOWN, prv_down_click_handler); |
|
| 67 |
+} |
|
| 68 |
+ |
|
| 69 |
+static void prv_window_load(Window *window) {
|
|
| 70 |
+ Layer *window_layer = window_get_root_layer(window); |
|
| 71 |
+ GRect bounds = layer_get_bounds(window_layer); |
|
| 72 |
+ |
|
| 73 |
+ s_main_text_layer = text_layer_create(GRect(0, 72, bounds.size.w, 20)); |
|
| 74 |
+ text_layer_set_text(s_main_text_layer, "Press a button"); |
|
| 75 |
+ text_layer_set_text_alignment(s_main_text_layer, GTextAlignmentCenter); |
|
| 76 |
+ layer_add_child(window_layer, text_layer_get_layer(s_main_text_layer)); |
|
| 77 |
+} |
|
| 78 |
+ |
|
| 79 |
+static void prv_window_unload(Window *window) {
|
|
| 80 |
+ text_layer_destroy(s_main_text_layer); |
|
| 81 |
+} |
|
| 82 |
+ |
|
| 83 |
+void comm_is_ready() {
|
|
| 84 |
+ |
|
| 85 |
+ // set the text |
|
| 86 |
+ text_layer_set_text(s_main_text_layer, "ready"); |
|
| 87 |
+ /*text_layer_set_text(s_upperleft_text_layer, "-"); |
|
| 88 |
+ text_layer_set_text(s_upperleft_text_layer, "-"); |
|
| 89 |
+ text_layer_set_text(s_main_text_layer, "Ready"); |
|
| 90 |
+ text_layer_set_text(s_lowerleft_text_layer, "-"); |
|
| 91 |
+ text_layer_set_text(s_lowerright_text_layer, "-"); |
|
| 92 |
+ text_layer_set_text(s_lower_text_layer, "--:--:--");*/ |
|
| 93 |
+ |
|
| 94 |
+} |
|
| 95 |
+ |
|
| 96 |
+static void inbox_received_callback(DictionaryIterator *iter, void *context) {
|
|
| 97 |
+ // A new message has been successfully received |
|
| 98 |
+ APP_LOG(APP_LOG_LEVEL_INFO, "New message! "); |
|
| 99 |
+ |
|
| 100 |
+ size = dict_size(iter); |
|
| 101 |
+ |
|
| 102 |
+ // JS readiness |
|
| 103 |
+ Tuple *ready_tuple = dict_find(iter, MESSAGE_KEY_JSReady); |
|
| 104 |
+ |
|
| 105 |
+ if(ready_tuple){
|
|
| 106 |
+ //Pebblekit JS is ready ! Safe to send messages |
|
| 107 |
+ s_js_ready = true; |
|
| 108 |
+ comm_is_ready(); |
|
| 109 |
+ //APP_LOG(APP_LOG_LEVEL_DEBUG, "received jsready message on watch... "); |
|
| 110 |
+ } |
|
| 111 |
+ |
|
| 112 |
+ |
|
| 113 |
+ // Read status returns |
|
| 114 |
+ Tuple *status_tuple = dict_find(iter, MESSAGE_KEY_status); |
|
| 115 |
+ |
|
| 116 |
+ if(status_tuple) {
|
|
| 117 |
+ memset(s_status,'\0',sizeof(s_status)); |
|
| 118 |
+ strncpy(s_status, status_tuple->value->cstring, 4); |
|
| 119 |
+ //APP_LOG(APP_LOG_LEVEL_DEBUG, "status message received : %s",s_status); |
|
| 120 |
+ |
|
| 121 |
+ // DEBUG concatenate all data received |
|
| 122 |
+ /* |
|
| 123 |
+ memset(s_msg,'\0',sizeof(s_msg)); |
|
| 124 |
+ strncpy(s_msg, "Acc. ",5); |
|
| 125 |
+ strcat(s_msg, s_accuracy); |
|
| 126 |
+ strcat(s_msg, " Alt. "); |
|
| 127 |
+ strcat(s_msg, s_altitude); |
|
| 128 |
+ strcat(s_msg, "\n Max. "); |
|
| 129 |
+ strcat(s_msg, s_max_speed); |
|
| 130 |
+ strcat(s_msg, "\n Lat. "); |
|
| 131 |
+ strcat(s_msg, s_latitude); |
|
| 132 |
+ strcat(s_msg, "\n Long. "); |
|
| 133 |
+ strcat(s_msg, s_longitude); |
|
| 134 |
+ strcat(s_msg, "\n Time. "); |
|
| 135 |
+ strcat(s_msg, s_timestamp); |
|
| 136 |
+ */ |
|
| 137 |
+ |
|
| 138 |
+ //APP_LOG(APP_LOG_LEVEL_DEBUG, "to display : %s ",s_msg); |
|
| 139 |
+ if(strcmp(s_status, "L200")==0){
|
|
| 140 |
+ /*text_layer_set_text(s_upperleft_text_layer, s_altitude); |
|
| 141 |
+ text_layer_set_text(s_upperright_text_layer, s_distance); |
|
| 142 |
+ text_layer_set_text(s_main_text_layer, s_speed); |
|
| 143 |
+ text_layer_set_text(s_lowerleft_text_layer, s_avg_speed); |
|
| 144 |
+ text_layer_set_text(s_lowerright_text_layer, s_max_speed); |
|
| 145 |
+ text_layer_set_text(s_lower_text_layer, s_duration);*/ |
|
| 146 |
+ }else if(strcmp(s_status, "S200")==0){
|
|
| 147 |
+ //text_layer_set_text(s_lower_text_layer, "upload OK"); |
|
| 148 |
+ vibes_short_pulse(); |
|
| 149 |
+ }else if(strcmp(s_status, "S401")==0){
|
|
| 150 |
+ //text_layer_set_text(s_lower_text_layer, "auth err, retrying"); |
|
| 151 |
+ }else{
|
|
| 152 |
+ //text_layer_set_text(s_lower_text_layer, s_status); |
|
| 153 |
+ vibes_double_pulse(); |
|
| 154 |
+ } |
|
| 155 |
+ }else{
|
|
| 156 |
+ //APP_LOG(APP_LOG_LEVEL_DEBUG, "not status message... "); |
|
| 157 |
+ } |
|
| 158 |
+ |
|
| 159 |
+} |
|
| 160 |
+ |
|
| 161 |
+ |
|
| 162 |
+static void inbox_dropped_callback(AppMessageResult reason, void *context) {
|
|
| 163 |
+ // A message was received, but had to be dropped |
|
| 164 |
+ APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped. Reason: %d", (int)reason); |
|
| 165 |
+} |
|
| 166 |
+ |
|
| 167 |
+static void outbox_sent_callback(DictionaryIterator *iter, void *context) {
|
|
| 168 |
+ // The message just sent has been successfully delivered |
|
| 169 |
+ APP_LOG(APP_LOG_LEVEL_INFO, "Message sent. "); |
|
| 170 |
+} |
|
| 171 |
+ |
|
| 172 |
+static void outbox_failed_callback(DictionaryIterator *iter, AppMessageResult reason, void *context) {
|
|
| 173 |
+ // The message just sent failed to be delivered |
|
| 174 |
+ APP_LOG(APP_LOG_LEVEL_ERROR, "Message send failed. Reason: %d", (int)reason); |
|
| 175 |
+} |
|
| 176 |
+ |
|
| 177 |
+static void prv_init(void) {
|
|
| 178 |
+ |
|
| 179 |
+ // Open AppMessage |
|
| 180 |
+ app_message_open(inbox_size, outbox_size); |
|
| 181 |
+ |
|
| 182 |
+ // Register to be notified about inbox received events |
|
| 183 |
+ app_message_register_inbox_received(inbox_received_callback); |
|
| 184 |
+ // Register to be notified about inbox dropped events |
|
| 185 |
+ app_message_register_inbox_dropped(inbox_dropped_callback); |
|
| 186 |
+ // Register to be notified about outbox sent events |
|
| 187 |
+ app_message_register_outbox_sent(outbox_sent_callback); |
|
| 188 |
+ // Register to be notified about outbox failed events |
|
| 189 |
+ app_message_register_outbox_failed(outbox_failed_callback); |
|
| 190 |
+ |
|
| 191 |
+ |
|
| 192 |
+ s_window = window_create(); |
|
| 193 |
+ window_set_click_config_provider(s_window, prv_click_config_provider); |
|
| 194 |
+ window_set_window_handlers(s_window, (WindowHandlers) {
|
|
| 195 |
+ .load = prv_window_load, |
|
| 196 |
+ .unload = prv_window_unload, |
|
| 197 |
+ }); |
|
| 198 |
+ const bool animated = true; |
|
| 199 |
+ window_stack_push(s_window, animated); |
|
| 200 |
+} |
|
| 201 |
+ |
|
| 202 |
+static void prv_deinit(void) {
|
|
| 203 |
+ window_destroy(s_window); |
|
| 204 |
+} |
|
| 205 |
+ |
|
| 206 |
+int main(void) {
|
|
| 207 |
+ prv_init(); |
|
| 208 |
+ |
|
| 209 |
+ //APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", s_window); |
|
| 210 |
+ |
|
| 211 |
+ app_event_loop(); |
|
| 212 |
+ prv_deinit(); |
|
| 213 |
+} |