app_template.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "app_template.h"
  2. /*
  3. To use this example you need to rename
  4. AppExampleState - class to hold app state
  5. AppExampleEvent - class to hold app event
  6. AppExample - app class
  7. app_cpp_example - function that launch app
  8. */
  9. // event enumeration type
  10. typedef uint8_t event_t;
  11. // app state class
  12. class AppExampleState {
  13. public:
  14. // state data
  15. uint8_t example_data;
  16. // state initializer
  17. AppExampleState() {
  18. example_data = 0;
  19. }
  20. };
  21. // app events class
  22. class AppExampleEvent {
  23. public:
  24. // events enum
  25. static const event_t EventTypeTick = 0;
  26. static const event_t EventTypeKey = 1;
  27. // payload
  28. union {
  29. InputEvent input;
  30. } value;
  31. // event type
  32. event_t type;
  33. };
  34. // our app derived from base AppTemplate class
  35. // with template variables <state, events>
  36. class AppExample : public AppTemplate<AppExampleState, AppExampleEvent> {
  37. public:
  38. uint8_t run();
  39. void render(Canvas* canvas);
  40. };
  41. // start app
  42. uint8_t AppExample::run() {
  43. // here we dont need to acquire or release state
  44. // because before we call app_ready our application is "single threaded"
  45. state.example_data = 12;
  46. // signal that we ready to render and ipc
  47. app_ready();
  48. // from here, any data that pass in render function must be guarded
  49. // by calling acquire_state and release_state
  50. AppExampleEvent event;
  51. while(1) {
  52. if(get_event(&event, 1000)) {
  53. if(event.type == AppExampleEvent::EventTypeKey) {
  54. // press events
  55. if(event.value.input.type == InputTypeShort &&
  56. event.value.input.key == InputKeyBack) {
  57. printf("bye!\n");
  58. return exit();
  59. }
  60. if(event.value.input.type == InputTypeShort &&
  61. event.value.input.key == InputKeyUp) {
  62. // to read or write state you need to execute
  63. // acquire modify release state
  64. acquire_state();
  65. state.example_data = 24;
  66. release_state();
  67. }
  68. }
  69. }
  70. // signal to force gui update
  71. update_gui();
  72. };
  73. }
  74. // render app
  75. void AppExample::render(Canvas* canvas) {
  76. // here you dont need to call acquire_state or release_state
  77. // to read or write app state, that already handled by caller
  78. canvas_set_color(canvas, ColorBlack);
  79. canvas_set_font(canvas, FontPrimary);
  80. canvas_draw_str(canvas, 2, state.example_data, "Example app");
  81. }
  82. // app enter function
  83. extern "C" uint8_t app_cpp_example(void* p) {
  84. AppExample* app = new AppExample();
  85. return app->run();
  86. }