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