Subversion Repositories DashDisplay

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * display.cpp
  3.  *
  4.  *  Created on: 30 Nov 2020
  5.  *      Author: mike
  6.  */
  7.  
  8. #include "main.h"
  9. #include "display.h"
  10. #include "switches.h"
  11. #include "nvram.h"
  12. #include <cstring>
  13. #include "libOLED/stm32_halDisplay.H"
  14. #include "libOLED/fontclass.H"
  15. #include "libOLED/displayDial.H"
  16. #include "libPlx/displayInfo.H"
  17. #include "libOLED/ap_math.h"
  18.  
  19. #include "splash.H"
  20.  
  21. namespace
  22. {
  23.   int const WIDTH = 128;
  24.   int const HEIGHT = 64;
  25.   int const DISPLAY_RAMWIDTH = 132;
  26. }
  27.  
  28. uint8_t displayBuffer[2][dataSize (WIDTH, HEIGHT)];
  29.  
  30. stm32_halDisplay_t displays[MAX_DISPLAYS] =
  31.   { stm32_halDisplay_t (WIDTH, HEIGHT, DISPLAY_RAMWIDTH, displayBuffer[0],
  32.                         &hspi1,
  33.                         SPI_CD_GPIO_Port,
  34.                         SPI_CD_Pin,
  35.                         SPI_RESET_GPIO_Port,
  36.                         SPI_RESET_Pin,
  37.                         SPI_NSS1_GPIO_Port,
  38.                         SPI_NSS1_Pin), stm32_halDisplay_t (WIDTH, HEIGHT,
  39.                                                            DISPLAY_RAMWIDTH,
  40.                                                            displayBuffer[1],
  41.                                                            &hspi1,
  42.                                                            SPI_CD_GPIO_Port,
  43.                                                            SPI_CD_Pin,
  44.                                                            SPI_RESET_GPIO_Port,
  45.                                                            SPI_RESET_Pin,
  46.                                                            SPI_NSS2_GPIO_Port,
  47.                                                            SPI_NSS2_Pin) };
  48.  
  49. displayDial_t dials[MAX_DISPLAYS] =
  50.   { displayDial_t (displays[0], 64, 60, 60, 90), displayDial_t (displays[1], 64,
  51.                                                                 60, 60, 90) };
  52. #if defined __cplusplus
  53. extern "C"
  54. {
  55. #endif
  56.   static void
  57.   showMinMax (display_t &display, uint8_t dp_pos, int16_t int_min,
  58.               uint16_t int_max)
  59.   {
  60.     display.fontSigDigits (small_font, 0, 0, 0, dp_pos, int_min, WHITE);
  61.     display.gotoxy (0, 8);
  62.  
  63.     display.printString (small_font, "Min°", 4, WHITE);
  64.  
  65.     display.fontSigDigits (small_font, 120, 0, 1, dp_pos, int_max, WHITE);
  66.     display.gotoxy (110, 8);
  67.     display.printString (small_font, "Max", 3, WHITE);
  68.   }
  69.  
  70.   void
  71.   cc_init ()
  72.   {
  73.     for (auto i = 0; i < MAX_DISPLAYS; i++)
  74.       {
  75.         display_t &display = displays[i];
  76.         if (i == 0)
  77.           display.reset ();
  78.         display.init ();
  79.         display.clearDisplay ();
  80.         displaySplash (display);
  81.         display.gotoxy (8, 32);
  82.         display.printString (large_font, i == 0 ? "1" : "2", 1, BLACK);
  83.         display.display ();
  84.       }
  85.  
  86.     HAL_Delay (1000);
  87.  
  88.     for (auto i = 0; i < MAX_DISPLAYS; i++)
  89.       {
  90.         display_t &display = displays[i];
  91.         displayDial_t &dial = dials[i];
  92.         display.clearDisplay ();
  93.         display.setPixelMode(WHITE);
  94.         dial.draw_scale (0, 10, 12, 1, 1);
  95.         dial.draw_limits ();
  96.         display.gotoxy (8, 32);
  97.         display.printString (large_font, "Display ", 8, WHITE);
  98.         display.printString (large_font, i == 0 ? "1" : "2", 1, BLACK);
  99.         showMinMax (display, 2, 123, 456);
  100.  
  101.         display.display ();
  102.       }
  103.   }
  104.  
  105.   int
  106.   cc_display (int dialIndex, int itemIndex, int suppressIndex)
  107.  
  108.   {
  109.     if (dialIndex < 0 && dialIndex > MAX_DISPLAYS)
  110.       return -1;
  111.     context_t &context = contexts[dialIndex];
  112.     displayDial_t &dial = dials[dialIndex];
  113.     stm32_halDisplay_t &display = displays[dialIndex];
  114.     char buff[10];
  115.     int i;
  116.     int rc;
  117.  
  118.     // check for item suppression
  119.     if (itemIndex == suppressIndex)
  120.       {
  121.         context.dial1 = -1;
  122.         context.OldObservation = -1;
  123.         context.OldObservationIndex = -1;
  124.  
  125.         display.clearDisplay ();
  126.         display.display ();
  127.         return -1; // we suppressed this display
  128.       }
  129.  
  130.     // clear startup display off the screen
  131.     if (context.OldObservation == -1)
  132.       display.clearDisplay ();
  133.  
  134.     int DataVal = ConvPLX (Data.Sensor[itemIndex].ReadingH,
  135.                            Data.Sensor[itemIndex].ReadingL); // data reading
  136.     int Observation = ConvPLX (Data.Sensor[itemIndex].AddrH,
  137.                                Data.Sensor[itemIndex].AddrL);
  138.     int ObservationIndex = ConvPLX (0, Data.Sensor[itemIndex].Instance);
  139.     // now to convert the readings and format strings
  140.     // find out limits
  141.     char *msg;
  142.     int len;
  143.  
  144.     // if the user presses the dial then reset min/max to current value
  145.     if (push_pos[dialIndex] == 1)
  146.       {
  147.         Max[itemIndex] = DataVal;
  148.         Min[itemIndex] = DataVal; // 12 bit max value
  149.       }
  150.  
  151.     if (Observation < PLX_MAX_OBS)
  152.       {
  153.         if (Observation != context.OldObservation
  154.             || ObservationIndex != context.OldObservationIndex)
  155.           {
  156.  
  157.             context.dial_timer = DialTimeout;
  158.  
  159.             context.dial1 = -1;
  160.             display.clearDisplay ();
  161.             dial.draw_scale (DisplayInfo[Observation].Low,
  162.                              DisplayInfo[Observation].High, 12, 1,
  163.                              DisplayInfo[Observation].TickScale);
  164.  
  165.             dial.draw_limits ();
  166.  
  167.             msg = DisplayInfo[Observation].name;
  168.             len = 7;
  169.             int len1 = ObservationIndex > 0 ? len - 1 : len;
  170.             for (i = 0; i < len1 && msg[i]; i++)
  171.               {
  172.                 buff[i] = msg[i];
  173.               }
  174.             if (ObservationIndex > 0 && i < len)
  175.               {
  176.                 buff[i++] = ObservationIndex + '1';
  177.               }
  178.  
  179.             display.gotoxy (64 - i * 4, 48);
  180.             display.printString (large_font, buff, i, WHITE);
  181.  
  182.             context.OldObservation = Observation;
  183.             context.OldObservationIndex = ObservationIndex;
  184.  
  185.             display.display ();
  186.  
  187.           }
  188.         else
  189.           {
  190.             // check for timer timeout on consistent timer
  191.             if (context.dial_timer)
  192.               {
  193.                 context.dial_timer--;
  194.  
  195.                 if (context.dial_timer == 0)
  196.                   {
  197.                     uint16_t curr_val = dial_pos[dialIndex];
  198.  
  199.                     uint32_t addr = (uint32_t) (&dial_nvram[dialIndex]);
  200.                     WriteUint16NVRAM (addr, curr_val);
  201.  
  202.                   }
  203.               }
  204.           }
  205.  
  206.       }
  207.  
  208.     double max_rdg;
  209.     double min_rdg;
  210.     double cur_rdg;
  211.     int int_rdg;
  212.     int int_max;
  213.     int int_min;
  214.  
  215.     max_rdg = ConveriMFDRaw2Data (Observation, DisplayInfo[Observation].Units,
  216.                                   Max[itemIndex]);
  217.     min_rdg = ConveriMFDRaw2Data (Observation, DisplayInfo[Observation].Units,
  218.                                   Min[itemIndex]);
  219.     cur_rdg = ConveriMFDRaw2Data (Observation, DisplayInfo[Observation].Units,
  220.                                   DataVal);
  221.  
  222.     int dp_pos;  // where to print the decimal place
  223.     float scale = 1.0;
  224.     switch (DisplayInfo[Observation].DP)
  225.       {
  226.       default:
  227.       case 0:
  228.         scale = 1.0;
  229.         dp_pos = display_t::NO_DECIMAL;
  230.         break;
  231.       case 1:
  232.         scale = 10.0;
  233.         dp_pos = 1;
  234.         break;
  235.       case 2:
  236.         scale = 100.0;
  237.         dp_pos = 2;
  238.         break;
  239.  
  240.       }
  241.     int_rdg = (int) (cur_rdg * scale);
  242.     int_max = (int) (max_rdg * scale);
  243.     int_min = (int) (min_rdg * scale);
  244.  
  245.     cur_rdg -= DisplayInfo[Observation].Low;
  246.     cur_rdg = ap_math::SINE_STEPS * cur_rdg
  247.         / (DisplayInfo[Observation].High - DisplayInfo[Observation].Low);
  248.  
  249.     context.dial0 = (int) cur_rdg;
  250.  
  251.     display.gotoxy (32, 28);
  252.     display.fontDigits (large_font, 4, dp_pos, int_rdg, WHITE);
  253.  
  254.     display.printString (small_font, DisplayInfo[Observation].suffix,
  255.                          strlen (DisplayInfo[Observation].suffix));
  256.     display.printString (small_font, "    ",
  257.                          3 - strlen (DisplayInfo[Observation].suffix));
  258.     // print value overlaid by needle
  259.  
  260.     /* old needle un-draw */
  261.     if (context.dial1 >= 0)
  262.       {
  263.         dial.draw_needle (context.dial1);
  264.       }
  265.     dial.draw_needle (context.dial0);
  266.     context.dial1 = context.dial0;
  267.     showMinMax (display, dp_pos, int_min, int_max);
  268.  
  269.     display.display ();
  270.  
  271.     return itemIndex;
  272.   }
  273.  
  274. }
  275.  
  276.