Subversion Repositories DashDisplay

Rev

Rev 5 | Rev 7 | Go to most recent revision | 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 "stm32f1xx_hal.h"
  8. #include "ap_math.h"
  9. #include "font.h"
  10. #include "SSD1306.h"
  11.  
  12. static uint16_t a1 = 90;
  13. static uint8_t xo = 64;
  14. static uint8_t yo = 64;
  15. static uint8_t siz = 32;
  16.  
  17. /* percent is integer from 0 to 100 */
  18. void
  19. dial_draw_needle (uint16_t percent)
  20. {
  21.   int ang = ((percent - 50) * a1) / 50;
  22.  
  23.   int si = ap_sin (ang);
  24.   int co = ap_cos (ang);
  25.  
  26.   /* calculate a shift for a second side of the needle */
  27.   int xs = ap_sin (ang - 90);
  28.   int ys = ap_cos (ang - 90);
  29.  
  30.   int si2 = siz + 2;
  31.   int si3 = siz / 2;
  32.  
  33.   drawLine (AP_SCALE(si*si2-xs) + xo, yo - AP_SCALE(co * si2 - ys),
  34.  
  35.   AP_SCALE(si*si3-xs) + xo,
  36.             yo - AP_SCALE(co * si3 - ys), INVERT);
  37.   drawLine (AP_SCALE(si*si2+xs) + xo, yo - AP_SCALE(co * si2 + ys),
  38.  
  39.   AP_SCALE(si*si3+xs) + xo,
  40.             yo - AP_SCALE(co * si3 + ys), INVERT);
  41.  
  42. }
  43.  
  44. /* initialise */
  45. void
  46. dial_size (uint8_t size)
  47. {
  48.   siz = size;
  49. }
  50.  
  51. void
  52. dial_draw_scale (uint8_t low, uint8_t high, uint8_t width, uint8_t num_step)
  53. {
  54.   int sz;
  55.   int ang;
  56.   int step = 256 * a1 * 2 / (4 * (high - low));
  57.   int t;
  58.   ang = -a1 * 256;
  59.   for (t = low * 4; t <= high * 4; t++)
  60.     {
  61.       int si = ap_sin (ang / 256);
  62.       int co = ap_cos (ang / 256);
  63.  
  64.       int len;
  65.       switch (t % 4)
  66.         {
  67.         case 0:
  68.           len = width;
  69.           break;
  70.         case 1:
  71.         case 3:
  72.           len = width / 4;
  73.           break;
  74.         case 2:
  75.           len = width / 2;
  76.           break;
  77.         }
  78.  
  79.       drawLine (AP_SCALE((siz)*si) + xo, yo - AP_SCALE((siz) * co),
  80.       AP_SCALE((siz-len)*si) + xo,
  81.                 yo - AP_SCALE((siz - len) * co), 1);
  82.       ang += step;
  83.     }
  84.  
  85.   font_gotoxy (0, 7);
  86.   int d = low / 10;
  87.   if (d)
  88.     {
  89.       font_putchar (d + '0');
  90.     }
  91.   else
  92.     {
  93.       font_putchar (' ');
  94.     }
  95.   d = low % 10;
  96.   font_putchar (d + '0');
  97.  
  98.   font_gotoxy (19, 7);
  99.   d = high / 10;
  100.   if (d)
  101.     {
  102.       font_putchar (d + '0');
  103.     }
  104.   else
  105.     {
  106.       font_putchar (' ');
  107.     }
  108.   d = high % 10;
  109.   font_putchar (d + '0');
  110.  
  111. }
  112.  
  113. void
  114. dial_origin (uint8_t x, uint8_t y)
  115. {
  116.   xo = x;
  117.   yo = y;
  118. }
  119.