app-template.cpp 2.6 KB

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