Subversion Repositories DashDisplay

Rev

Rev 77 | Go to most recent revision | Blame | Compare with Previous | 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 <cstdlib>
  14. #include "libOLED/stm32_halDisplay.H"
  15. #include "libOLED/fontclass.H"
  16. #include "libOLED/displayDial.H"
  17. #include "libPlx/displayInfo.H"
  18. #include "libOLED/ap_math.h"
  19. #include "libSmallPrintf/small_printf.h"
  20.  
  21. #include "splash.H"
  22.  
  23. namespace
  24. {
  25.         int const WIDTH = 128;
  26.         int const HEIGHT = 64;
  27.         int const DISPLAY_RAMWIDTH = 132;
  28.  
  29. }
  30.  
  31. uint8_t displayBuffer[2][dataSize(WIDTH, HEIGHT)];
  32.  
  33. stm32_halDisplay_t displays[MAX_DISPLAYS] =
  34.         {stm32_halDisplay_t(WIDTH, HEIGHT, DISPLAY_RAMWIDTH, displayBuffer[0],
  35.                                                 &hspi1,
  36.                                                 SPI_CD_GPIO_Port,
  37.                                                 SPI_CD_Pin,
  38.                                                 SPI_RESET_GPIO_Port,
  39.                                                 SPI_RESET_Pin,
  40.                                                 SPI_NSS1_GPIO_Port,
  41.                                                 SPI_NSS1_Pin),
  42.          stm32_halDisplay_t(WIDTH, HEIGHT,
  43.                                                 DISPLAY_RAMWIDTH,
  44.                                                 displayBuffer[1],
  45.                                                 &hspi1,
  46.                                                 SPI_CD_GPIO_Port,
  47.                                                 SPI_CD_Pin,
  48.                                                 SPI_RESET_GPIO_Port,
  49.                                                 SPI_RESET_Pin,
  50.                                                 SPI_NSS2_GPIO_Port,
  51.                                                 SPI_NSS2_Pin)};
  52.  
  53. displayDial_t dials[MAX_DISPLAYS] =
  54.         {displayFullDial_t(displays[0]), displayFullDial_t(displays[1])};
  55. #if defined __cplusplus
  56. extern "C"
  57. {
  58. #endif
  59.         static void
  60.         showMinMax(display_t &display, uint8_t dp_pos, int16_t int_min,
  61.                            uint16_t int_max)
  62.         {
  63.                 const char padding[] = "      ";
  64.                 // left justified display of minimum
  65.                 int8_t width = display.fontSigDigits(small_font, 0, 0, 0, dp_pos, int_min, WHITE);
  66.                 // pad with spaces if fewer than 6 characters are used.
  67.                 if (width != 6)
  68.                         display.printString(small_font, padding, 6 - width, WHITE);
  69.  
  70.                 display.gotoxy(0, 8);
  71.                 display.printString(small_font, "Min", 3, WHITE);
  72.  
  73.                 // right justified display of maximum
  74.                 width = display.fontSigDigits(small_font, 120, 0, 1, dp_pos, int_max, WHITE);
  75.                 // right justified display of maximum : pad spaces to left
  76.                 if (width != 6)
  77.                         display.printString(small_font, padding, 6 - width, WHITE);
  78.  
  79.                 display.gotoxy(110, 8);
  80.                 display.printString(small_font, "Max", 3, WHITE);
  81.         }
  82.  
  83.         void
  84.         cc_init()
  85.         {
  86.                 for (auto i = 0; i < MAX_DISPLAYS; i++)
  87.                 {
  88.                         display_t &display = displays[i];
  89.                         if (i == 0)
  90.                                 display.reset();
  91.                         display.init();
  92.                         display.clearDisplay(BLACK);
  93.                         displaySplash(display);
  94.                         display.gotoxy(8, 32);
  95.                         display.printString(large_font, i == 0 ? "1" : "2", 1, BLACK);
  96.                         display.display();
  97.                 }
  98.  
  99.                 HAL_Delay(1000);
  100.  
  101.                 for (auto i = 0; i < MAX_DISPLAYS; i++)
  102.                 {
  103.                         display_t &display = displays[i];
  104.                         display.clearDisplay(BLACK);
  105.                         display.setPixelMode(WHITE);
  106.                         display.display();
  107.                         context_t &context = contexts[i];
  108.                         context.dial_timer = 500; // enough time to see at least one frame of PLX before NVRAM check
  109.                         context.dial1 = -1;
  110.                         context.OldObservation = nullObs;
  111.  
  112.                         context.knobPos = -1; // indicate it is initialised
  113.                 }
  114.         }
  115.  
  116.         // Check to see if there is an observation/instance in the dynamic data array
  117.         // that matches the current observation/instance in the NVRAM
  118.  
  119.         void
  120.         cc_check_nvram(int dialIndex)
  121.         {
  122.                 if (dialIndex < 0 && dialIndex > MAX_DISPLAYS)
  123.                         return;
  124.                 // algorithm only works when there is a vector of observations
  125.  
  126.                 context_t &context = contexts[dialIndex];
  127.  
  128.                 // check for timer timeout on consistent timer
  129.  
  130.                 if (context.dial_timer)
  131.                 {
  132.                         context.dial_timer--;
  133.                 }
  134.                 if (context.dial_timer == 0)
  135.                 {
  136.                         context.dial_timer = DialTimeout;
  137.                         int i;
  138.                         // use dialIndex+1 as tag for data : always non-zero.
  139.                         nvram_info_t *dial_nvram = find_nvram_data(dialIndex + 1);
  140.  
  141.                         // initial read operation if knobPos < 0
  142.                         if (dial_nvram && context.knobPos < 0)
  143.                         {
  144.                                 context.knobPos = dial_nvram->data.pos;
  145.                                 return;
  146.                         }
  147.                         if (context.knobPos < 0)
  148.                                 context.knobPos = dialIndex; // timed out , not in NVRAM, use a default
  149.  
  150.                         // dont save dial info for invalid data
  151.                         if (!isValid(context.knobPos))
  152.                                 return;
  153.                         // is this a change since the last timeout ?
  154.  
  155.                         if (!dial_nvram  || context.knobPos != dial_nvram->data.pos)
  156.                         {
  157.  
  158.                                 // store the observation and instance in the NVRAM, not dial position.
  159.                                 nvram_info_t curr_val;
  160.                                 curr_val.data.pos = context.knobPos;
  161.                                 curr_val.data.tag = dialIndex + 1;
  162.  
  163.                                 write_nvram_data(curr_val);
  164.                         }
  165.                 }
  166.         }
  167.  
  168.         int
  169.         cc_display(int dialIndex, int suppressIndex)
  170.  
  171.         {
  172.  
  173.                 if (dialIndex < 0 && dialIndex > MAX_DISPLAYS)
  174.                         return -1;
  175.                 context_t &context = contexts[dialIndex];
  176.                 displayDial_t &dial = dials[dialIndex];
  177.                 stm32_halDisplay_t &display = displays[dialIndex];
  178.                 int itemIndex = context.knobPos;
  179.                 char buff[10];
  180.                 int i;
  181.                 const char *msg;
  182.                 int len;
  183.                 // check for startup phase
  184.                 if (itemIndex < 0)
  185.                 {
  186.                         display.clearDisplay(BLACK);
  187.                         i = small_sprintf(buff, "Wait");
  188.                         display.gotoxy(64 - i * 4, 48);
  189.                         display.printString(large_font, buff, i, WHITE);
  190.  
  191.                         display.display();
  192.                         return -1;
  193.                 }
  194.  
  195.                 // check for item suppression
  196.                 if (itemIndex == suppressIndex)
  197.                 {
  198.                         context.dial1 = -1;
  199.                         context.OldObservation = nullObs;
  200.                         display.clearDisplay(BLACK);
  201.                         i = small_sprintf(buff, "Supp-%02d", itemIndex);
  202.                         display.gotoxy(64 - i * 4, 48);
  203.                         display.printString(large_font, buff, i, WHITE);
  204.  
  205.                         display.display();
  206.                         return -1; // we suppressed this display
  207.                 }
  208.  
  209.                 // check for item validity
  210.                 if (!isValid(itemIndex))
  211.                 {
  212.                         context.dial1 = -1;
  213.                         context.OldObservation = nullObs;
  214.                         display.clearDisplay(BLACK);
  215.                         i = small_sprintf(buff, "Inval-%02d", itemIndex);
  216.                         display.gotoxy(64 - i * 4, 48);
  217.                         display.printString(large_font, buff, i, WHITE);
  218.  
  219.                         display.display();
  220.                         return itemIndex;
  221.                 }
  222.  
  223.                 // clear startup display off the screen
  224.                 if (context.OldObservation.Obs == -1)
  225.                         display.clearDisplay(BLACK);
  226.  
  227.                 int DataVal = Info[itemIndex].data; // data reading
  228.                 PLX_Observations Observation = Info[itemIndex].observation.Obs;
  229.                 uint8_t Instance = Info[itemIndex].observation.Instance;
  230.                 // now to convert the readings and format strings
  231.                 // find out limits
  232.                 // if the user presses the dial then reset min/max to current value
  233.                 if (push_pos[dialIndex] == 1)
  234.                 {
  235.                         Info[itemIndex].Max = DataVal;
  236.                         Info[itemIndex].Min = DataVal; // 12 bit max value
  237.                 }
  238.  
  239.                 // detect change in observation being displayed, reset the dial
  240.                 if (Observation < PLX_MAX_OBS)
  241.                 {      
  242.                         if (Observation != context.OldObservation.Obs || Instance != context.OldObservation.Instance)
  243.                         {
  244.                                
  245.                                 display.clearDisplay(BLACK);
  246.                                 dial.draw_scale(DisplayInfo[Observation].Low,
  247.                                                                 DisplayInfo[Observation].High, 12, 1,
  248.                                                                 DisplayInfo[Observation].TickScale);
  249.  
  250.                                 dial.draw_limits();
  251.  
  252.                                 msg = DisplayInfo[Observation].name;
  253.                                 len = 7;
  254.                                 int len1 = Instance > 0 ? len - 1 : len;
  255.                                 for (i = 0; i < len1 && msg[i]; i++)
  256.                                 {
  257.                                         buff[i] = msg[i];
  258.                                 }
  259.                                 if (Instance > 0 && i < len)
  260.                                 {
  261.                                         buff[i++] = Instance + '1';
  262.                                 }
  263.  
  264.                                 display.gotoxy(64 - i * 4, 48);
  265.                                 display.printString(large_font, buff, i, WHITE);
  266.  
  267.                                 context.OldObservation.Obs = Observation;
  268.                                 context.OldObservation.Instance = Instance;
  269.                                 context.dial1 = -1; // do not display old needle, cleared screen
  270.                                 display.display();
  271.                         }
  272.                 }
  273.  
  274.                 if (Info[itemIndex].updated)
  275.                 {
  276.                         Info[itemIndex].updated = 0;
  277.  
  278.                         double max_rdg;
  279.                         double min_rdg;
  280.                         double cur_rdg;
  281.                         int int_rdg;
  282.                         int int_max;
  283.                         int int_min;
  284.  
  285.                         max_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
  286.                                                                                  Info[itemIndex].Max);
  287.                         min_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
  288.                                                                                  Info[itemIndex].Min);
  289.                         cur_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
  290.                                                                                  Info[itemIndex].data);
  291.                         int dp_pos; // where to print the decimal place
  292.                         float scale = 1.0;
  293.                         switch (DisplayInfo[Observation].DP)
  294.                         {
  295.                         default:
  296.                         case 0:
  297.                                 scale = 1.0;
  298.                                 dp_pos = display_t::NO_DECIMAL;
  299.                                 break;
  300.                         case 1:
  301.                                 scale = 10.0;
  302.                                 dp_pos = 1;
  303.                                 break;
  304.                         case 2:
  305.                                 scale = 100.0;
  306.                                 dp_pos = 2;
  307.                                 break;
  308.                         }
  309.                         int_rdg = (int)(cur_rdg * scale);
  310.                         int_max = (int)(max_rdg * scale);
  311.                         int_min = (int)(min_rdg * scale);
  312.  
  313.                         cur_rdg -= DisplayInfo[Observation].Low;
  314.                         cur_rdg = ap_math::SINE_STEPS * cur_rdg / (DisplayInfo[Observation].High - DisplayInfo[Observation].Low);
  315.  
  316.                         context.dial0 = (int)cur_rdg;
  317.  
  318.                         display.gotoxy(32, 28);
  319.                         display.fontDigits(large_font, 4, dp_pos, int_rdg, WHITE);
  320.  
  321.                         display.printString(small_font, DisplayInfo[Observation].suffix,
  322.                                                                 strlen(DisplayInfo[Observation].suffix));
  323.                         display.printString(small_font, "    ",
  324.                                                                 3 - strlen(DisplayInfo[Observation].suffix));
  325.                         // print value overlaid by needle
  326.  
  327.                         /* old needle un-draw */
  328.                         if (context.dial1 >= 0)
  329.                         {
  330.                                 dial.draw_needle(context.dial1);
  331.                         }
  332.                         dial.draw_needle(context.dial0);
  333.                         context.dial1 = context.dial0;
  334.                         showMinMax(display, dp_pos, int_min, int_max);
  335.                 }
  336.                 else
  337.                 {
  338.                         if (Info[itemIndex].lastUpdated && ((HAL_GetTick() - Info[itemIndex].lastUpdated) > 1000))
  339.                         {
  340.                                 context.OldObservation = nullObs; // force a redraw on next update
  341.                                 Info[itemIndex].lastUpdated = 0;  // and stop further timeouts.
  342.                         }
  343.                 }
  344.  
  345.                 display.gotoxy(0, 32);
  346.  
  347.                 // display BT connection status
  348.                 display.printString(small_font, btConnected() ? "\x81" : " ", 1);
  349.  
  350.                 display.display();
  351.  
  352.                 return itemIndex;
  353.         }
  354. }
  355.