Skip to content

Commit 56fa587

Browse files
committed
Rewrite digital watchface in pawn and add pawn scripts
1 parent fbbe598 commit 56fa587

8 files changed

Lines changed: 708 additions & 85 deletions

File tree

src/displayapp/screens/Pawn.cpp

Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
#include <stdio.h>
33
#include <charconv>
44

5+
#include "components/heartrate/HeartRateController.h"
6+
57
using namespace Pinetime::Applications::Screens;
68

79
enum {
810
PAWN_ERR_PARAMCOUNT = 100,
911
PAWN_ERR_MISSINGHANDLER,
1012
PAWN_ERR_INVALIDSTRING,
13+
PAWN_ERR_INVALIDSETTING,
1114

1215
PAWN_ERR_FIRST = PAWN_ERR_PARAMCOUNT,
1316
};
@@ -24,6 +27,15 @@ enum {
2427

2528
constexpr int max_overlay_size = 512;
2629

30+
static void label_set_text(AMX* amx, lv_obj_t* label, cell str) {
31+
char* text;
32+
amx_StrParam_Type(amx, str, text, char*);
33+
if (text != NULL)
34+
lv_label_set_text(label, text);
35+
else
36+
lv_label_set_text_static(label, "");
37+
}
38+
2739
static void event_handler(lv_obj_t* obj, lv_event_t event) {
2840
if (event == LV_EVENT_DELETE)
2941
return;
@@ -45,9 +57,12 @@ static cell AMX_NATIVE_CALL F_lv_scr_act(AMX* amx, const cell* params) {
4557
}
4658

4759
static cell AMX_NATIVE_CALL F_lv_label_create(AMX* amx, const cell* params) {
48-
ASSERT_PARAMS(2);
60+
ASSERT_PARAMS(3);
61+
62+
lv_obj_t* label = lv_label_create(PARAMS_OBJ(1) ?: lv_scr_act(), PARAMS_OBJ(2));
63+
label_set_text(amx, label, params[3]);
4964

50-
return (cell) lv_label_create(PARAMS_OBJ(1) ?: lv_scr_act(), PARAMS_OBJ(2));
65+
return (cell) label;
5166
}
5267

5368
static cell AMX_NATIVE_CALL F_lv_btn_create(AMX* amx, const cell* params) {
@@ -93,15 +108,7 @@ static cell AMX_NATIVE_CALL F_lv_obj_set_event_cb(AMX* amx, const cell* params)
93108
static cell AMX_NATIVE_CALL F_lv_label_set_text(AMX* amx, const cell* params) {
94109
ASSERT_PARAMS(2);
95110

96-
lv_obj_t* label = PARAMS_OBJ(1);
97-
98-
char* text;
99-
amx_StrParam_Type(amx, params[2], text, char*);
100-
if (text != NULL)
101-
lv_label_set_text(label, text);
102-
else
103-
amx_RaiseError(amx, PAWN_ERR_INVALIDSTRING);
104-
111+
label_set_text(amx, PARAMS_OBJ(1), params[2]);
105112
return 0;
106113
}
107114

@@ -282,26 +289,29 @@ static cell AMX_NATIVE_CALL F_sprintf(AMX* amx, const cell* params) {
282289
return bufc - buf;
283290
}
284291

285-
cell AMX_NATIVE_CALL F_get_datetime(AMX* amx, const cell* params) {
292+
cell AMX_NATIVE_CALL F_read_datetime(AMX* amx, const cell* params) {
286293
ASSERT_PARAMS(1);
287294

288295
Pawn* pawn = PAWN_INST;
289296

290297
pawn->currentDateTime = std::chrono::time_point_cast<std::chrono::minutes>(pawn->controllers.dateTimeController.CurrentDateTime());
291298

292299
cell* ret = amx_Address(amx, params[1]);
300+
cell data[] = {
301+
pawn->controllers.dateTimeController.Minutes(),
302+
pawn->controllers.dateTimeController.Hours(),
303+
pawn->controllers.dateTimeController.Day(),
304+
pawn->controllers.dateTimeController.Year(),
305+
};
293306

294-
ret[0] = pawn->currentDateTime.IsUpdated();
295-
ret[1] = pawn->controllers.dateTimeController.Seconds();
296-
ret[2] = pawn->controllers.dateTimeController.Minutes();
297-
ret[3] = pawn->controllers.dateTimeController.Hours();
298-
ret[4] = pawn->controllers.dateTimeController.Day();
299-
ret[5] = pawn->controllers.dateTimeController.Year();
300-
301-
return 0;
307+
if (memcmp(ret, data, sizeof(data)) == 0) {
308+
return 0;
309+
}
310+
memcpy(ret, data, sizeof(data));
311+
return 1;
302312
}
303313

304-
static cell AMX_NATIVE_CALL F_get_datetime_short_str(AMX* amx, const cell* params) {
314+
static cell AMX_NATIVE_CALL F_read_datetime_short_str(AMX* amx, const cell* params) {
305315
ASSERT_PARAMS(2);
306316

307317
Pawn* pawn = PAWN_INST;
@@ -343,6 +353,46 @@ static cell AMX_NATIVE_CALL F_status_icons_update(AMX* amx, const cell*) {
343353
return 0;
344354
}
345355

356+
static cell AMX_NATIVE_CALL F_has_new_notifications(AMX* amx, const cell*) {
357+
return PAWN_INST->controllers.notificationManager.AreNewNotificationsAvailable();
358+
}
359+
360+
static cell AMX_NATIVE_CALL F_get_setting(AMX* amx, const cell* params) {
361+
ASSERT_PARAMS(1);
362+
363+
#define SETTING(n, m) \
364+
case n: \
365+
return (cell) PAWN_INST->controllers.settingsController.m();
366+
367+
switch (params[1]) {
368+
SETTING(0, GetWatchFace)
369+
SETTING(1, GetChimeOption)
370+
SETTING(2, GetPrideFlag)
371+
SETTING(3, GetClockType)
372+
SETTING(4, GetWeatherFormat)
373+
SETTING(5, GetNotificationStatus)
374+
SETTING(6, GetStepsGoal)
375+
376+
default:
377+
amx_RaiseError(amx, PAWN_ERR_INVALIDSETTING);
378+
return 0;
379+
}
380+
381+
#undef SETTING
382+
}
383+
384+
static cell AMX_NATIVE_CALL F_get_heartrate(AMX* amx, const cell*) {
385+
return PAWN_INST->controllers.heartRateController.HeartRate();
386+
}
387+
388+
static cell AMX_NATIVE_CALL F_get_heartrate_state(AMX* amx, const cell*) {
389+
return (cell) PAWN_INST->controllers.heartRateController.State();
390+
}
391+
392+
static cell AMX_NATIVE_CALL F_get_step_number(AMX* amx, const cell*) {
393+
return PAWN_INST->controllers.motionController.NbSteps();
394+
}
395+
346396
static cell AMX_NATIVE_CALL F_raise_error(AMX* amx, const cell* params) {
347397
ASSERT_PARAMS(1);
348398

@@ -448,19 +498,25 @@ extern "C" const AMX_NATIVE pawn_natives[] = {
448498
F_lv_obj_set_style_local_color,
449499
F_lv_obj_set_style_local_opa,
450500
F_lv_obj_set_style_local_ptr,
451-
F_get_datetime,
452-
F_get_datetime_short_str,
501+
F_read_datetime,
502+
F_read_datetime_short_str,
453503
F_status_icons_create,
454504
F_status_icons_update,
505+
F_has_new_notifications,
506+
F_get_setting,
507+
F_get_heartrate,
508+
F_get_heartrate_state,
509+
F_get_step_number,
455510
F_raise_error,
456511
};
457512

458513
#include "program.h"
514+
459515
Pawn::Pawn(AppControllers& controllers) : Pawn(program, controllers) {
460516
(void) program_len;
461517
}
462518

463-
Pawn::Pawn(const uint8_t *file, AppControllers& controllers) : controllers(controllers), file(file) {
519+
Pawn::Pawn(const uint8_t* file, AppControllers& controllers) : controllers(controllers), file(file) {
464520
int result = LoadProgram();
465521
if (result != AMX_ERR_NONE) {
466522
ShowError(result);
@@ -532,8 +588,10 @@ void Pawn::ShowError(unsigned int amx_err) {
532588
"INDEX", "DEBUG", "INIT", "USERDATA", "INIT_JIT", "PARAMS", "DOMAIN", "GENERAL", "OVERLAY",
533589
};
534590
static const char* pawn_err_msgs[] = {
535-
"invalid parameter count", // PAWN_ERR_PARAMCOUNT
536-
"missing event handler", // PAWN_ERR_MISSINGHANDLER
591+
"parameter count mismatch", // PAWN_ERR_PARAMCOUNT
592+
"missing event handler", // PAWN_ERR_MISSINGHANDLER
593+
"invalid string", // PAWN_ERR_INVALIDSTRING
594+
"invalid setting", // PAWN_ERR_INVALIDSETTING
537595
};
538596

539597
if (amx_err == AMX_ERR_EXIT) {
@@ -583,7 +641,7 @@ bool Pawn::OnTouchEvent(TouchEvents event) {
583641

584642
cell ret;
585643

586-
amx_Push(&amx, (cell)event);
644+
amx_Push(&amx, (cell) event);
587645

588646
int result = amx_Exec(&amx, &ret, gesture_index);
589647
if (result != AMX_ERR_NONE) {

src/displayapp/screens/program.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
const unsigned char program[] = {
2-
0xc8, 0x00, 0x00, 0x00, 0xe0, 0xf1, 0x0b, 0x0b, 0x04, 0x00, 0x08, 0x00,
3-
0x40, 0x00, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00,
4-
0x90, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
2+
0xb0, 0x00, 0x00, 0x00, 0xe0, 0xf1, 0x0b, 0x0b, 0x04, 0x00, 0x08, 0x00,
3+
0x40, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00, 0xb0, 0x00, 0x00, 0x00,
4+
0x78, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
55
0x3c, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
66
0x3c, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
77
0x1f, 0x00, 0x00, 0x00, 0xad, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
8-
0x8e, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
9-
0xfd, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x87, 0x00, 0x00, 0x00,
10-
0x8e, 0x00, 0x14, 0x00, 0x8e, 0x00, 0x14, 0x00, 0x8f, 0x00, 0x00, 0x00,
11-
0x70, 0x00, 0x00, 0x00, 0xfb, 0xff, 0xff, 0xff, 0x0c, 0x00, 0x00, 0x00,
12-
0x92, 0x00, 0x04, 0x00, 0x8f, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
13-
0xf6, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00,
14-
0x8e, 0x00, 0x00, 0x00, 0x95, 0x00, 0x02, 0x00, 0x00, 0x00, 0xff, 0x00,
15-
0x89, 0x80, 0x00, 0x00, 0x8f, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
16-
0xf4, 0xff, 0xff, 0xff, 0x14, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00,
17-
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x69, 0x6c, 0x43,
18-
0x20, 0x3a, 0x73, 0x6b, 0x00, 0x00, 0x00, 0x30
8+
0x9c, 0x00, 0xfc, 0xff, 0x92, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00,
9+
0x8e, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xfd, 0xff, 0xff, 0xff,
10+
0x0c, 0x00, 0x00, 0x00, 0x88, 0x00, 0xfc, 0xff, 0x8e, 0x00, 0x14, 0x00,
11+
0x8e, 0x00, 0x14, 0x00, 0x90, 0x00, 0xfc, 0xff, 0x70, 0x00, 0x00, 0x00,
12+
0xfb, 0xff, 0xff, 0xff, 0x0c, 0x00, 0x00, 0x00, 0x92, 0x00, 0x04, 0x00,
13+
0x90, 0x00, 0xfc, 0xff, 0x70, 0x00, 0x00, 0x00, 0xf6, 0xff, 0xff, 0xff,
14+
0x08, 0x00, 0x00, 0x00, 0x9c, 0x00, 0x04, 0x00, 0x66, 0x00, 0x00, 0x00,
15+
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x6c, 0x65, 0x48,
16+
0x6f, 0x77, 0x20, 0x6f, 0x00, 0x64, 0x6c, 0x72
1917
};
20-
unsigned int program_len = 200;
18+
unsigned int program_len = 176;

0 commit comments

Comments
 (0)