
/*
 * dials.c
 *
 *  Created on: 22 Jan 2016
 *      Author: Mike
 */
#include "stm32f1xx_hal.h"
#include "ap_math.h"
#include "font.h"
#include "SSD1306.h"

static uint16_t  a1 = 120;
static uint8_t  xo=64;
static uint8_t  yo=64;
static uint8_t siz=32;



/* percent is integer from 0 to 100 */
void dial_draw_needle(uint16_t percent)
{
  int ang  = ((percent - 50)* a1) / 50 ;

  int si = ap_sin(ang);
  int co = ap_cos(ang);

  /* calculate a shift for a second side of the needle */
  int xs = ap_sin(ang-90);
  int ys = ap_cos(ang-90);

  int si2 = siz+2;
  drawLine(AP_SCALE(si*si2-xs)+xo,yo-AP_SCALE(co*si2-ys),
		   AP_SCALE(si*2-xs)+xo,yo-AP_SCALE(co*2-ys), INVERT);
  drawLine(AP_SCALE(si*si2+xs)+xo,yo-AP_SCALE(co*si2+ys),
		   AP_SCALE(si*2+xs)+xo,yo-AP_SCALE(co*2+ys), INVERT);

}



/* initialise */
void dial_size(uint8_t size)
{
	siz=size;
}

void dial_draw_scale(uint8_t low, uint8_t high,uint8_t width, uint8_t num_step)
{
	int sz;
	int ang;
	int step = a1*2 / (4 * (high-low));
	int t;
	ang = -a1;
	for(t=low*4; t <= high* 4 ; t++)
	{
		int si = ap_sin(ang);
		int co = ap_cos(ang);

		int len;
		switch(t % 4)
		{
		case 0:
		        {
				uint8_t v = t/4;
//				if((v % num_step) ==  0)
//				{
//				   char buff[2];
//				   char * p = buff+1;
//				   int d = 1;
//				   buff[1] = v %10 + '0';
//				   if(v>=10)
//				   {
//					   p = buff;
//					   d= 2;
//					   buff[0] = v/10 + '0';
//				   }
//
//				   print_large_string(p,AP_SCALE((siz-width/2-3)*si)+xo - d *4,
//						   yo - AP_SCALE((siz - width / 2 ) * co), d);
//
//				}
		        len=2;
		        }
		        break;
			case 1:
			case 3:
				len = width/4;
				break;
			case 2:
				len = width/2;
				break;
			}


			drawLine(AP_SCALE((siz)*si) + xo, yo - AP_SCALE((siz)*co),
					 AP_SCALE((siz-len)*si) +xo, yo - AP_SCALE((siz-len)*co), 1);
			ang += step;
		}


	}



void dial_origin(uint8_t x, uint8_t y)
{
	xo=x; yo=y;
}
