playback_scene.c 3.1 KB

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