Skip to content

Commit aec12fd

Browse files
committed
Add pawn library and app
1 parent f2814dd commit aec12fd

File tree

8 files changed

+4837
-1
lines changed

8 files changed

+4837
-1
lines changed

src/CMakeLists.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ set(LVGL_SRC
355355
libs/lvgl/src/lv_widgets/lv_win.c
356356
)
357357

358+
list(APPEND PAWN_SRC
359+
pawn/amx.c
360+
)
361+
358362
list(APPEND IMAGE_FILES
359363
displayapp/icons/battery/batteryicon.c
360364
)
@@ -398,6 +402,7 @@ list(APPEND SOURCE_FILES
398402
displayapp/screens/Alarm.cpp
399403
displayapp/screens/Styles.cpp
400404
displayapp/screens/WeatherSymbols.cpp
405+
displayapp/screens/Pawn.cpp
401406
displayapp/Colors.cpp
402407
displayapp/widgets/Counter.cpp
403408
displayapp/widgets/PageIndicator.cpp
@@ -900,6 +905,19 @@ target_compile_options(lvgl PRIVATE
900905
$<$<COMPILE_LANGUAGE:ASM>: ${ASM_FLAGS}>
901906
)
902907

908+
# pawn
909+
add_library(pawn STATIC ${PAWN_SRC})
910+
target_include_directories(pawn SYSTEM PUBLIC . ../)
911+
target_include_directories(pawn SYSTEM PUBLIC ${INCLUDES_FROM_LIBS})
912+
target_compile_options(pawn PRIVATE
913+
${COMMON_FLAGS}
914+
-DAMX_ANSIONLY
915+
$<$<CONFIG:DEBUG>: ${DEBUG_FLAGS}>
916+
$<$<CONFIG:RELEASE>: ${RELEASE_FLAGS}>
917+
$<$<COMPILE_LANGUAGE:CXX>: ${CXX_FLAGS}>
918+
$<$<COMPILE_LANGUAGE:ASM>: ${ASM_FLAGS}>
919+
)
920+
903921
# LITTLEFS_SRC
904922
add_library(littlefs STATIC ${LITTLEFS_SRC})
905923
target_include_directories(littlefs SYSTEM PUBLIC . ../)
@@ -918,7 +936,7 @@ set(EXECUTABLE_FILE_NAME ${EXECUTABLE_NAME}-${pinetime_VERSION_MAJOR}.${pinetime
918936
set(NRF5_LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/gcc_nrf52.ld")
919937
add_executable(${EXECUTABLE_NAME} ${SOURCE_FILES})
920938
set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_FILE_NAME})
921-
target_link_libraries(${EXECUTABLE_NAME} nimble nrf-sdk lvgl littlefs infinitime_fonts infinitime_apps)
939+
target_link_libraries(${EXECUTABLE_NAME} nimble nrf-sdk lvgl pawn littlefs infinitime_fonts infinitime_apps)
922940
target_compile_options(${EXECUTABLE_NAME} PUBLIC
923941
${COMMON_FLAGS}
924942
${WARNING_FLAGS}

src/displayapp/UserApps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "displayapp/screens/Alarm.h"
66
#include "displayapp/screens/Dice.h"
7+
#include "displayapp/screens/Pawn.h"
78
#include "displayapp/screens/Timer.h"
89
#include "displayapp/screens/Twos.h"
910
#include "displayapp/screens/Tile.h"

src/displayapp/screens/Pawn.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include "Pawn.h"
2+
#include <stdio.h>
3+
4+
using namespace Pinetime::Applications::Screens;
5+
6+
#include "program.h"
7+
8+
static cell AMX_NATIVE_CALL F_lv_label_create(AMX*, const cell*) {
9+
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
10+
11+
return (cell) label;
12+
}
13+
14+
static cell AMX_NATIVE_CALL F_lv_obj_set_pos(AMX*, const cell* params) {
15+
lv_obj_t* label = (lv_obj_t*) params[1];
16+
lv_obj_set_pos(label, params[2], params[3]);
17+
18+
return 0;
19+
}
20+
21+
static cell AMX_NATIVE_CALL F_lv_label_set_text(AMX* amx, const cell* params) {
22+
lv_obj_t* label = (lv_obj_t*) params[1];
23+
24+
char* text;
25+
amx_StrParam_Type(amx, params[2], text, char*);
26+
if (text != NULL)
27+
lv_label_set_text(label, text);
28+
else
29+
lv_label_set_text(label, "<invalid>");
30+
31+
return 0;
32+
}
33+
34+
static cell AMX_NATIVE_CALL F_sprintf(AMX* amx, const cell* params) {
35+
// param[0] is the number of total parameter bytes, divide it by cell size and subtract 3 to account for the fixed parameters
36+
int args_count = params[0] / sizeof(cell) - 3;
37+
38+
cell *output = amx_Address(amx, params[1]);
39+
cell output_size = params[2];
40+
41+
char buf[output_size];
42+
43+
char *fmt;
44+
amx_StrParam_Type(amx, params[3], fmt, char*);
45+
if (fmt == NULL)
46+
return 0;
47+
48+
cell ret = 0;
49+
50+
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
51+
switch (args_count)
52+
{
53+
case 0:
54+
strcpy(buf, fmt);
55+
ret = strlen(fmt) + 1;
56+
break;
57+
case 1:
58+
ret = snprintf(buf, output_size, fmt, *amx_Address(amx, params[4]));
59+
break;
60+
case 2:
61+
ret = snprintf(buf, output_size, fmt, *amx_Address(amx, params[4]), *amx_Address(amx, params[5]));
62+
break;
63+
case 3:
64+
ret = snprintf(buf, output_size, fmt, *amx_Address(amx, params[4]), *amx_Address(amx, params[5]), *amx_Address(amx, params[6]));
65+
break;
66+
case 4:
67+
ret = snprintf(buf, output_size, fmt, *amx_Address(amx, params[4]), *amx_Address(amx, params[5]), *amx_Address(amx, params[6]), *amx_Address(amx, params[7]));
68+
break;
69+
case 5:
70+
ret = snprintf(buf, output_size, fmt, *amx_Address(amx, params[4]), *amx_Address(amx, params[5]), *amx_Address(amx, params[6]), *amx_Address(amx, params[7]), *amx_Address(amx, params[8]));
71+
break;
72+
default:
73+
return 0;
74+
}
75+
#pragma GCC diagnostic warning "-Wformat-nonliteral"
76+
77+
amx_SetString(output, buf, 1, 0, output_size);
78+
79+
return ret;
80+
}
81+
82+
Pawn::Pawn() {
83+
uint8_t* prog = new uint8_t[program_len];
84+
memcpy(prog, program, program_len);
85+
86+
memset(&amx, 0, sizeof(amx));
87+
amx_Init(&amx, prog);
88+
89+
amx.userdata[0] = this;
90+
91+
static AMX_NATIVE_INFO natives[] = {
92+
{"sprintf", F_sprintf},
93+
{"lv_label_create", F_lv_label_create},
94+
{"lv_obj_set_pos", F_lv_obj_set_pos},
95+
{"lv_label_set_text", F_lv_label_set_text},
96+
{0, 0} /* terminator */
97+
};
98+
amx_Register(&amx, natives, -1);
99+
100+
amx_Exec(&amx, NULL, AMX_EXEC_MAIN);
101+
102+
if (amx_FindPublic(&amx, "@refresh", &refresh_index) == AMX_ERR_NONE)
103+
{
104+
taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
105+
Refresh();
106+
}
107+
}
108+
109+
Pawn::~Pawn() {
110+
lv_task_del(taskRefresh);
111+
lv_obj_clean(lv_scr_act());
112+
113+
amx_Cleanup(&amx);
114+
delete amx.base;
115+
}
116+
117+
void Pawn::Refresh() {
118+
amx_Exec(&amx, NULL, refresh_index);
119+
}

src/displayapp/screens/Pawn.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
#include "displayapp/apps/Apps.h"
4+
#include "displayapp/screens/Screen.h"
5+
#include "displayapp/Controllers.h"
6+
7+
#include "pawn/amx.h"
8+
9+
namespace Pinetime {
10+
namespace Applications {
11+
namespace Screens {
12+
13+
class Pawn : public Screen {
14+
public:
15+
Pawn();
16+
~Pawn() override;
17+
18+
void Refresh() override;
19+
20+
private:
21+
AMX amx;
22+
int refresh_index;
23+
24+
lv_task_t* taskRefresh;
25+
};
26+
}
27+
28+
template <>
29+
struct AppTraits<Apps::Pawn> {
30+
static constexpr Apps app = Apps::Pawn;
31+
static constexpr const char* icon = "P";
32+
33+
static Screens::Screen* Create(AppControllers& /*controllers*/) {
34+
return new Screens::Pawn();
35+
};
36+
37+
static bool IsAvailable(Pinetime::Controllers::FS& /*filesystem*/) {
38+
return true;
39+
};
40+
};
41+
}
42+
}

src/displayapp/screens/program.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
unsigned char program[] = {
2+
0x84, 0x02, 0x00, 0x00, 0xe0, 0xf1, 0x0b, 0x0b, 0x00, 0x00, 0x08, 0x00,
3+
0xa8, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x84, 0x02, 0x00, 0x00,
4+
0x84, 0x42, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
5+
0x44, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00,
6+
0x64, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00,
7+
0x84, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8+
0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00,
9+
0x00, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
10+
0x96, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x40, 0x72, 0x65, 0x66, 0x72, 0x65,
11+
0x73, 0x68, 0x00, 0x6c, 0x76, 0x5f, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x5f,
12+
0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x00, 0x6c, 0x76, 0x5f, 0x6f, 0x62,
13+
0x6a, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x70, 0x6f, 0x73, 0x00, 0x73, 0x70,
14+
0x72, 0x69, 0x6e, 0x74, 0x66, 0x00, 0x6c, 0x76, 0x5f, 0x6c, 0x61, 0x62,
15+
0x65, 0x6c, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x74, 0x65, 0x78, 0x74, 0x00,
16+
0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
17+
0x49, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18+
0x16, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19+
0x1c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
20+
0x00, 0x00, 0x00, 0x00, 0x49, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
21+
0x14, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
22+
0x14, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
23+
0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
24+
0x0c, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00,
25+
0x01, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
26+
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
27+
0x1e, 0x00, 0x00, 0x00, 0x49, 0x00, 0x00, 0x00, 0x49, 0x00, 0x00, 0x00,
28+
0x09, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
29+
0x68, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00,
30+
0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00,
31+
0x18, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00,
32+
0x18, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00,
33+
0x16, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
34+
0x18, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
35+
0x16, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
36+
0x1c, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00,
37+
0xfc, 0xff, 0xff, 0xff, 0x49, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
38+
0x68, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00,
39+
0x3c, 0x00, 0x00, 0x00, 0x49, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
40+
0x04, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
41+
0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
42+
0x08, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00,
43+
0x03, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
44+
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
45+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54+
0x65, 0x6d, 0x69, 0x54, 0x61, 0x6c, 0x65, 0x20, 0x64, 0x65, 0x73, 0x70,
55+
0x6c, 0x25, 0x20, 0x3a, 0x00, 0x00, 0x00, 0x64
56+
};
57+
unsigned int program_len = 644;

0 commit comments

Comments
 (0)