Skip to content

Commit

Permalink
Added Team Display
Browse files Browse the repository at this point in the history
The team is now displayed when the down button is pressed on the watch. Press again to move back to status.
  • Loading branch information
333fred committed Nov 27, 2015
1 parent 854a349 commit 043b527
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 37 deletions.
12 changes: 6 additions & 6 deletions appinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
"projectType": "native",
"resources": {
"media": [
{
"characterRegex": "[0-9:a-zA-Z]",
"file": "fonts/SourceCodePro-Regular.ttf",
"name": "SOURCE_CODE_PRO_REG_38",
"type": "font"
},
{
"file": "images/FIRSTicon_RGB.png",
"menuIcon": true,
"name": "main_icon_image",
"type": "png"
},
{
"characterRegex": "[0-9:a-zA-Z]",
"file": "fonts/SourceCodePro-Regular.ttf",
"name": "SOURCE_CODE_PRO_REG_38",
"type": "font"
}
]
},
Expand Down
151 changes: 120 additions & 31 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#include <pebble.h>
#include "utility.h"

#define RED1 1
#define RED2 2
#define RED3 3
#define BLUE1 4
#define BLUE2 5
#define BLUE3 6
#define RED1_STATUS 1
#define RED2_STATUS 2
#define RED3_STATUS 3
#define BLUE1_STATUS 4
#define BLUE2_STATUS 5
#define BLUE3_STATUS 6
#define VIBE 7
#define UPDATE 8
#define RED1_NUMBER 9
#define RED2_NUMBER 10
#define RED3_NUMBER 11
#define BLUE1_NUMBER 12
#define BLUE2_NUMBER 13
#define BLUE3_NUMBER 14
#define CHECK_TYPE(type, size) if (type != TUPLE_UINT && size != 1) { \
APP_LOG(APP_LOG_LEVEL_ERROR, "Received nonuint type %d", (int) type); \
t = dict_read_next(iter); \
continue; \
}
}

// Constant text strings for the connection statuses
const char *eth = "Eth", *ds = "DS", *radio = "Rd", *rio = "RIO", *code = "Cd", *estop = "Est", *good = "G", *bwu = "BWU", *byp = "BYP";
Expand All @@ -29,50 +35,112 @@ static TextLayer *s_blue1;
static TextLayer *s_blue2;
static TextLayer *s_blue3;
static GFont *s_source_code_pro;
static int s_red1_num = 4;
static int s_red2_num = 5;
static int s_red3_num = 6;
static int s_blue1_num = 1;
static int s_blue2_num = 2;
static int s_blue3_num = 3;
static int s_show_team_nums = 0;

static char *s_red1_text;
static char *s_red2_text;
static char *s_red3_text;
static char *s_blue1_text;
static char *s_blue2_text;
static char *s_blue3_text;

typedef enum {
ETH=0, DS=1, RADIO=2, RIO=3, CODE=4, ESTOP=5, GOOD=6, BWU=7, BYP = 8
} status_type;

static status_type s_red1_status = 0;
static status_type s_red2_status = 0;
static status_type s_red3_status = 0;
static status_type s_blue1_status = 0;
static status_type s_blue2_status = 0;
static status_type s_blue3_status = 0;

void set_alliance_text(const char *text, bool hi_contrast, uint8_t alliance, uint8_t team);
void set_alliance_status(status_type status, uint8_t alliance, uint8_t team);

// Updates the text on the screen with the current values. It uses the correct display whether
// the current update type is status or team number.
void update_text() {
if (s_show_team_nums) {
snprintf(s_red1_text, 5, "%d", s_red1_num);
snprintf(s_red2_text, 5, "%d", s_red2_num);
snprintf(s_red3_text, 5, "%d", s_red3_num);
snprintf(s_blue1_text, 5, "%d", s_blue1_num);
snprintf(s_blue2_text, 5, "%d", s_blue2_num);
snprintf(s_blue3_text, 5, "%d", s_blue3_num);
set_alliance_text(s_red1_text, false, 1, 1);
set_alliance_text(s_red2_text, false, 1, 2);
set_alliance_text(s_red3_text, false, 1, 3);
set_alliance_text(s_blue1_text, false, 2, 1);
set_alliance_text(s_blue2_text, false, 2, 2);
set_alliance_text(s_blue3_text, false, 2, 3);
} else {
set_alliance_status(s_red1_status, 1, 1);
set_alliance_status(s_red2_status, 1, 2);
set_alliance_status(s_red3_status, 1, 3);
set_alliance_status(s_blue1_status, 2, 1);
set_alliance_status(s_blue2_status, 2, 2);
set_alliance_status(s_blue3_status, 2, 3);
}
}

