Subversion Repositories DashDisplay

Rev

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

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