#include "gfx_oled_example.h"
#include "math.h"
int menu_white_theme = 0;

//#define ROTATING_ARROW_EXAMPLE
#define SINE_EXAMPLE
//#define ECG_EXAMPLE
//#define PLAYER_EXAMPLE


#define ECG_SIZE 550


// Примерный массив, симулирующий ЭКГ-сигнал (нормализованный от 0 до 1)
float ecg_data[ECG_SIZE] = {1.05893, 0.999357, 0.933132, 0.744792, 0.664672, 0.328846, 0.136133, -0.00837916, -0.131494, 
  -0.1437, -0.182276, -0.215992, -0.128979, -0.131278, -0.0675223, 0.0027252, 0.0467067, -0.078859, -0.0658536, 0.0626352, 
  -0.0462435, 0.0476623, 0.0535328, 0.122938, 0.0501136, 0.034033, 0.0919253, 0.108374, 0.0667457, 0.0351678, -0.00115748, 
  0.0896459, 0.0288452, -0.0102636, 0.136738, 0.0397245, 0.0856079, 0.141222, 0.0826385, 0.165799, 0.105852, 0.172638, 
  0.186488, 0.144543, 0.0710717, 0.145054, 0.176092, 0.158212, 0.157025, 0.29588, 0.231833, 0.338024, 0.253539, 0.344064, 
  0.284527, 0.381661,0.385494, 0.324123, 0.305506, 0.433373, 0.477326, 0.364201,0.44309, 0.405536, 0.4922, 0.485606, 
  0.370194, 0.512566, 0.540046, 0.445303, 0.518052, 0.456006, 0.364917, 0.367834, 0.40163, 0.460996, 0.466174, 0.447784, 
  0.341301,0.371422, 0.268949, 0.259113, 0.249429, 0.343, 0.291218, 0.214958, 0.260874, 0.176372, 0.143037, 0.287679, 
  0.21553, 0.275687, 0.215986, 0.180708, 0.215402, 0.217034, 0.228714, 0.0215511,0.18583, 0.1273, 0.195534, 0.0965174, 
  0.0809704, 0.140033, 0.0206099, 0.0704855, 0.147377, 0.0789603, 0.130335, 0.106715, 0.074257, 0.00483191,0.0875146, 
  -0.0300366, 0.0777518, 0.084268, 0.0977437, 0.129822, 0.148143, 0.105601,0.0682866, 0.137946, 0.068663, -0.0435304, 
  -0.0222684, 0.12664, 0.0515385, 0.12427, -0.00217019, 0.0670832, 0.0832982, -0.0355677, 0.081701,0.031973, -0.0298655, 
  0.0588815, 0.000209182, -0.0129383, 0.0042788, -0.00678231,0.00246806, -0.0260747, 0.0931759, 0.0232501,0.075334, 0.107382, 
  0.0689022, 0.0769642, 0.0595603, -0.00397555, 0.0700185, 0.143251, 0.148137, 0.028107, 0.0164351, 0.0884327, 0.104194, 0.060235, 
  0.0187268, 0.167828, -0.00462437, 0.000835874, 0.00885075, 0.0145025, 0.106193, 0.0975606, -0.00591114, 0.17083, 0.131315, 0.134244, 
  0.000579408, 0.161378, 0.177776, 0.189649, 0.16676, 0.154687, 0.103331, 0.0398174, 0.0883113, 0.0404233, 0.0256448, 0.214035, 
  0.0942273, 0.101924, 0.119377, 0.157289, 0.205664, 0.0941939, 0.271559, 0.229571, 0.303699, 0.217788, 0.252682, 0.189268, 
  0.228094, 0.338367, 0.283619, 0.407, 0.258733, 0.438339, 0.351821, 0.385913, 0.442646, 0.296084, 0.314822, 0.315994, 0.364966, 
  0.36342, 0.206273, 0.207504, 0.228995, 0.153435, 0.231611, 0.179904, 0.133949, 0.127457, 0.254484, 0.197385, 0.146029, 0.225966, 
  0.0566748, 0.178446, 0.171133, 0.131769, 0.0525685, 0.132109, 0.069929, 0.0340608, 0.0468317, 0.180335, 0.0104617, 0.0354713, 
  -0.0163161, 0.0425572, -0.0653781, 0.0877674, -0.0742378, -0.110943, -0.0746231, -0.0359488, -0.073415, 0.168732, 0.13128, 
  0.249452, 0.351835, 0.563543, 0.679657, 0.92211, 1.08445, 1.1125, 0.993184, 0.916222, 0.925942, 0.786796, 0.53263, 0.270246, 
  0.25373, 0.0244354, -0.0842442, -0.0607408, -0.243356, -0.243292, -0.105114, -0.0890295, -0.0677865, 0.0111256, 0.0417756, 
  0.0859112, 0.00967447, 0.10676, 0.0906405, 0.0659345, 0.00378074, 0.150838, 0.0649321, 0.121398, 0.15057, 0.0259133, 0.0332993, 
  0.120676, 0.111174, 0.195936, 0.181734, 0.173334, 0.0417151, 0.0514996, 0.0606249, 0.209138, 0.245178, 0.201025, 0.0990306, 
  0.226141, 0.113311, 0.125047, 0.240658, 0.189883, 0.267101, 0.298959, 0.279095, 0.324262, 0.237427, 0.351991, 0.413902, 0.408103, 
  0.266948, 0.337957, 0.353324, 0.430994, 0.427623, 0.479357, 0.407738, 0.387289, 0.374333, 0.521238, 0.416679, 0.460645, 0.499469, 
  0.439634, 0.525509, 0.495788, 0.429484, 0.55411, 0.414761, 0.448777, 0.431282, 0.482458, 0.385804, 0.438642, 0.367588, 0.357191, 
  0.478922, 0.366946, 0.415339, 0.473029, 0.351773, 0.389086, 0.386449, 0.305993, 0.335247, 0.210934, 0.360828, 0.197025, 0.198693, 
  0.291518, 0.216643, 0.260826, 0.174855, 0.139573, 0.082479, 0.200454, 0.14318, 0.13979, 0.164275, 0.0477158, 0.0931762, 0.179358,
  0.159488, 0.0412108, 0.0386311, 0.0279973, 0.00843795, 0.0892224, 0.133798, 0.145674, 0.106026, 0.0205011, 0.124274, 0.0226069, 
  0.0237319, 0.0163099, 0.024851, 0.030066, 0.0357348, 0.0601346, 0.0601495, 0.0406621, 0.0476954, 0.176459, 0.138954, 0.109933, 
  0.0361806, 0.0422346, 0.0158523, 0.183712, 0.142873, 0.113671, 0.065383, 0.0365197, 0.128355, 0.202013, 0.0390733, 0.0570969, 
  0.0854399, 0.0214106, 0.143953, 0.0881267, 0.204725, 0.0891005, 0.133303, 0.04055, 0.0864498, 0.0429089, 0.162802, 0.185894, 
  0.0823151, 0.14975, 0.0719509, 0.119721, 0.180551, 0.134033, 0.0820637, 0.0753133, 0.106455, 0.100938, 0.0888068, 0.129034, 
  0.166379, 0.103271, 0.104842, 0.0445476, 0.0251081, 0.0789694, 0.0852342, 0.153383, 0.215103, 0.212132, 0.118084, 0.0764331,
  0.183322, 0.185061, 0.184503, 0.188901, 0.0658428, 0.186253, 0.148702, 0.105533, 0.0908859, 0.245061, 0.125606, 0.134644, 0.247456, 
  0.306363, 0.244781, 0.29657, 0.201664, 0.376652, 0.308954, 0.351074, 0.236012, 0.40292, 0.401634, 0.284931, 0.372803, 0.337598, 
  0.383977, 0.35428, 0.3549, 0.303027, 0.310696, 0.25438, 0.423706, 0.356807, 0.398502, 0.229798, 0.365983, 0.325231, 0.266461, 
  0.224066, 0.173261, 0.258585, 0.141305, 0.0968079, 0.226929, 0.198325, 0.198878, 0.177088, 0.126345, 0.115497, 0.196221, 
  0.092804, 0.18902, 0.18094, 0.190455, 0.11711, 0.137381, 0.192176, 0.0792287, -0.0132089, 0.128102, 0.0579884, -0.0206506,
  0.0874069, -0.0799489, 0.0058573, 0.015077, 0.0137692, 0.0355411, 0.113175, 0.321936, 0.427422, 0.70414, 0.801907, 1.04001,
  1.11783, 1.1273, 1.02789, 1.10223, 0.829465, 0.792606, 0.470991, 0.324366, 0.108762, 0.0881276, -0.102915, -0.195709, -0.0950335, 
  -0.178844, -0.103735, -0.131323, -0.0461341, -0.129231, 0.0830591, 0.0903014, 0.102478, 0.132797, 0.0580604, 0.111787, 0.108011,
 0.102112, 0.0534435, 0.0500927, 0.0925822, 0.0498444, 0.167477, 0.206324, 0.0179187, 0.12221, 0.0360253, 0.18297, 0.224765, 0.0451847,
 0.225041, 0.0467012, 0.186295, 0.213311, 0.171037, 0.249559, 0.261215, 0.216183, 0.128802};