// Callback for receiving a message
static void inbox_received_callback(DictionaryIterator *iter, void *ctx) {
Tuple *t = dict_read_first(iter);
while (t != NULL) {
if (t->type != TUPLE_UINT && t->length != 1) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Received nonuint type %d", (int) t->type);
t = dict_read_next(iter);
continue;
}
uint8_t status = t->value->uint8;
CHECK_TYPE(t->type, t->length)
uint32_t status = t->value->uint32;
switch (t->key) {
case RED1:
set_alliance_status((status_type) status, 1, 1);
case RED1_STATUS:
s_red1_status = (status_type) status;
break;
case RED2:
set_alliance_status((status_type) status, 1, 2);
case RED2_STATUS:
s_red2_status = (status_type) status;
break;
case RED3:
set_alliance_status((status_type) status, 1, 3);
case RED3_STATUS:
s_red3_status = (status_type) status;
break;
case BLUE1:
set_alliance_status((status_type) status, 2, 1);
case BLUE1_STATUS:
s_blue1_status = (status_type) status;
break;
case BLUE2:
set_alliance_status((status_type) status, 2, 2);
case BLUE2_STATUS:
s_blue2_status = (status_type) status;
break;
case BLUE3:
set_alliance_status((status_type) status, 2, 3);
case BLUE3_STATUS:
s_blue3_status = (status_type) status;
break;
case VIBE:
APP_LOG(APP_LOG_LEVEL_INFO, "Vibrating");
vibes_short_pulse();
break;
case RED1_NUMBER:
s_red1_num = status;
break;
case RED2_NUMBER:
s_red2_num = status;
break;
case RED3_NUMBER:
s_red3_num = status;
break;
case BLUE1_NUMBER:
s_blue1_num = status;
break;
case BLUE2_NUMBER:
s_blue2_num = status;
break;
case BLUE3_NUMBER:
s_blue3_num = status;
break;
}

t = dict_read_next(iter);
}
update_text();
}

// Requests an update from the companion app, if it's available
Expand All @@ -89,11 +157,6 @@ void select_single_click_handler(ClickRecognizerRef recognizer, void *context) {
request_update();
}

// ClickConfigProvider for the refresh button
void config_provider(Window *window) {
window_single_click_subscribe(BUTTON_ID_SELECT, select_single_click_handler);
}

static void message_dropped_callback(AppMessageResult reason, void *ctx) {
APP_LOG(APP_LOG_LEVEL_WARNING, "Message Dropped: %s", translate_error(reason));
}
Expand All @@ -118,6 +181,7 @@ static void canvas_update_proc(Layer *this_layer, GContext *ctx) {
graphics_draw_line(ctx, GPoint(0, 112), GPoint(144, 112));
}

// Sets the alliance text to be the given text string for the given alliance and team.
void set_alliance_text(const char *text, bool hi_contrast, uint8_t alliance, uint8_t team) {
uint16_t switch_mult = (alliance << 8) | team;
TextLayer *team_layer;
Expand Down Expand Up @@ -157,6 +221,7 @@ void set_alliance_text(const char *text, bool hi_contrast, uint8_t alliance, uin
// Sets the status of an alliance based on the given status type
void set_alliance_status(status_type status, uint8_t alliance, uint8_t team) {
// If the app has told us to vibrate, then vibrate
if (s_show_team_nums) return;
switch (status) {
case ETH:
set_alliance_text(eth, true, alliance, team);
Expand Down Expand Up @@ -188,6 +253,18 @@ void set_alliance_status(status_type status, uint8_t alliance, uint8_t team) {
}
}

// Down click handler - shows the team numbers for 3 seconds.
static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
s_show_team_nums = !s_show_team_nums;
update_text();
}

// ClickConfigProvider for the refresh button
void config_provider(Window *window) {
window_single_click_subscribe(BUTTON_ID_SELECT, select_single_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
}

void setup_alliance_textlayer(TextLayer **layer, Layer *parent, int x, int y) {
*layer = text_layer_create(GRect(x, y, 74, 46));
text_layer_set_text_alignment(*layer, GTextAlignmentCenter);
Expand Down Expand Up @@ -251,6 +328,12 @@ static void main_window_unload(Window *window) {
}

static void init() {
s_red1_text = (char*) malloc(5 * sizeof(char));
s_red2_text = (char*) malloc(5 * sizeof(char));
s_red3_text = (char*) malloc(5 * sizeof(char));
s_blue1_text = (char*) malloc(5 * sizeof(char));
s_blue2_text = (char*) malloc(5 * sizeof(char));
s_blue3_text = (char*) malloc(5 * sizeof(char));
s_source_code_pro = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_SOURCE_CODE_PRO_REG_38));
s_main_window = window_create();
window_set_window_handlers(s_main_window, (WindowHandlers) {
Expand All @@ -274,6 +357,12 @@ static void deinit() {
window_destroy(s_main_window);
fonts_unload_custom_font(s_source_code_pro);
app_message_deregister_callbacks();
free(s_red1_text);
free(s_red2_text);
free(s_red3_text);
free(s_blue1_text);
free(s_blue2_text);
free(s_blue3_text);
}

int main() {
Expand Down

0 comments on commit 043b527

Please sign in to comment.