Subversion Repositories DashDisplay

Rev

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