void GFX_Draw_LoopIcon(uint8_t *Buffer_Frame, uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t pxColor);

GFXIconsTypeDef icons;
GFX_PlotterHandleTypeDef plotter; 

void Example_GFX_Init(void)
{
  Example_GFX_PlotterInit();
  Example_GFX_IconInit();
	oled_init();
}
 

void Example_OLED_GFX_Update(PlayerTypeDef *player)
{
  static uint32_t oled_prevtick = 0;
  uint32_t oled_period = 50;
  if(uwTick - oled_prevtick > oled_period)
  {
    oled_prevtick = uwTick;
    Example_GFX_CreateFrame(player);
    oled_refresh();
  }
}
uint8_t sine2plot;
float sine_cnt_step = 0.5;
float sine_cnt = 1;
float sine_scale = 255;
float ecg_step = 2.5;
float ecg_scale = 1.1;
void Example_GFX_CreateFrame(PlayerTypeDef *player)
{
#if defined(ROTATING_ARROW_EXAMPLE)
  GFX_Clean_Buffer_Frame(oled_buf);
  __GFX_Draw_Arrow(oled_buf, 64, 16, 5, sine_cnt, 1);
  sine_cnt+=ecg_step;
  
#elif defined(SINE_EXAMPLE)
  sine2plot = (sinf(sine_cnt)+1)*128;
  GFX_Plotter_uint8_t(oled_buf, &plotter, &sine2plot, 0, 0, sine_scale);
  sine_cnt += sine_cnt_step;
  
#elif defined(ECG_EXAMPLE) 
  GFX_Plotter_float(oled_buf, &plotter, ecg_data, ECG_SIZE, ecg_step, ecg_scale);
  
#elif defined(PLAYER_EXAMPLE)
	GFX_Clean_Buffer_Frame(oled_buf);
	
	// название песни
	GFX_Output_String(oled_buf, 0, 0, "Harry Potter Theme", 0, 0);
	
	// кнопка стоп/плей
//	GFX_Draw_Circle(oled_buf, circle_stopplay_x, circle_stopplay_y, circle_stopplay_r, 1);
	
	// иконка "плей"
	if(player->play)
	{ 
		GFX_Draw_Line(oled_buf, &icons.StopPlay.play_lines[0]);
		GFX_Draw_Line(oled_buf, &icons.StopPlay.play_lines[1]);
	}
	// иконка "стоп"
	else
  { 
		GFX_Draw_Triangle(oled_buf, &icons.StopPlay.stop);
	}
	if(player->pressed_start)
		GFX_Invertion_Area(oled_buf, icons.StopPlay.PressedArea.xPos_Start, icons.StopPlay.PressedArea.yPos_Start, icons.StopPlay.PressedArea.area_Width, icons.StopPlay.PressedArea.area_Height);
		
	
	// дорожка песни
	__GFX_Draw_Line(oled_buf, 0, GFX_BufferHeight-2, player->currenttime*GFX_BufferWidth, GFX_BufferHeight-2, 1);
	
	
	// перемотка вперед
  GFX_Draw_Triangle(oled_buf, &icons.Forward.trig);
  GFX_Draw_Line(oled_buf, &icons.Forward.line);
	if(player->pressed_forward)
		GFX_Invertion_Area(oled_buf, icons.Forward.PressedArea.xPos_Start, icons.Forward.PressedArea.yPos_Start, icons.Forward.PressedArea.area_Width, icons.Forward.PressedArea.area_Height);
		
		
	// перемотка назад
	GFX_Draw_Triangle(oled_buf, &icons.Backward.trig);
	GFX_Draw_Line(oled_buf, &icons.Backward.line);
	if(player->pressed_backward)
		GFX_Invertion_Area(oled_buf, icons.Backward.PressedArea.xPos_Start, icons.Backward.PressedArea.yPos_Start, icons.Backward.PressedArea.area_Width, icons.Backward.PressedArea.area_Height);
		
	// индикация скорости
	char buff_speed[5];
	sprintf(buff_speed, "x%.1f", player->speed);
	if((player->speed < 1.05) && player->speed > 0.95)
		GFX_Output_String(oled_buf, speed_x_cursore, speed_y_cursore, buff_speed, 0, 0);
	else
	{		
		GFX_Output_String(oled_buf, speed_x_cursore, speed_y_cursore, buff_speed, 0, 1);
	}
//	GFX_Draw_Line(oled_buf, speed_underline_x_left, speed_underline_y, speed_underline_x_rigth, speed_underline_y, 1);
	
	// иконка зацикливания
	GFX_Draw_LoopIcon(oled_buf, icons.Loop.xPos_Start, icons.Loop.yPos_Start, icons.Loop.icon_Width, icons.Loop.icon_Height, 1);
	if(player->loop)
		GFX_Invertion_Area(oled_buf, icons.Loop.PressedArea.xPos_Start, icons.Loop.PressedArea.yPos_Start, icons.Loop.PressedArea.area_Width, icons.Loop.PressedArea.area_Height);
		
#endif // PLAYER_EXAMPLE
  
  if(menu_white_theme)
  {
		GFX_Invertion_Display(oled_buf);
  }


}



