app-template.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "flipper.h"
  2. #include "flipper_v2.h"
  3. #include "app-template.h"
  4. /*
  5. To use this example you need to rename
  6. AppExampleState - class to hold app state
  7. AppExampleEvent - class to hold app event
  8. AppExample - app class
  9. app_cpp_example - function that launch app
  10. */
  11. // event enumeration type
  12. typedef uint8_t event_t;
  13. // app state class
  14. class AppExampleState {
  15. public:
  16. // state data
  17. uint8_t example_data;
  18. // state initializer
  19. AppExampleState() {
  20. example_data = 0;
  21. }
  22. };
  23. // app events class
  24. class AppExampleEvent {
  25. public:
  26. // events enum
  27. static const event_t EventTypeTick = 0;
  28. static const event_t EventTypeKey = 1;
  29. // payload
  30. union {
  31. InputEvent input;
  32. } value;
  33. // event type
  34. event_t type;
  35. };
  36. // our app derived from base AppTemplate class
  37. // with template variables <state, events>
  38. class AppExample : public AppTemplate<AppExampleState, AppExampleEvent> {
  39. public:
  40. void run();
  41. void render(Canvas* canvas);
  42. };
  43. // start app
  44. void AppExample::run() {
  45. // here we dont need to acquire or release state
  46. // because before we call app_ready our application is "single threaded"
  47. state.example_data = 12;
  48. // signal that we ready to render and ipc
  49. app_ready();
  50. // from here, any data that pass in render function must be guarded
  51. // by calling acquire_state and release_state
  52. AppExampleEvent event;
  53. while(1) {
  54. if(get_event(&event, 1000)) {
  55. if(event.type == AppExampleEvent::EventTypeKey) {
  56. // press events
  57. if(event.value.input.state && event.value.input.input == InputBack) {
  58. printf("bye!\n");
  59. exit();
  60. }
  61. if(event.value.input.state && event.value.input.input == InputUp) {
  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" void app_cpp_example(void* p) {
  84. AppExample* app = new AppExample();
  85. app->run();
  86. }