Subversion Repositories DashDisplay

Rev

Rev 67 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
66 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
{
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
        {displayDial_t(displays[0], 64, 60, 60, 90), displayDial_t(displays[1], 64,
52
                                                                                                                           60, 60, 90)};
53
#if defined __cplusplus
54
extern "C"
55
{
56
#endif
57
        static void
58
        showMinMax(display_t &display, uint8_t dp_pos, int16_t int_min,
59
                           uint16_t int_max)
60
        {
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
 
68
                display.gotoxy(0, 8);
69
                display.printString(small_font, "Min", 3, WHITE);
70
 
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);
76
 
77
                display.gotoxy(110, 8);
78
                display.printString(small_font, "Max", 3, WHITE);
79
        }
80
 
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
                }
96
 
97
                HAL_Delay(1000);
98
 
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
        }
112
 
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
115
 
116
        void
117
        cc_check_nvram(int dialIndex)
118
        {
119
                if (dialIndex < 0 && dialIndex > MAX_DISPLAYS)
120
                        return;
121
                context_t &context = contexts[dialIndex];
122
 
123
                // check for timer timeout on consistent timer
124
 
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
                                // use dialIndex+1 as tag for data : always non-zero.
133
                                nvram_info_t *dial_nvram = find_nvram_data(dialIndex + 1);
134
 
135
                                if (dial_nvram && context.knobPos < 0)
136
                                {
137
                                        for (i = 0; i < PLXItems; i++)
138
                                                if ((Info[i].observation == dial_nvram->data.observation) && (Info[i].instance == dial_nvram->data.instance))
139
                                                {
140
                                                        context.knobPos = i;
141
                                                        return;
142
                                                }
143
                                }
144
                                if (context.knobPos == -1)
145
                                        context.knobPos = dialIndex; // timed out , not in NVRAM, use a default
146
 
147
                                // is this a change since the last timeout ?
148
                                if (!dial_nvram || (Info[context.knobPos].observation != dial_nvram->data.observation) || (Info[context.knobPos].instance != dial_nvram->data.instance))
149
                                {
150
 
151
                                        // store the observation and instance in the NVRAM, not dial position.
152
                                        nvram_info_t curr_val;
153
                                        curr_val.data.observation = Info[context.knobPos].observation;
154
                                        curr_val.data.instance = Info[context.knobPos].instance;
155
                                        curr_val.data.tag = dialIndex + 1;
156
 
157
                                        write_nvram_data(curr_val);
158
                                }
159
                        }
160
                }
161
        }
162
 
163
        int
164
        cc_display(int dialIndex, int suppressIndex)
165
 
166
        {
167
                if (dialIndex < 0 && dialIndex > MAX_DISPLAYS)
168
                        return -1;
169
                context_t &context = contexts[dialIndex];
170
                displayDial_t &dial = dials[dialIndex];
171
                stm32_halDisplay_t &display = displays[dialIndex];
172
                int itemIndex = context.knobPos;
173
                char buff[10];
174
                int i;
175
 
176
                // check for item suppression
177
                if (itemIndex == suppressIndex)
178
                {
179
                        context.dial1 = -1;
180
                        context.OldObservation = -1;
181
                        context.OldObservationIndex = -1;
182
 
183
                        display.clearDisplay();
184
                        display.display();
185
                        return -1; // we suppressed this display
186
                }
187
 
188
                // clear startup display off the screen
189
                if (context.OldObservation == -1)
190
                        display.clearDisplay(BLACK);
191
 
192
                int DataVal = Info[itemIndex].data; // data reading
193
                int Observation = Info[itemIndex].observation;
194
                int ObservationIndex = Info[itemIndex].instance;
195
                // now to convert the readings and format strings
196
                // find out limits
197
                char *msg;
198
                int len;
199
 
200
                // if the user presses the dial then reset min/max to current value
201
                if (push_pos[dialIndex] == 1)
202
                {
203
                        Info[itemIndex].Max = DataVal;
204
                        Info[itemIndex].Min = DataVal; // 12 bit max value
205
                }
206
 
207
                // detect change in observation being displayed, reset the dial
208
                if (Observation < PLX_MAX_OBS)
209
                {
210
                        if (Observation != context.OldObservation || ObservationIndex != context.OldObservationIndex)
211
                        {
212
 
213
                                display.clearDisplay();
214
                                dial.draw_scale(DisplayInfo[Observation].Low,
215
                                                                DisplayInfo[Observation].High, 12, 1,
216
                                                                DisplayInfo[Observation].TickScale);
217
 
218
                                dial.draw_limits();
219
 
220
                                msg = DisplayInfo[Observation].name;
221
                                len = 7;
222
                                int len1 = ObservationIndex > 0 ? len - 1 : len;
223
                                for (i = 0; i < len1 && msg[i]; i++)
224
                                {
225
                                        buff[i] = msg[i];
226
                                }
227
                                if (ObservationIndex > 0 && i < len)
228
                                {
229
                                        buff[i++] = ObservationIndex + '1';
230
                                }
231
 
232
                                display.gotoxy(64 - i * 4, 48);
233
                                display.printString(large_font, buff, i, WHITE);
234
 
235
                                context.OldObservation = Observation;
236
                                context.OldObservationIndex = ObservationIndex;
237
                                context.dial1 = -1; // do not display old needle, cleared screen
238
                                display.display();
239
                        }
240
                }
241
 
242
                if (Info[itemIndex].updated)
243
                {
244
                        Info[itemIndex].updated = 0;
245
 
246
                        double max_rdg;
247
                        double min_rdg;
248
                        double cur_rdg;
249
                        int int_rdg;
250
                        int int_max;
251
                        int int_min;
252
 
253
                        max_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
254
                                                                                 Info[itemIndex].Max);
255
                        min_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
256
                                                                                 Info[itemIndex].Min);
