text: ﻿/***************************************************************************************************
     Name: OLED.c
     Sphere OS: 19.05
  ****************************************************************************************************/
  
  #include "oled.h"
  #include <math.h>
  
  uint8_t oled_state = 0;
  
  /**
    * @brief  OLED initialization.
    * @param  None.
    * @retval Positive if was unsuccefully, zero if was succefully.
    */
  uint8_t oled_init()
  {
  	return sd1306_init();
  }
  
  // State machine to change the OLED status
  void update_oled()
  {
  	switch (oled_state)
  	{
  	case 0:
  	{
  		oled_i2c_bus_status(3);
  	}
  	break;
  	case 1:
  	{
  		oled_draw_logo();
  	}
  	break;
  
  	default:
  		break;
  	}
  }
  
  /**
    * @brief  Template to show a logo
    * @param  None.
    * @retval None.
    */
  void oled_draw_logo(void)
  {
  	// Copy image_avnet to OLED buffer
  	sd1306_draw_img(Image_avnet_bmp);
  
  	// Send the buffer to OLED RAM
  	sd1306_refresh();
  }
  
  // reverses a string 'str' of length 'len' 
  static void reverse(uint8_t* str, int32_t len)
  {
  	int32_t i = 0;
  	int32_t j = len - 1;
  	int32_t temp;
  
  	while (i < j)
  	{
  		temp = str[i];
  		str[i] = str[j];
  		str[j] = temp;
  		i++; j--;
  	}
  }
  
  /**
    * @brief  Converts a given integer x to string uint8_t[]
    * @param  x: x integer input
    * @param  str: uint8_t array output
    * @param  d: Number of zeros added
    * @retval i: number of digits
    */
  int32_t intToStr(int32_t x, uint8_t str[], int32_t d)
  {
  	int32_t i = 0;
  	uint8_t flag_neg = 0;
  
  	if (x < 0)
  	{
  		flag_neg = 1;
  		x *= -1;
  	}
  	while (x)
  	{
  		str[i++] = (x % 10) + '0';
  		x = x / 10;
  	}
  
  	// If number of digits required is more, then 
  	// add 0s at the beginning 
  	while (i < d)
  	{
  		str[i++] = '0';
  	}
  
  	if (flag_neg)
  	{
  		str[i] = '-';
  		i++;
  	}
  
  	reverse(str, i);
  	str[i] = '\0';
  	return i;
  }
  
  /**
    * @brief  Converts a given integer x to string uint8_t[]
    * @param  n: float number to convert
    * @param  res:
    * @param  afterpoint:
    * @retval None.
    */
  void ftoa(float n, uint8_t* res, int32_t afterpoint)
  {
  	// Extract integer part 
  	int32_t ipart = (int32_t)n;
  
  	// Extract floating part 
  	float fpart = n - (float)ipart;
  
  	int32_t i;
  
  	if (ipart < 0)
  	{
  		res[0] = '-';
  		res++;
  		ipart *= -1;
  	}
  
  	if (fpart < 0)
  	{
  		fpart *= -1;
  
  		if (ipart == 0)
  		{
  			res[0] = '-';
  			res++;
  		}
  	}
  
  	// convert integer part to string 
  	i = intToStr(ipart, res, 1);
  
  	// check for display option after point 
  	if (afterpoint != 0)
  	{
  		res[i] = '.';  // add dot 
  
  		// Get the value of fraction part upto given no. 
  		// of points after dot. The third parameter is needed 
  		// to handle cases like 233.007 
  		fpart = fpart * pow(10, afterpoint);
  
  		intToStr((int32_t)fpart, res + i + 1, afterpoint);
  	}
  }
  
  // AVNET logo
  
  const unsigned char Image_avnet_bmp[BUFFER_SIZE] =
  {
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,128,240,240,240,240, 48,  0,  0,112,
    240,240,240,224,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,112,
    240,240,240,192,  0,  0,  0,  0,  0,  0,  0,  0,  0,224,240,240,
    240, 16,  0,  0,  0,  0,  0,  0,  0,  0,240,240,240,240,224,128,
  	0,  0,  0,  0,  0,  0,  0,  0,240,240,240,240,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,240,240,240,240,112,112,112,112,112,112,
    112,112,112,112,112,  0,  0,  0,  0,  0,  0,  0,  0,112,112,112,
    112,112,112,112,240,240,240,240,112,112,112,112,112,112,  0,  0,
  	0,  0,  0,  0,  0,224,252,255,255,255, 15,  1,  0,  0,  0,  0,
  	3, 15,127,255,255,248,128,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	7, 31,255,255,254,240,  0,  0,  0,  0,224,248,255,255,127,  7,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,255,255,255,255, 15, 31,
    127,252,248,224,224,128,  0,  0,255,255,255,255,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,255,255,255,255,224,224,224,224,224,224,
    224,224,224,224,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,255,255,255,255,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,128,240,254,255,127, 15,  1,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  3, 31,255,255,252,224,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  7, 63,255,255,248,240,254,255,255, 31,  3,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,255,255,255,255,  0,  0,
  	0,  1,  3, 15, 15, 63,126,252,255,255,255,255,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,255,255,255,255,129,129,129,129,129,129,
    129,129,129,129,128,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,255,255,255,255,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  7,  7,  7,  3,  0,  0,  0, 12, 14, 14, 14, 14, 14, 14,
     14, 14, 12,  0,  0,  0,  7,  7,  7,  7,  4,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  1,  7,  7,  7,  7,  1,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  7,  7,  7,  7,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  1,  3,  7,  7,  7,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
  	7,  7,  7,  7,  7,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  7,  7,  7,  7,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  	0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
  };
  
  
  uint8_t get_str_size(uint8_t* str)
  {
  	uint8_t legth = 0;
  	while (*(str) != NULL)
  	{
  		str++;
  		legth++;
  	}
  	return legth;
  }