/* Функция рисования значка "зациклить песню" */
/* Сгенерировано чат гпт */
void GFX_Draw_LoopIcon(uint8_t *Buffer_Frame, uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t pxColor)
{
  #define arr_size 2
  #define spot_width 2
    uint8_t r = (width < height ? width : height) / 4; // Радиус закруглений ограничен минимальным размером

    // Четыре дуги по углам
    __GFX_Draw_Arc(Buffer_Frame, x + width - r, y + r, r, 270, 360, pxColor);   // Верхний правый угол
    __GFX_Draw_Arc(Buffer_Frame, x + r, y + r, r, 180, 270, pxColor);           // Верхний левый угол
    __GFX_Draw_Arc(Buffer_Frame, x + r, y + height - r, r, 90, 180, pxColor);   // Нижний левый угол
    __GFX_Draw_Arc(Buffer_Frame, x + width - r, y + height - r, r, 0, 90, pxColor); // Нижний правый угол

    // Прямые линии между дугами
    __GFX_Draw_Line(Buffer_Frame, x + r, y, x + width - r-spot_width, y, pxColor);           // Верхняя горизонталь
    __GFX_Draw_Line(Buffer_Frame, x, y + r, x, y + height - r, pxColor);          // Левая вертикаль
    __GFX_Draw_Line(Buffer_Frame, x + r+spot_width, y + height, x + width - r, y + height, pxColor); // Нижняя горизонталь
    __GFX_Draw_Line(Buffer_Frame, x + width, y + r, x + width, y + height - r, pxColor); // Правая вертикаль

    // Стрелки
    __GFX_Draw_Line(Buffer_Frame, x + width - r-spot_width, y, x + width - r - arr_size-spot_width, y - arr_size, pxColor); // Верхняя стрелка
    __GFX_Draw_Line(Buffer_Frame, x + width - r-spot_width, y, x + width - r - arr_size-spot_width, y + arr_size, pxColor);

    __GFX_Draw_Line(Buffer_Frame, x + r+spot_width, y + height, x + r + arr_size+spot_width, y + height - arr_size, pxColor); // Нижняя стрелка
    __GFX_Draw_Line(Buffer_Frame, x + r+spot_width, y + height, x + r + arr_size+spot_width, y + height + arr_size, pxColor);
}



