Subversion Repositories DashDisplay

Rev

Rev 76 | Rev 79 | 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.         }
  113.  
  114.         // Check to see if there is an observation/instance in the dynamic data array
  115.         // that matches the current observation/instance in the NVRAM
  116.  
  117.         void
  118.         cc_check_nvram(int dialIndex)
  119.         {
  120.                 if (dialIndex < 0 && dialIndex > MAX_DISPLAYS)
  121.                         return;
  122.                 // algorithm only works when there is a vector of observations
  123.  
  124.                 context_t &context = contexts[dialIndex];
  125.  
  126.                 // check for timer timeout on consistent timer
  127.  
  128.                 if (context.dial_timer)
  129.                 {
  130.                         context.dial_timer--;
  131.                 }
  132.                 if (context.dial_timer == 0)
  133.                 {
  134.                         context.dial_timer = DialTimeout;
  135.                         int i;
  136.                         // use dialIndex+1 as tag for data : always non-zero.
  137.                         nvram_info_t *dial_nvram = find_nvram_data(dialIndex + 1);
  138.  
  139.                         if (dial_nvram && context.knobPos < 0)
  140.                         {
  141.                                 for (i = 0; i < INFO_SIZE; i++)
  142.                                         if (isValid(i) && (Info[i].observation.Obs == dial_nvram->data.observation) && (Info[i].observation.Instance == dial_nvram->data.instance))
  143.                                         {
  144.                                                 context.knobPos = i;
  145.                                                 return;
  146.                                         }
  147.                         }
  148.                         if (context.knobPos == -1)
  149.                                 context.knobPos = dialIndex; // timed out , not in NVRAM, use a default
  150.  
  151.                         // dont save dial info for invalid data
  152.                         if (!isValid(context.knobPos))
  153.                                 return;
  154.                         // is this a change since the last timeout ?
  155.  
  156.                         if (!dial_nvram || (Info[context.knobPos].observation.Obs != dial_nvram->data.observation) || (Info[context.knobPos].observation.Instance != dial_nvram->data.instance))
  157.                         {
  158.  
  159.                                 // store the observation and instance in the NVRAM, not dial position.
  160.                                 nvram_info_t curr_val;
  161.                                 curr_val.data.observation = Info[context.knobPos].observation.Obs;
  162.                                 curr_val.data.instance = Info[context.knobPos].observation.Instance;
  163.                                 curr_val.data.tag = dialIndex + 1;
  164.  
  165.                                 write_nvram_data(curr_val);
  166.                         }
  167.                 }
  168.         }
  169.  
  170.         int
  171.         cc_display(int dialIndex, int suppressIndex)
  172.  
  173.         {
  174.  
  175.                 if (dialIndex < 0 && dialIndex > MAX_DISPLAYS)
  176.                         return -1;
  177.                 context_t &context = contexts[dialIndex];
  178.                 displayDial_t &dial = dials[dialIndex];
  179.                 stm32_halDisplay_t &display = displays[dialIndex];
  180.                 int itemIndex = context.knobPos;
  181.                 char buff[10];
  182.                 int i;
  183.                 const char *msg;
  184.                 int len;
  185.                 // check for startup phase
  186.                 if (itemIndex < 0)
  187.                 {
  188.                         display.clearDisplay(BLACK);
  189.                         i = small_sprintf(buff, "Wait");
  190.                         display.gotoxy(64 - i * 4, 48);
  191.                         display.printString(large_font, buff, i, WHITE);
  192.  
  193.                         display.display();
  194.                         return -1;
  195.                 }
  196.  
  197.                 // check for item suppression
  198.                 if (itemIndex == suppressIndex)
  199.                 {
  200.                         context.dial1 = -1;
  201.                         context.OldObservation = nullObs;
  202.                         display.clearDisplay(BLACK);
  203.                         i = small_sprintf(buff, "Supp-%02d", itemIndex);
  204.                         display.gotoxy(64 - i * 4, 48);
  205.                         display.printString(large_font, buff, i, WHITE);
  206.  
  207.                         display.display();
  208.                         return -1; // we suppressed this display
  209.                 }
  210.  
  211.                 // check for item validity
  212.                 if (!isValid(itemIndex))
  213.                 {
  214.                         context.dial1 = -1;
  215.                         context.OldObservation = nullObs;
  216.                         display.clearDisplay(BLACK);
  217.                         i = small_sprintf(buff, "Inval-%02d", itemIndex);
  218.                         display.gotoxy(64 - i * 4, 48);
  219.                         display.printString(large_font, buff, i, WHITE);
  220.  
  221.                         display.display();
  222.                         return itemIndex;
  223.                 }
  224.  
  225.                 // clear startup display off the screen
  226.                 if (context.OldObservation.Obs == -1)
  227.                         display.clearDisplay(BLACK);
  228.  
  229.                 int DataVal = Info[itemIndex].data; // data reading
  230.                 PLX_Observations Observation = Info[itemIndex].observation.Obs;
  231.                 uint8_t Instance = Info[itemIndex].observation.Instance;
  232.                 // now to convert the readings and format strings
  233.                 // find out limits
  234.                 // if the user presses the dial then reset min/max to current value
  235.                 if (push_pos[dialIndex] == 1)
  236.                 {
  237.                         Info[itemIndex].Max = DataVal;
  238.                         Info[itemIndex].Min = DataVal; // 12 bit max value
  239.                 }
  240.  
  241.                 // detect change in observation being displayed, reset the dial
  242.                 if (Observation < PLX_MAX_OBS)
  243.                 {      
  244.                         if (Observation != context.OldObservation.Obs || Instance != context.OldObservation.Instance)
  245.                         {
  246.                                
  247.                                 display.clearDisplay(BLACK);
  248.                                 dial.draw_scale(DisplayInfo[Observation].Low,
  249.                                                                 DisplayInfo[Observation].High, 12, 1,
  250.                                                                 DisplayInfo[Observation].TickScale);
  251.  
  252.                                 dial.draw_limits();
  253.  
  254.                                 msg = DisplayInfo[Observation].name;
  255.                                 len = 7;
  256.                                 int len1 = Instance > 0 ? len - 1 : len;
  257.                                 for (i = 0; i < len1 && msg[i]; i++)
  258.                                 {
  259.                                         buff[i] = msg[i];
  260.                                 }
  261.                                 if (Instance > 0 && i < len)
  262.                                 {
  263.                                         buff[i++] = Instance + '1';
  264.                                 }
  265.  
  266.                                 display.gotoxy(64 - i * 4, 48);
  267.                                 display.printString(large_font, buff, i, WHITE);
  268.  
  269.                                 context.OldObservation.Obs = Observation;
  270.                                 context.OldObservation.Instance = Instance;
  271.                                 context.dial1 = -1; // do not display old needle, cleared screen
  272.                                 display.display();
  273.                         }
  274.                 }
  275.  
  276.                 if (Info[itemIndex].updated)
  277.                 {
  278.                         Info[itemIndex].updated = 0;
  279.  
  280.                         double max_rdg;
  281.                         double min_rdg;
  282.                         double cur_rdg;
  283.                         int int_rdg;
  284.                         int int_max;
  285.                         int int_min;
  286.  
  287.                         max_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
  288.                                                                                  Info[itemIndex].Max);
  289.                         min_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
  290.                                                                                  Info[itemIndex].Min);
  291.                         cur_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
  292.                                                                                  Info[itemIndex].data);
  293.                         int dp_pos; // where to print the decimal place
  294.                         float scale = 1.0;
  295.                         switch (DisplayInfo[Observation].DP)
  296.                         {
  297.                         default:
  298.                         case 0:
  299.                                 scale = 1.0;
  300.                                 dp_pos = display_t::NO_DECIMAL;
  301.                                 break;
  302.                         case 1:
  303.                                 scale = 10.0;
  304.                                 dp_pos = 1;
  305.                                 break;
  306.                         case 2:
  307.                                 scale = 100.0;
  308.                                 dp_pos = 2;
  309.                                 break;
  310.                         }
  311.                         int_rdg = (int)(cur_rdg * scale);
  312.                         int_max = (int)(max_rdg * scale);
  313.                         int_min = (int)(min_rdg * scale);
  314.  
  315.                         cur_rdg -= DisplayInfo[Observation].Low;
  316.                         cur_rdg = ap_math::SINE_STEPS * cur_rdg / (DisplayInfo[Observation].High - DisplayInfo[Observation].Low);
  317.  
  318.                         context.dial0 = (int)cur_rdg;
  319.  
  320.                         display.gotoxy(32, 28);
  321.                         display.fontDigits(large_font, 4, dp_pos, int_rdg, WHITE);
  322.  
  323.                         display.printString(small_font, DisplayInfo[Observation].suffix,
  324.                                                                 strlen(DisplayInfo[Observation].suffix));
  325.                         display.printString(small_font, "    ",
  326.                                                                 3 - strlen(DisplayInfo[Observation].suffix));
  327.                         // print value overlaid by needle
  328.  
  329.                         /* old needle un-draw */
  330.                         if (context.dial1 >= 0)
  331.                         {
  332.                                 dial.draw_needle(context.dial1);
  333.                         }
  334.                         dial.draw_needle(context.dial0);
  335.                         context.dial1 = context.dial0;
  336.                         showMinMax(display, dp_pos, int_min, int_max);
  337.                 }
  338.                 else
  339.                 {
  340.                         if (Info[itemIndex].lastUpdated && ((HAL_GetTick() - Info[itemIndex].lastUpdated) > 1000))
  341.                         {
  342.                                 context.OldObservation = nullObs; // force a redraw on next update
  343.                                 Info[itemIndex].lastUpdated = 0;  // and stop further timeouts.
  344.                         }
  345.                 }
  346.  
  347.                 display.gotoxy(0, 32);
  348.  
  349.                 // display BT connection status
  350.                 display.printString(small_font, btConnected() ? "\x81" : " ", 1);
  351.  
  352.                 display.display();
  353.  
  354.                 return itemIndex;
  355.         }
  356. }
  357.