Skip to content

Commit

Permalink
Add device status subtitle
Browse files Browse the repository at this point in the history
  • Loading branch information
afwolfe committed Feb 7, 2022
1 parent ae91476 commit b8e4f55
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ The `watchapp/` folder contains the source for the Pebble app. The PebbleKit JS
- [ ] Additional information on watchapp
- [x] Show request success/failure on Pebble
- [ ] Icons for device class
- [ ] Battery level
- [x] Battery level and status
- [ ] Location name using reverse geocoding API
- [ ] Add endpoints to login/save iCloud credentials without pyicloud's `icloud` CLI.
- [ ] Additional endpoint to set "Lost Mode"
Expand Down
3 changes: 3 additions & 0 deletions watchapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
"watchface": false
},
"messageKeys": [
"BatteryLevel",
"BatteryStatus",
"Command",
"DeviceClass",
"DeviceName",
"DeviceId",
"DeviceStatus",
"Status",
"Server",
"Username",
Expand Down
7 changes: 5 additions & 2 deletions watchapp/src/c/appmessage.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ static void in_out_size_calc() {

Tuplet in_values[] = {
TupletInteger(MESSAGE_KEY_Command, COMMAND_FIND),
TupletCString(MESSAGE_KEY_DeviceName, "abcdefghijklmnopqrstuvwxyz123456"),
TupletInteger(MESSAGE_KEY_DeviceId, 123),
TupletInteger(MESSAGE_KEY_DeviceClass, TABLET)
TupletCString(MESSAGE_KEY_DeviceName, "abcdefghijklmnopqrstuvwxyz123456"),
TupletInteger(MESSAGE_KEY_DeviceClass, TABLET),
TupletInteger(MESSAGE_KEY_DeviceStatus, 200),
TupletCString(MESSAGE_KEY_BatteryLevel, "100"),
TupletInteger(MESSAGE_KEY_BatteryStatus, CHARGING)
};
inbound_size = dict_calc_buffer_size_from_tuplets(in_values, ARRAY_LENGTH(in_values)) + 8;

Expand Down
7 changes: 6 additions & 1 deletion watchapp/src/c/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
static DeviceSummary devices[MAX_DEVICES];
static int device_count = 0;

void add_device(uint8_t deviceId, char *deviceName, DeviceClass deviceClass) {
void add_device(uint8_t deviceId, char *deviceName, DeviceClass deviceClass, uint16_t deviceStatus, char *batteryLevel, BatteryStatus batteryStatus) {

// void add_device(uint8_t deviceId, char *deviceName, DeviceClass deviceClass) {

// Don't do anything if device already exists.
for (int i=0; i<device_count; i++) {
Expand All @@ -21,6 +23,9 @@ void add_device(uint8_t deviceId, char *deviceName, DeviceClass deviceClass) {
devices[device_count].deviceId = deviceId;
strcpy(devices[device_count].deviceName, deviceName);
devices[device_count].deviceClass = deviceClass;
devices[device_count].deviceStatus = deviceStatus;
strcpy(devices[device_count].batteryLevel, batteryLevel);
devices[device_count].batteryStatus = batteryStatus;
device_count++;

}
Expand Down
13 changes: 11 additions & 2 deletions watchapp/src/c/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ typedef enum {
UNKNOWN
} DeviceClass;

typedef enum {
CHARGING,
NOT_CHARGING,
BATTERY_UNKNOWN,
} BatteryStatus;

typedef struct {
uint8_t deviceId;
char deviceName[MAX_NAME_LENGTH];
DeviceClass deviceClass;
DeviceClass deviceClass;
uint16_t deviceStatus;
char batteryLevel[4];
BatteryStatus batteryStatus;
} DeviceSummary;

void add_device(uint8_t deviceId, char *deviceName, DeviceClass deviceClass);
void add_device(uint8_t deviceId, char *deviceName, DeviceClass deviceClass, uint16_t deviceStatus, char *batteryLevel, BatteryStatus BatteryStatus);

DeviceSummary* get_device_at_index(int index);

Expand Down
35 changes: 31 additions & 4 deletions watchapp/src/c/find-my.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ void find_my_inbox_received_handler(DictionaryIterator *iter) {
Tuple *id_tuple = dict_find(iter, MESSAGE_KEY_DeviceId);
Tuple *name_tuple = dict_find(iter, MESSAGE_KEY_DeviceName);
Tuple *class_tuple = dict_find(iter, MESSAGE_KEY_DeviceClass);
Tuple *dstatus_tuple = dict_find(iter, MESSAGE_KEY_DeviceStatus);
Tuple *blevel_tuple = dict_find(iter, MESSAGE_KEY_BatteryLevel);
Tuple *bstatus_tuple = dict_find(iter, MESSAGE_KEY_BatteryStatus);

if (id_tuple && name_tuple && class_tuple) {
if (id_tuple && name_tuple && class_tuple && dstatus_tuple && blevel_tuple && bstatus_tuple) {
LOG("Received device: %s", name_tuple->value->cstring);
add_device(id_tuple->value->uint8, name_tuple->value->cstring, class_tuple->value->uint8);
add_device(
id_tuple->value->uint8,
name_tuple->value->cstring,
class_tuple->value->uint8,
dstatus_tuple->value->uint16,
blevel_tuple->value->cstring,
bstatus_tuple->value->uint8
);
menu_layer_reload_data_and_mark_dirty(s_menu_layer);
}
break;
Expand Down Expand Up @@ -89,9 +99,26 @@ static void menu_draw_row_callback(GContext *ctx, const Layer *cell_layer, MenuI
if (device == NULL) {
return;
}
// TODO: Implement battery/status subtitle
// TODO: Implement icons
menu_cell_basic_draw(ctx, cell_layer, device->deviceName, NULL, NULL);
char statusString[30];
LOG("%d", device->deviceStatus);
switch (device->deviceStatus) {
case 200:
case 201:
strcpy(statusString, "Online - ");
break;
default:
strcpy(statusString, "Unknown - ");
break;
}

strcat(statusString, device->batteryLevel);

if (device->batteryStatus == CHARGING) {
strcat(statusString, " - Charging");
}

menu_cell_basic_draw(ctx, cell_layer, device->deviceName, statusString, NULL);

}

Expand Down
15 changes: 14 additions & 1 deletion watchapp/src/pkjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const clay = new Clay(clayConfig);
const COMMAND_KEY_LIST = 0;
const COMMAND_KEY_FIND = 1;

const BATTERY_CHARGING = 0;
const BATTERY_NOT_CHARGING = 1;
const BATTERY_UNKNOWN = 2;

// DeviceClass Enums
const DEVICE_CLASS_PHONE = 0;
Expand Down Expand Up @@ -108,6 +111,13 @@ function deviceClassToEnum(classString) {
return DEVICE_CLASS_UNKNOWN;
}

function batteryStatusToEnum(statusString) {
statusString = statusString.toLowerCase();
if (statusString === "charging") { return BATTERY_CHARGING; }
else if (statusString === "notcharging") { return BATTERY_NOT_CHARGING; }
else { return BATTERY_UNKNOWN; }
}

function deviceIdToCrc(deviceId) {
var crc8 = new CRC8();
var deviceIdBytes = [];
Expand Down Expand Up @@ -175,7 +185,10 @@ function updateDeviceList(callback) {
newDeviceSummaries.push({
"DeviceName": device.name,
"DeviceId": deviceCrc,
"DeviceClass": deviceClassToEnum(device.deviceClass)
"DeviceClass": deviceClassToEnum(device.deviceClass),
"DeviceStatus": parseInt(device.deviceStatus),
"BatteryLevel": String(parseInt(device.batteryLevel * 100)) + "%", // Convert float to a percent
"BatteryStatus": batteryStatusToEnum(device.batteryStatus)
});
}

Expand Down

0 comments on commit b8e4f55

Please sign in to comment.