Subversion Repositories libOLED

Rev

Rev 16 | 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.  
  8. #include "libOLED/ap_math.H"
  9. #include "libOLED/displayclass.H"
  10. #include "libOLED/fontclass.H"
  11. #include "libOLED/displayDial.H"
  12. #include <string.h>
  13.  
  14. static ap_math math;
  15.  
  16. displayDial_t::displayDial_t(display_t &display,
  17.                              uint8_t x, uint8_t y,
  18.                              uint8_t siz,
  19.                              int16_t angle_low,
  20.                              int16_t angle_high) : m_display(display),
  21.                                                    m_xo(x), m_yo(y), m_siz(siz),
  22.                                                    m_angleLow(angle_low),
  23.                                                    m_angleRange(angle_high - angle_low),
  24.                                                    m_unitStr(nullptr),
  25.                                                    m_titleStr(nullptr)
  26. {
  27. }
  28.  
  29. displayDial_t::displayDial_t(display_t &display,
  30.                              uint8_t x, uint8_t y,
  31.                              uint8_t siz,
  32.                              uint16_t angle_) : m_display(display),
  33.                                                 m_xo(x), m_yo(y),
  34.                                                 m_siz(siz),
  35.                                                 m_angleLow(-angle_),
  36.                                                 m_angleRange(2 * angle_),
  37.                                                 m_unitStr(nullptr),
  38.                                                 m_titleStr(nullptr)
  39. {
  40. }
  41.  
  42. /* position is integer from 0 to SINE_STEPS */
  43. void displayDial_t::draw_needle(int16_t position)
  44. {
  45.   // int ang = math.SINE_SCALING * ((position - (math.SINE_STEPS / 2)) * m_a1) / (math.SINE_STEPS / 2);
  46.   int ang = (math.SINE_SCALING * position * m_angleRange) / math.SINE_STEPS + m_angleLow * math.SINE_SCALING;
  47.   int si = math.ap_sin(ang);
  48.   int co = math.ap_cos(ang);
  49.  
  50.   /* calculate an orthogonal XY shift for a second side of the needle */
  51.   int xl = math.ap_sin(ang - math.SINE_STEPS);
  52.   int yl = math.ap_cos(ang - math.SINE_STEPS);
  53.  
  54.   int si2 = m_siz + 2;
  55.   int si3 = 2 * m_siz / 3;
  56.  
  57.   int xs, ys;
  58.   // three parallel lines
  59.   xs = -xl;
  60.   ys = -yl;
  61.   int step;
  62.   for (step = 0; step < 3; step++)
  63.   {
  64.     m_display.drawLine(math.AP_SCALE(si * si2 - xs) + m_xo, m_yo - math.AP_SCALE(co * si2 - ys),
  65.                        math.AP_SCALE(si * si3 - xs) + m_xo, m_yo - math.AP_SCALE(co * si3 - ys), INVERT);
  66.     xs += xl;
  67.     ys += yl;
  68.   }
  69. }
  70.  
  71. void displayDial_t::draw_scale(int16_t low, int16_t high, uint8_t width, uint8_t num_step, int16_t scale)
  72. {
  73.  
  74.   int ang;
  75.   m_low = low;
  76.   m_high = high;
  77.   int sc_low = low / scale;
  78.   int sc_high = high / scale;
  79.   int step = 256 * m_angleRange / (4 * (sc_high - sc_low));
  80.   int t;
  81.   ang = m_angleLow * 256;
  82.   int d = sc_low < sc_high ? 1 : -1;
  83.   t = sc_low * 4;
  84.   while (1)
  85.   {
  86.     int si = math.ap_sin((ang * math.SINE_SCALING) / 256);
  87.     int co = math.ap_cos((ang * math.SINE_SCALING) / 256);
  88.  
  89.     int len;
  90.     switch (t % 4)
  91.     {
  92.     case 0:
  93.       len = width;
  94.       break;
  95.     case 1:
  96.     case 3:
  97.     case -1:
  98.     case -3:
  99.       len = width / 4;
  100.       break;
  101.     case 2:
  102.     case -2:
  103.       len = width / 2;
  104.       break;
  105.     }
  106.  
  107.     m_display.drawLine(math.AP_SCALE((m_siz)*si) + m_xo, m_yo - math.AP_SCALE((m_siz)*co),
  108.                        math.AP_SCALE((m_siz - len) * si) + m_xo,
  109.                        m_yo - math.AP_SCALE((m_siz - len) * co), WHITE);
  110.  
  111.     if (t == sc_high * 4)
  112.       break;
  113.  
  114.     ang += step;
  115.     t += d;
  116.   }
  117. }
  118.  
  119. void displayDial_t::draw_value_items(int val, int dpCode, int width, int x, int y)
  120. {
  121.  
  122.   m_display.gotoxy(x, y);
  123.   m_display.fontDigits(medium_font, width, dpCode, val, WHITE);
  124.   m_display.gotoxy(x, y + 16);
  125.   if (m_unitStr)
  126.   {
  127.     int l = strlen(m_unitStr);
  128.     if (l > 4)
  129.       l = 4;
  130.     m_display.printString(small_font, m_unitStr, l);
  131.     m_display.printString(small_font, "     ", 4 - l);
  132.   }
  133.   else
  134.   {
  135.     m_display.printString(small_font, "     ", 4);
  136.   }
  137. }
  138.  
  139. ////////////////////////////////////////////////////////////////////////////////
  140. displayFullDial_t::displayFullDial_t(display_t &display) : displayDial_t(display, 64, 60, 60, 90)
  141. {
  142. }
  143.  
  144. void displayFullDial_t::draw_limits()
  145. {
  146.   m_display.fontSigDigits(small_font, 0, 56, 0, 10, m_low, WHITE);
  147.   m_display.fontSigDigits(small_font, 120, 56, 1, 10, m_high, WHITE);
  148.  
  149.   if (m_titleStr)
  150.   {
  151.     int i = strlen(m_titleStr);
  152.     m_display.gotoxy(64 - i * 5, 48);
  153.     m_display.printString(large_font, m_titleStr, i, WHITE);
  154.   }
  155. }
  156.  
  157. void displayFullDial_t ::draw_value(int val, int dp, int digits)
  158. {
  159.   draw_value_items(val, dp, digits, 30, 32);
  160. }
  161.  
  162. ////////////////////////////////////////////////////////////////////////////////
  163. displayLeftQuadrantDial_t::displayLeftQuadrantDial_t(display_t &display) : displayDial_t(display, 0, 60, 60, 90, 0){};
  164.  
  165. void displayLeftQuadrantDial_t::draw_limits()
  166. {
  167.   m_display.fontSigDigits(small_font, 0, 10, false, display_t::NO_DECIMAL, m_high, WHITE);
  168.   m_display.fontSigDigits(small_font, 56, 56, true, display_t::NO_DECIMAL, m_low, WHITE);
  169.  
  170.   if (m_titleStr)
  171.   {
  172.     int i = strlen(m_titleStr);
  173.     m_display.gotoxy(64 - i * 5, 0);
  174.     m_display.printString(large_font, m_titleStr, i, WHITE);
  175.   }
  176. }
  177.  
  178. void displayLeftQuadrantDial_t ::draw_value(int val, int dp, int digits)
  179. {
  180.   draw_value_items(val, dp, digits, 0, 32);
  181. }
  182.  
  183. ////////////////////////////////////////////////////////////////////////////////
  184. displayRightQuadrantDial_t::displayRightQuadrantDial_t(display_t &display) : displayDial_t(display, 127, 60, 60, -90, 0){};
  185.  
  186. void displayRightQuadrantDial_t::draw_limits()
  187. {
  188.   m_display.fontSigDigits(small_font, 128, 10, true, display_t::NO_DECIMAL, m_high, WHITE);
  189.   m_display.fontSigDigits(small_font, 128 - 56, 56, false, display_t::NO_DECIMAL, m_low, WHITE);
  190. }
  191.  
  192. void displayRightQuadrantDial_t::draw_value(int val, int dp, int digits)
  193. {
  194.   draw_value_items(val, dp, digits, 90, 32);
  195. }
  196.