257
                        cur_rdg = ConveriMFDRaw2Data((enum PLX_Observations)Observation, DisplayInfo[Observation].Units,
258
                                                                                 Info[itemIndex].data);
259
 
260
                        int dp_pos; // where to print the decimal place
261
                        float scale = 1.0;
262
                        switch (DisplayInfo[Observation].DP)
263
                        {
264
                        default:
265
                        case 0:
266
                                scale = 1.0;
267
                                dp_pos = display_t::NO_DECIMAL;
268
                                break;
269
                        case 1:
270
                                scale = 10.0;
271
                                dp_pos = 1;
272
                                break;
273
                        case 2:
274
                                scale = 100.0;
275
                                dp_pos = 2;
276
                                break;
277
                        }
278
                        int_rdg = (int)(cur_rdg * scale);
279
                        int_max = (int)(max_rdg * scale);
280
                        int_min = (int)(min_rdg * scale);
281
 
282
                        cur_rdg -= DisplayInfo[Observation].Low;
283
                        cur_rdg = ap_math::SINE_STEPS * cur_rdg / (DisplayInfo[Observation].High - DisplayInfo[Observation].Low);
284
 
285
                        context.dial0 = (int)cur_rdg;
286
 
287
                        display.gotoxy(32, 28);
288
                        display.fontDigits(large_font, 4, dp_pos, int_rdg, WHITE);
289
 
290
                        display.printString(small_font, DisplayInfo[Observation].suffix,
291
                                                                strlen(DisplayInfo[Observation].suffix));
292
                        display.printString(small_font, "    ",
293
                                                                3 - strlen(DisplayInfo[Observation].suffix));
294
                        // print value overlaid by needle
295
 
296
                        /* old needle un-draw */
297
                        if (context.dial1 >= 0)
298
                        {
299
                                dial.draw_needle(context.dial1);
300
                        }
301
                        dial.draw_needle(context.dial0);
302
                        context.dial1 = context.dial0;
303
                        showMinMax(display, dp_pos, int_min, int_max);
304
                }
305
                else
306
                {
307
                        if (Info[itemIndex].lastUpdated && ((HAL_GetTick() - Info[itemIndex].lastUpdated) > 1000))
308
                        {
309
                                context.OldObservation = -1;     // force a redraw of axes on next entry point
310
                                Info[itemIndex].lastUpdated = 0; // and stop further timeouts.
311
                        }
312
                }
313
 
314
                display.gotoxy(0, 32);
315
 
316
                // display BT connection status
317
                display.printString(small_font, btConnected() ? "\x81" : " ", 1);
318
 
319
                display.display();
320
 
321
                return itemIndex;
322
        }
323
}