void Example_GFX_PlotterInit(void)
{ 
#if defined(SINE_EXAMPLE)  
  plotter.xPos = 0;
  plotter.yPos = 0;
  plotter.plotWidth = 128;
  plotter.plotHeight = 30; 
  plotter.plotYShift = 64;
  plotter.f.plotXAxis = 1;
  plotter.f.plotYAxis = 1;  
#elif defined(ECG_EXAMPLE)   
  plotter.xPos = 2;
  plotter.yPos = 0;
  plotter.plotWidth = 128-plotter.xPos;
  plotter.plotHeight = 32; 
  plotter.f.plotXAxis = 1;
  plotter.f.plotYAxis = 1;   
  plotter.f.plotFrame = 1;
#endif
}
void Example_GFX_IconInit(void)
{
  // play
  icons.StopPlay.stop.xPos1 = play_icon_x_left-1;
  icons.StopPlay.stop.yPos1 = play_icon_y_up;
  icons.StopPlay.stop.xPos2 = play_icon_x_left-1;
  icons.StopPlay.stop.yPos2 = play_icon_y_down;
  icons.StopPlay.stop.xPos3 = play_icon_x_rigth+1;
  icons.StopPlay.stop.yPos3 = play_icon_y_mid;
  icons.StopPlay.stop.pxColor = 1;
  
  // stop
  icons.StopPlay.play_lines[0].xPos_Start = play_icon_x_left+1;
  icons.StopPlay.play_lines[0].yPos_Start = play_icon_y_up;
  icons.StopPlay.play_lines[0].xPos_End = play_icon_x_left+1;
  icons.StopPlay.play_lines[0].yPos_End = play_icon_y_down;
  icons.StopPlay.play_lines[0].pxColor = 1;  
  icons.StopPlay.play_lines[1].xPos_Start = play_icon_x_rigth;
  icons.StopPlay.play_lines[1].yPos_Start = play_icon_y_up;
  icons.StopPlay.play_lines[1].xPos_End = play_icon_x_rigth;
  icons.StopPlay.play_lines[1].yPos_End = play_icon_y_down;
  icons.StopPlay.play_lines[1].pxColor = 1;
  
  // stop/play pressed
  icons.StopPlay.PressedArea.selected_Width = selected_width;
  icons.StopPlay.PressedArea.xPos_Start = play_icon_x_left-1-selected_width;
  icons.StopPlay.PressedArea.yPos_Start = play_icon_y_up-selected_width;
  icons.StopPlay.PressedArea.area_Width = play_icon_x_rigth+2-(play_icon_x_left-1)+2*selected_width+1;
  icons.StopPlay.PressedArea.area_Height = play_icon_y_down-play_icon_y_up+2*selected_width+1;
  
  // forward
  icons.Forward.line.xPos_Start = forward_icon_x_rigth;
  icons.Forward.line.yPos_Start = forward_icon_y_up;
  icons.Forward.line.xPos_End = forward_icon_x_rigth;
  icons.Forward.line.yPos_End = forward_icon_y_down;
  icons.Forward.line.pxColor = 1;
  icons.Forward.trig.xPos1 = forward_icon_x_left;
  icons.Forward.trig.yPos1 = forward_icon_y_up;
  icons.Forward.trig.xPos2 = forward_icon_x_left;
  icons.Forward.trig.yPos2 = forward_icon_y_down;
  icons.Forward.trig.xPos3 = forward_icon_x_rigth-1;
  icons.Forward.trig.yPos3 = forward_icon_y_mid;
  icons.Forward.trig.pxColor = 1;
  
  // forward pressed
  icons.Forward.PressedArea.selected_Width = selected_width;
  icons.Forward.PressedArea.xPos_Start = forward_icon_x_left-selected_width;
  icons.Forward.PressedArea.yPos_Start = forward_icon_y_up-selected_width;
  icons.Forward.PressedArea.area_Width = forward_icon_x_rigth-(forward_icon_x_left)+2*selected_width+1;
  icons.Forward.PressedArea.area_Height = forward_icon_y_down-forward_icon_y_up+2*selected_width+1;
  
  // backward
  icons.Backward.line.xPos_Start = backward_icon_x_left;
  icons.Backward.line.yPos_Start = backward_icon_y_up;
  icons.Backward.line.xPos_End = backward_icon_x_left;
  icons.Backward.line.yPos_End = backward_icon_y_down;
  icons.Backward.line.pxColor = 1;
  icons.Backward.trig.xPos1 = backward_icon_x_rigth;
  icons.Backward.trig.yPos1 = backward_icon_y_up;
  icons.Backward.trig.xPos2 = backward_icon_x_rigth;
  icons.Backward.trig.yPos2 = backward_icon_y_down;
  icons.Backward.trig.xPos3 = backward_icon_x_left+1;
  icons.Backward.trig.yPos3 = backward_icon_y_mid;
  icons.Backward.trig.pxColor = 1;
  
  // backward pressed
  icons.Backward.PressedArea.xPos_Start = backward_icon_x_left-selected_width;
  icons.Backward.PressedArea.yPos_Start = backward_icon_y_up-selected_width;
  icons.Backward.PressedArea.area_Width = backward_icon_x_rigth-(backward_icon_x_left)+2*selected_width+1;
  icons.Backward.PressedArea.area_Height = backward_icon_y_down-backward_icon_y_up+2*selected_width+1;
    
  // loop
  icons.Loop.xPos_Start = loop_icon_x_start;
  icons.Loop.yPos_Start = loop_icon_y_start;
  icons.Loop.icon_Width = loop_icon_width;
  icons.Loop.icon_Height = loop_icon_height;
  
  // loop pressed
  icons.Loop.PressedArea.xPos_Start = loop_icon_x_start+3;
  icons.Loop.PressedArea.yPos_Start = loop_icon_y_start+3;
  icons.Loop.PressedArea.area_Width = loop_icon_width-5;
  icons.Loop.PressedArea.area_Height = loop_icon_height-5;
// иконка зацикливания
	GFX_Draw_LoopIcon(oled_buf, loop_icon_x_start, loop_icon_y_start, loop_icon_width, loop_icon_height, 1);
  
}
