playback_scene.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "playback_scene.h"
  2. #include "../app_context.h"
  3. #include "../tone_gen.h"
  4. #define SINE_WAVE(x, toneModelData) \
  5. (sin((x + toneDataModel->animationOffset) * 50) * 20 + (64 / 2))
  6. #define SQUARE_WAVE(x, toneModelData) \
  7. ((sin((x + toneDataModel->animationOffset) * 50) > 0 ? 1 : -1) * 20 + (64 / 2))
  8. // Renders the waveform
  9. static void playback_view_draw_callback(Canvas* canvas, void* model) {
  10. UNUSED(model);
  11. struct ToneData_t* toneDataModel = (struct ToneData_t*)model;
  12. for(int x = 1; x < 128; x++) {
  13. int x1 = x - 1;
  14. int x2 = x;
  15. int y1 = 0;
  16. int y2 = 0;
  17. switch(toneDataModel->waveType) {
  18. case SINE:
  19. y1 = SINE_WAVE(x1, toneDataModel);
  20. y2 = SINE_WAVE(x2, toneDataModel);
  21. break;
  22. case SQUARE:
  23. y1 = SQUARE_WAVE(x1, toneDataModel);
  24. y2 = SQUARE_WAVE(x2, toneDataModel);
  25. break;
  26. default:
  27. y1 = 64 / 2;
  28. y2 = 64 / 2;
  29. break;
  30. }
  31. // Draw lines to connect the pieces of the wave.
  32. canvas_draw_line(canvas, x1, y1, x2, y2);
  33. }
  34. if(toneDataModel->animationOffset < 128) {
  35. toneDataModel->animationOffset += 2;
  36. } else {
  37. toneDataModel->animationOffset = 0;
  38. }
  39. }
  40. // Sets up the waveform to be displayed
  41. void scene_on_enter_playback_scene(void* context) {
  42. FURI_LOG_I(TAG, "scene_on_enter_playback_scene");
  43. struct AppContext_t* app = (struct AppContext_t*)context;
  44. struct View_t* playbackView = app->activeViews[ToneGenAppView_PlaybackView];
  45. // Configure the custom view
  46. view_set_draw_callback(playbackView->viewData, playback_view_draw_callback);
  47. view_set_context(playbackView->viewData, context);
  48. FURI_LOG_I(TAG, "setting view model");
  49. struct ToneData_t* toneDataModel = (struct ToneData_t*)view_get_model(playbackView->viewData);
  50. toneDataModel->waveType = ((struct ToneData_t*)app->additionalData)->waveType;
  51. toneDataModel->frequency = ((struct ToneData_t*)app->additionalData)->frequency;
  52. // Set the currently active view
  53. FURI_LOG_I(TAG, "setting active view");
  54. view_dispatcher_switch_to_view(app->view_dispatcher, ToneGenAppView_PlaybackView);
  55. }
  56. // Not actively used in this instance.
  57. bool scene_on_event_playback_scene(void* context, SceneManagerEvent event) {
  58. FURI_LOG_I(TAG, "scene_on_event_playback_scene");
  59. UNUSED(context);
  60. UNUSED(event);
  61. return false;
  62. }
  63. // Not actively used in this instance.
  64. void scene_on_exit_playback_scene(void* context) {
  65. FURI_LOG_I(TAG, "scene_on_exit_playback_scene");
  66. UNUSED(context);
  67. }