Subversion Repositories DashDisplay

Rev

Rev 47 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/*
2
 * dials.c
3
 *
4
 *  Created on: 22 Jan 2016
5
 *      Author: Mike
6
 */
30 mjames 7
#include "stm32l1xx_hal.h"
2 mjames 8
#include "ap_math.h"
9
#include "font.h"
10
#include "SSD1306.h"
47 mjames 11
#include "dials.h"
2 mjames 12
 
47 mjames 13
// this is the number of degrees between top centre of scale and
14
// left or right hand end of scale : 90 degrees produces a semicircle.
6 mjames 15
static uint16_t a1 = 90;
16
static uint8_t xo = 64;
17
static uint8_t yo = 64;
18
static uint8_t siz = 32;
2 mjames 19
 
47 mjames 20
/* position is integer from 0 to SINE_STEPS */
6 mjames 21
void
47 mjames 22
dial_draw_needle (uint16_t position)
2 mjames 23
{
47 mjames 24
  int ang = SINE_SCALING * ((position - (SINE_STEPS/2)) * a1) / (SINE_STEPS/2);
2 mjames 25
 
6 mjames 26
  int si = ap_sin (ang);
27
  int co = ap_cos (ang);
2 mjames 28
 
29
  /* calculate a shift for a second side of the needle */
46 mjames 30
  int xl = ap_sin (ang - SINE_STEPS);
31
  int yl = ap_cos (ang - SINE_STEPS);
2 mjames 32
 
6 mjames 33
  int si2 = siz + 2;
7 mjames 34
  int si3 = 2 * siz / 3;
5 mjames 35
 
7 mjames 36
  int xs,ys;
37
  // three parallel lines
38
  xs = -xl;
39
  ys = -yl;
40
  int step;
41
  for(step =0; step < 3; step++)
42
    {
43
    drawLine (AP_SCALE(si*si2-xs) + xo, yo - AP_SCALE(co * si2 - ys),
5 mjames 44
 
48 mjames 45
    AP_SCALE(si*si3-xs) + xo,
6 mjames 46
            yo - AP_SCALE(co * si3 - ys), INVERT);
7 mjames 47
    xs+=xl;
48
    ys+=yl;
49
    }
50
 }
5 mjames 51
 
2 mjames 52
/* initialise */
6 mjames 53
void
54
dial_size (uint8_t size)
2 mjames 55
{
6 mjames 56
  siz = size;
2 mjames 57
}
58
 
13 mjames 59
void dial_draw_scale (int16_t low, int16_t high, uint8_t width, uint8_t num_step,int16_t scale)
2 mjames 60
{
6 mjames 61
  int sz;
62
  int ang;
13 mjames 63
  int sc_low  = low/scale;
64
  int sc_high = high/scale;
65
  int step = 256 * a1 * 2 / (4 * (sc_high - sc_low));
6 mjames 66
  int t;
67
  ang = -a1 * 256;
13 mjames 68
  for (t = sc_low * 4; t <= sc_high * 4; t++)
6 mjames 69
    {
46 mjames 70
      int si = ap_sin ((ang*SINE_SCALING) / 256);
71
      int co = ap_cos ((ang*SINE_SCALING) / 256);
6 mjames 72
 
73
      int len;
74
      switch (t % 4)
2 mjames 75
        {
6 mjames 76
        case 0:
77
          len = width;
78
          break;
79
        case 1:
80
        case 3:
15 mjames 81
        case -1:
82
        case -3:
6 mjames 83
          len = width / 4;
84
          break;
85
        case 2:
15 mjames 86
        case -2:
6 mjames 87
          len = width / 2;
88
          break;
89
        }
2 mjames 90
 
6 mjames 91
      drawLine (AP_SCALE((siz)*si) + xo, yo - AP_SCALE((siz) * co),
92
      AP_SCALE((siz-len)*si) + xo,
93
                yo - AP_SCALE((siz - len) * co), 1);
94
      ang += step;
95
    }
2 mjames 96
 
15 mjames 97
  font_sig_digits (0,7,0,10,low);
98
  font_sig_digits(20,7,1,10,high);
6 mjames 99
}
2 mjames 100
 
6 mjames 101
void
102
dial_origin (uint8_t x, uint8_t y)
2 mjames 103
{
6 mjames 104
  xo = x;
105
  yo = y;
2 mjames 106
}