Subversion Repositories DashDisplay

Rev

Rev 29 | Rev 31 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29 Rev 30
Line 40... Line 40...
40
#include "Font.h"
40
#include "Font.h"
41
#include "dials.h"
41
#include "dials.h"
42
#include "switches.h"
42
#include "switches.h"
43
#include <math.h>
43
#include <math.h>
44
#include "plx.h"
44
#include "plx.h"
45
#include "eeprom.h"
-
 
46
#include "displayinfo.h"
45
#include "displayinfo.h"
47
#include "small_printf.h"
46
#include "small_printf.h"
-
 
47
#include "nvram.h"
48
 
48
 
49
/* USER CODE END Includes */
49
/* USER CODE END Includes */
50
 
50
 
51
/* Private variables ---------------------------------------------------------*/
51
/* Private variables ---------------------------------------------------------*/
52
SPI_HandleTypeDef hspi1;
52
SPI_HandleTypeDef hspi1;
Line 61... Line 61...
61
 
61
 
62
/* timeout when the ignition is switched off */
62
/* timeout when the ignition is switched off */
63
#define IGNITION_OFF_TIMEOUT 30000UL
63
#define IGNITION_OFF_TIMEOUT 30000UL
64
 
64
 
65
int OldObservation[2] =
65
int OldObservation[2] =
66
  { -1, -1 }; // illegal initial value
66
{ -1, -1 }; // illegal initial value
67
int OldObservationIndex[2] =
67
int OldObservationIndex[2] =
68
  { -1, -1 }; // if more than one sensor this will be printed
68
{ -1, -1 }; // if more than one sensor this will be printed
69
int16_t dial0[2] =
69
int16_t dial0[2] =
70
  { 0, 0 };
70
{ 0, 0 };
71
int16_t dial1[2] =
71
int16_t dial1[2] =
72
  { -1, -1 };
72
{ -1, -1 };
73
 
73
 
74
uint16_t dial_timer[2] =
74
uint16_t dial_timer[2] =
75
  { 0, 0 };
75
{ 0, 0 };
76
 
76
 
77
static const int DialTimeout = 50; // about 20 seconds after twiddle, save the dial position.
77
static const int DialTimeout = 50; // about 20 seconds after twiddle, save the dial position.
78
 
78
 
79
/* Virtual address defined by the user: 0xFFFF value is prohibited */
79
uint16_t dial_nvram[2] __attribute__((section(".NVRAM_Data")));
80
uint16_t VirtAddVarTab[NumbOfVar] =
-
 
81
  { 0x1111, 0x2222 };
-
 
82
 
80
 
83
union
81
union
84
{
82
{
85
  PLX_SensorInfo Sensor[MAXRDG];
83
        PLX_SensorInfo Sensor[MAXRDG];
86
  char Bytes[MAXRDG * sizeof(PLX_SensorInfo)];
84
        char Bytes[MAXRDG * sizeof(PLX_SensorInfo)];
87
} Data;
85
} Data;
88
int Max[MAXRDG];
86
int Max[MAXRDG];
89
int Min[MAXRDG];
87
int Min[MAXRDG];
90
int PLXItems;
88
int PLXItems;
91
 
89
 
Line 107... Line 105...
107
 
105
 
108
/* USER CODE END PFP */
106
/* USER CODE END PFP */
109
 
107
 
110
/* USER CODE BEGIN 0 */
108
/* USER CODE BEGIN 0 */
111
/* dummy function */
109
/* dummy function */
112
void
-
 
113
_init (void)
110
void _init(void)
114
{
111
{
115
 
112
 
116
}
113
}
117
// the dial is the switch number we are using.
114
// the dial is the switch number we are using.
118
// suppress is the ItemIndex we wish to suppress on this display
115
// suppress is the ItemIndex we wish to suppress on this display
119
int
-
 
120
DisplayCurrent (int dial, int suppress)
116
int DisplayCurrent(int dial, int suppress)
121
{
117
{
122
  char buff[10];
118
        char buff[10];
123
  int i;
119
        int i;
124
  int rc;
120
        int rc;
125
  select_display (dial); // pick the display we are using
121
        select_display(dial); // pick the display we are using
126
  int ItemIndex = dial_pos[dial] / 4;
122
        int ItemIndex = dial_pos[dial] / 4;
127
 
-
 
128
  // wrap around count if dial too far to the right
-
 
129
  if (ItemIndex >= PLXItems)
-
 
130
    {
-
 
131
      dial_pos[dial] = 0;
-
 
132
      ItemIndex = 0;
-
 
133
    }
-
 
134
  if (ItemIndex < 0)
-
 
135
    {
-
 
136
      ItemIndex = PLXItems - 1;
-
 
137
      dial_pos[dial] = (PLXItems - 1) * 4;
-
 
138
    }
-
 
139
 
-
 
140
  // check for item suppression
-
 
141
  if (ItemIndex == suppress)
-
 
142
    {
-
 
143
      dial1[dial] = -1;
-
 
144
      OldObservation[dial] = -1;
-
 
145
      OldObservationIndex[dial] = -1;
-
 
146
 
-
 
147
      clearDisplay ();
-
 
148
      display ();
-
 
149
      return -1; // we suppressed this display
-
 
150
    }
-
 
151
  // do not try to convert if no items in buffer
-
 
152
  if (PLXItems > 0)
-
 
153
    {
-
 
154
      int DataVal = ConvPLX (Data.Sensor[ItemIndex].ReadingH,
-
 
155
                             Data.Sensor[ItemIndex].ReadingL); // data reading
-
 
156
      int Observation = ConvPLX (Data.Sensor[ItemIndex].AddrH,
-
 
157
                                 Data.Sensor[ItemIndex].AddrL);
-
 
158
      int ObservationIndex = ConvPLX (0, Data.Sensor[ItemIndex].Instance);
-
 
159
      // now to convert the readings and format strings
-
 
160
      // find out limits
-
 
161
      char * msg;
-
 
162
      int len;
-
 
163
 
123
 
164
      // if the user presses the dial then reset min/max to current value
124
        // wrap around count if dial too far to the right
165
      if (push_pos[dial] == 1)
125
        if (ItemIndex >= PLXItems)
166
        {
126
        {
167
          Max[ItemIndex] = DataVal;
127
                dial_pos[dial] = 0;
168
          Min[ItemIndex] = DataVal; // 12 bit max value
128
                ItemIndex = 0;
169
        }
129
        }
170
 
-
 
171
      if (Observation < PLX_MAX_OBS)
130
        if (ItemIndex < 0)
172
        {
131
        {
173
          if (Observation != OldObservation[dial]
-
 
174
              || ObservationIndex != OldObservationIndex[dial])
-
 
175
            {
-
 
176
 
-
 
177
              dial_timer[dial] = DialTimeout;
-
 
178
 
-
 
179
              dial1[dial] = -1;
-
 
180
              clearDisplay ();
-
 
181
              dial_draw_scale (DisplayInfo[Observation].Low,
-
 
182
                               DisplayInfo[Observation].High, 12, 1,
-
 
183
                               DisplayInfo[Observation].TickScale);
-
 
184
 
-
 
185
              msg = DisplayInfo[Observation].name;
-
 
186
              len = 7;
-
 
187
              int len1 = ObservationIndex > 0 ? len - 1 : len;
-
 
188
              for (i = 0; i < len1 && msg[i]; i++)
-
 
189
                {
-
 
190
                  buff[i] = msg[i];
132
                ItemIndex = PLXItems - 1;
191
                }
-
 
192
              if (ObservationIndex > 0 && i < len)
-
 
193
                {
-
 
194
                  buff[i++] = ObservationIndex + '1';
133
                dial_pos[dial] = (PLXItems - 1) * 4;
195
                }
134
        }
196
 
135
 
-
 
136
        // check for item suppression
-
 
137
        if (ItemIndex == suppress)
-
 
138
        {
-
 
139
                dial1[dial] = -1;
-
 
140
                OldObservation[dial] = -1;
-
 
141
                OldObservationIndex[dial] = -1;
-
 
142
 
-
 
143
                clearDisplay();
-
 
144
                display();
-
 
145
                return -1; // we suppressed this display
-
 
146
        }
-
 
147
        // do not try to convert if no items in buffer
-
 
148
        if (PLXItems > 0)
-
 
149
        {
-
 
150
                int DataVal = ConvPLX(Data.Sensor[ItemIndex].ReadingH,
-
 
151
                                Data.Sensor[ItemIndex].ReadingL); // data reading
-
 
152
                int Observation = ConvPLX(Data.Sensor[ItemIndex].AddrH,
-
 
153
                                Data.Sensor[ItemIndex].AddrL);
197
              print_large_string (buff, 64 - i * 4, 48, i); // this prints spaces for \0 at end of string
154
                int ObservationIndex = ConvPLX(0, Data.Sensor[ItemIndex].Instance);
-
 
155
                // now to convert the readings and format strings
-
 
156
                // find out limits
-
 
157
                char * msg;
-
 
158
                int len;
198
 
159
 
199
              // print suffix if present.
160
                // if the user presses the dial then reset min/max to current value
200
              font_gotoxy (15, 4);
161
                if (push_pos[dial] == 1)
201
              int i = 0;
-
 
202
              while (DisplayInfo[Observation].suffix[i])
-
 
203
                {
162
                {
-
 
163
                        Max[ItemIndex] = DataVal;
204
                  font_putchar (DisplayInfo[Observation].suffix[i++]);
164
                        Min[ItemIndex] = DataVal; // 12 bit max value
205
                }
165
                }
206
 
166
 
207
              OldObservation[dial] = Observation;
167
                if (Observation < PLX_MAX_OBS)
208
              OldObservationIndex[dial] = ObservationIndex;
-
 
209
              //
-
 
210
              display ();
-
 
211
 
-
 
212
            }
-
 
213
          else
-
 
214
            {
-
 
215
              // check for timer timeout on consistent timer
-
 
216
              if (dial_timer[dial])
-
 
217
                {
168
                {
-
 
169
                        if (Observation != OldObservation[dial]
-
 
170
                                        || ObservationIndex != OldObservationIndex[dial])
-
 
171
                        {
-
 
172
 
-
 
173
                                dial_timer[dial] = DialTimeout;
-
 
174
 
218
                  dial_timer[dial]--;
175
                                dial1[dial] = -1;
-
 
176
                                clearDisplay();
-
 
177
                                dial_draw_scale(DisplayInfo[Observation].Low,
-
 
178
                                                DisplayInfo[Observation].High, 12, 1,
-
 
179
                                                DisplayInfo[Observation].TickScale);
-
 
180
 
-
 
181
                                msg = DisplayInfo[Observation].name;
-
 
182
                                len = 7;
-
 
183
                                int len1 = ObservationIndex > 0 ? len - 1 : len;
-
 
184
                                for (i = 0; i < len1 && msg[i]; i++)
-
 
185
                                {
-
 
186
                                        buff[i] = msg[i];
-
 
187
                                }
-
 
188
                                if (ObservationIndex > 0 && i < len)
-
 
189
                                {
-
 
190
                                        buff[i++] = ObservationIndex + '1';
-
 
191
                                }
-
 
192
 
-
 
193
                                print_large_string(buff, 64 - i * 4, 48, i); // this prints spaces for \0 at end of string
-
 
194
 
-
 
195
                                // print suffix if present.
-
 
196
                                font_gotoxy(15, 4);
-
 
197
                                int i = 0;
-
 
198
                                while (DisplayInfo[Observation].suffix[i])
-
 
199
                                {
-
 
200
                                        font_putchar(DisplayInfo[Observation].suffix[i++]);
-
 
201
                                }
-
 
202
 
-
 
203
                                OldObservation[dial] = Observation;
-
 
204
                                OldObservationIndex[dial] = ObservationIndex;
-
 
205
                                //
-
 
206
                                display();
219
 
207
 
220
                  if (dial_timer[dial] == 0)
208
                        }
221
                    {
209
                        else
222
                      uint16_t curr_val = dial_pos[dial];
-
 
223
                      rc = EE_ReadVariable (VirtAddVarTab[dial], &curr_val);
-
 
224
                      if ((rc != 0) || (curr_val != dial_pos[dial]))
-
 
225
                        {
210
                        {
-
 
211
                                // check for timer timeout on consistent timer
-
 
212
                                if (dial_timer[dial])
-
 
213
                                {
226
                          //__disable_irq();
214
                                        dial_timer[dial]--;
-
 
215
 
227
                          HAL_FLASH_Unlock ();
216
                                        if (dial_timer[dial] == 0)
-
 
217
                                        {
-
 
218
                                                uint16_t curr_val = dial_pos[dial];
-
 
219
 
-
 
220
                                                uint8_t * ptr = (uint8_t *) (&dial_nvram[dial]);
-
 
221
                                                WriteNVRAM(ptr, curr_val & 0xFF);
-
 
222
                                                WriteNVRAM(ptr + 1, curr_val >> 8);
228
 
223
 
229
                          rc = EE_WriteVariable (VirtAddVarTab[dial],
-
 
230
                                                 dial_pos[dial]);
224
                                        }
231
                          HAL_FLASH_Lock ();
-
 
232
                          //__enable_irq();
225
                                }
233
                        }
226
                        }
234
                    }
227
 
235
                }
228
                }
236
 
229
 
-
 
230
                double max_rdg;
-
 
231
                double min_rdg;
-
 
232
                double cur_rdg;
-
 
233
                int int_rdg;
-
 
234
                int int_max;
-
 
235
                int int_min;
-
 
236
 
-
 
237
                max_rdg = ConveriMFDRaw2Data(Observation,
-
 
238
                                DisplayInfo[Observation].Units, Max[ItemIndex]);
-
 
239
                min_rdg = ConveriMFDRaw2Data(Observation,
-
 
240
                                DisplayInfo[Observation].Units, Min[ItemIndex]);
-
 
241
                cur_rdg = ConveriMFDRaw2Data(Observation,
-
 
242
                                DisplayInfo[Observation].Units, DataVal);
-
 
243
 
-
 
244
                int dp_pos;  // where to print the decimal place
-
 
245
                float scale = 1.0;
-
 
246
                switch (DisplayInfo[Observation].DP)
-
 
247
                {
-
 
248
                case 0:
-
 
249
                        scale = 1.0;
-
 
250
                        dp_pos = 100;
-
 
251
                        break;
-
 
252
                case 1:
-
 
253
                        scale = 10.0;
-
 
254
                        dp_pos = 1;
-
 
255
                        break;
-
 
256
                case 2:
-
 
257
                        scale = 100.0;
-
 
258
                        dp_pos = 2;
-
 
259
                        break;
237
            }
260
                }
-
 
261
                int_rdg = (int) (cur_rdg * scale);
-
 
262
                int_max = (int) (max_rdg * scale);
-
 
263
                int_min = (int) (min_rdg * scale);
-
 
264
 
-
 
265
                cur_rdg -= DisplayInfo[Observation].Low;
-
 
266
                cur_rdg =
-
 
267
                                100 * cur_rdg
-
 
268
                                                / (DisplayInfo[Observation].High
-
 
269
                                                                - DisplayInfo[Observation].Low);
-
 
270
 
-
 
271
                dial0[dial] = (int) cur_rdg;
238
 
272
 
239
          double max_rdg;
-
 
240
          double min_rdg;
-
 
241
          double cur_rdg;
-
 
242
          int int_rdg;
-
 
243
          int int_max;
-
 
244
          int int_min;
-
 
245
 
-
 
246
          max_rdg = ConveriMFDRaw2Data (Observation,
-
 
247
                                        DisplayInfo[Observation].Units,
-
 
248
                                        Max[ItemIndex]);
-
 
249
          min_rdg = ConveriMFDRaw2Data (Observation,
-
 
250
                                        DisplayInfo[Observation].Units,
-
 
251
                                        Min[ItemIndex]);
-
 
252
          cur_rdg = ConveriMFDRaw2Data (Observation,
-
 
253
                                        DisplayInfo[Observation].Units,
-
 
254
                                        DataVal);
-
 
255
 
-
 
256
          int dp_pos;  // where to print the decimal place
-
 
257
          float scale = 1.0;
-
 
258
          switch (DisplayInfo[Observation].DP)
-
 
259
            {
-
 
260
            case 0:
-
 
261
              scale = 1.0;
-
 
262
              dp_pos = 100;
-
 
263
              break;
-
 
264
            case 1:
-
 
265
              scale = 10.0;
-
 
266
              dp_pos = 1;
-
 
267
              break;
-
 
268
            case 2:
-
 
269
              scale = 100.0;
-
 
270
              dp_pos = 2;
-
 
271
              break;
-
 
272
            }
-
 
273
          int_rdg = (int) (cur_rdg * scale);
-
 
274
          int_max = (int) (max_rdg * scale);
-
 
275
          int_min = (int) (min_rdg * scale);
-
 
276
 
-
 
277
          cur_rdg -= DisplayInfo[Observation].Low;
-
 
278
          cur_rdg = 100 * cur_rdg
-
 
279
              / (DisplayInfo[Observation].High - DisplayInfo[Observation].Low);
-
 
280
 
-
 
281
          dial0[dial] = (int) cur_rdg;
-
 
282
 
-
 
283
          /* old needle un-draw */
273
                /* old needle un-draw */
284
          if (dial1[dial] >= 0)
274
                if (dial1[dial] >= 0)
285
            {
275
                {
286
              dial_draw_needle (dial1[dial]);
276
                        dial_draw_needle(dial1[dial]);
287
            }
277
                }
288
          dial_draw_needle (dial0[dial]);
278
                dial_draw_needle(dial0[dial]);
289
          // print value overlaid by needle
279
                // print value overlaid by needle
290
          // this is actual reading
280
                // this is actual reading
291
          print_digits (30, 30, 5, dp_pos, int_rdg);
281
                print_digits(30, 30, 5, dp_pos, int_rdg);
292
          font_gotoxy (0, 0);
282
                font_gotoxy(0, 0);
293
          font_digits (5, dp_pos, int_min);
283
                font_digits(5, dp_pos, int_min);
294
 
284
 
295
          font_gotoxy (0, 1);
285
                font_gotoxy(0, 1);
296
          font_puts ("Min");
286
                font_puts("Min");
297
 
287
 
298
          font_gotoxy (15, 0);
288
                font_gotoxy(15, 0);
299
          font_digits (5, dp_pos, int_max);
289
                font_digits(5, dp_pos, int_max);
300
          font_gotoxy (18, 1);
290
                font_gotoxy(18, 1);
301
          font_puts ("Max");
291
                font_puts("Max");
302
 
292
 
303
          dial1[dial] = dial0[dial];
293
                dial1[dial] = dial0[dial];
304
 
294
 
305
          display ();
295
                display();
306
 
296
 
307
        }
297
        }
308
    }
-
 
309
  return ItemIndex;
298
return ItemIndex;
310
}
299
}
311
/* USER CODE END 0 */
300
/* USER CODE END 0 */
312
 
301
 
313
int main(void)
302
int main(void)
314
{
303
{
315
 
304
 
316
  /* USER CODE BEGIN 1 */
305
  /* USER CODE BEGIN 1 */
317
 
306
 
318
  GPIO_InitTypeDef GPIO_InitStruct;
307
GPIO_InitTypeDef GPIO_InitStruct;
319
 
308
 
320
  __HAL_RCC_SPI1_CLK_ENABLE()
309
__HAL_RCC_SPI1_CLK_ENABLE()
321
  ;
310
;
322
  __HAL_RCC_USART1_CLK_ENABLE()
311
__HAL_RCC_USART1_CLK_ENABLE()
323
  ; // PLX main port
312
; // PLX main port
324
  __HAL_RCC_USART2_CLK_ENABLE()
313
__HAL_RCC_USART2_CLK_ENABLE()
325
  ; // debug port
314
; // debug port
326
  __HAL_RCC_USART3_CLK_ENABLE (); // Bluetooth port
315
__HAL_RCC_USART3_CLK_ENABLE ()
-
 
316
; // Bluetooth port
327
 
317
 
328
  /* USER CODE END 1 */
318
  /* USER CODE END 1 */
329
 
319
 
330
  /* MCU Configuration----------------------------------------------------------*/
320
  /* MCU Configuration----------------------------------------------------------*/
331
 
321
 
Line 342... Line 332...
342
  MX_USART2_UART_Init();
332
  MX_USART2_UART_Init();
343
  MX_USART3_UART_Init();
333
  MX_USART3_UART_Init();
344
 
334
 
345
  /* USER CODE BEGIN 2 */
335
  /* USER CODE BEGIN 2 */
346
 
336
 
347
  /* Turn on USART1 IRQ */
337
/* Turn on USART1 IRQ */
348
  HAL_NVIC_SetPriority (USART1_IRQn, 2, 0);
338
HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);
349
  HAL_NVIC_EnableIRQ (USART1_IRQn);
339
HAL_NVIC_EnableIRQ(USART1_IRQn);
350
 
-
 
351
  /* Turn on USART2 IRQ  */
-
 
352
  HAL_NVIC_SetPriority (USART2_IRQn, 4, 0);
-
 
353
  HAL_NVIC_EnableIRQ (USART2_IRQn);
-
 
354
 
-
 
355
  /* turn on USART3 IRQ */
-
 
356
  HAL_NVIC_SetPriority (USART3_IRQn, 4, 0);
-
 
357
  HAL_NVIC_EnableIRQ (USART3_IRQn);
-
 
358
 
340
 
359
  /* setup the USART control blocks */
341
/* Turn on USART2 IRQ  */
360
  init_usart_ctl (&uc1, huart1.Instance);
342
HAL_NVIC_SetPriority(USART2_IRQn, 4, 0);
361
  init_usart_ctl (&uc2, huart2.Instance);
343
HAL_NVIC_EnableIRQ(USART2_IRQn);
362
  init_usart_ctl (&uc3, huart3.Instance);
-
 
363
 
344
 
364
  EnableSerialRxInterrupt (&uc1);
345
/* turn on USART3 IRQ */
365
  EnableSerialRxInterrupt (&uc2);
346
HAL_NVIC_SetPriority(USART3_IRQn, 4, 0);
366
  EnableSerialRxInterrupt (&uc3);
347
HAL_NVIC_EnableIRQ(USART3_IRQn);
367
 
348
 
368
  /* Unlock the Flash to enable the flash control register access *************/
349
/* setup the USART control blocks */
-
 
350
init_usart_ctl(&uc1, huart1.Instance);
-
 
351
init_usart_ctl(&uc2, huart2.Instance);
369
  HAL_FLASH_Unlock ();
352
init_usart_ctl(&uc3, huart3.Instance);
370
 
353
 
371
  //__disable_irq();
354
EnableSerialRxInterrupt(&uc1);
372
  EE_Init ();
355
EnableSerialRxInterrupt(&uc2);
373
  //__enable_irq();
356
EnableSerialRxInterrupt(&uc3);
374
 
357
 
375
  HAL_FLASH_Lock ();
-
 
376
 
358
 
377
  InitSwitches ();
359
InitSwitches();
378
 
360
 
379
  int i;
361
int i;
380
  uint16_t rc;
362
uint16_t rc;
381
  for (i = 0; i < 2; i++)
363
for (i = 0; i < 2; i++)
382
    {
-
 
383
      uint16_t val;
-
 
384
 
-
 
385
      uint16_t rc = EE_ReadVariable (VirtAddVarTab[i], &val);
-
 
386
 
-
 
387
      if (rc == 0)
-
 
388
        {
364
{
389
          dial_pos[i] = val;
365
        dial_pos[i] = dial_nvram[i];
390
        }
-
 
391
      else
-
 
392
        {
-
 
393
          break;
-
 
394
        }
366
}
395
    }
-
 
396
 
367
 
397
  ap_init (); // set up the approximate math library
368
ap_init(); // set up the approximate math library
398
 
369
 
399
  int disp;
370
int disp;
400
 
371
 
401
  ssd1306_begin (1, 0);
372
ssd1306_begin(1, 0);
402
  dial_origin (64, 60);
373
dial_origin(64, 60);
403
  dial_size (60);
374
dial_size(60);
404
 
375
 
405
  /* reset the display timeout, latch on power from accessories */
376
/* reset the display timeout, latch on power from accessories */
406
  Latch_Timer = IGNITION_OFF_TIMEOUT;
377
Latch_Timer = IGNITION_OFF_TIMEOUT;
407
  HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET);
378
HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET);
408
 
379
 
409
  for (disp = 0; disp < 2; disp++)
380
for (disp = 0; disp < 2; disp++)
410
    {
381
{
411
      select_display (disp);
382
        select_display(disp);
412
      clearDisplay ();
383
        clearDisplay();
413
      dim (0);
384
        dim(0);
414
      //font_puts(
385
        //font_puts(
415
      //                "Hello world !!\rThis text is a test of the text rendering library in a 5*7 font");
386
        //              "Hello world !!\rThis text is a test of the text rendering library in a 5*7 font");
416
 
387
 
417
      dial_draw_scale (0, 10, 12, 5, 1);
388
        dial_draw_scale(0, 10, 12, 5, 1);
418
      char buffer[] = "Display  ";
389
        char buffer[] = "Display  ";
419
      buffer[8] = disp + '1';
390
        buffer[8] = disp + '1';
420
      print_large_string (buffer, 20, 30, 9);
391
        print_large_string(buffer, 20, 30, 9);
421
 
392
 
422
      display ();
393
        display();
423
 
394
 
424
    }
395
}
425
 
396
 
426
  /* USER CODE END 2 */
397
  /* USER CODE END 2 */
427
 
398
 
428
  /* Infinite loop */
399
  /* Infinite loop */
429
  /* USER CODE BEGIN WHILE */
400
  /* USER CODE BEGIN WHILE */
430
  uint32_t Ticks = HAL_GetTick () + 100;
401
uint32_t Ticks = HAL_GetTick() + 100;
431
 
402
 
432
  /* while ignition is on, keep resetting power latch timer */
403
/* while ignition is on, keep resetting power latch timer */
433
  if (HAL_GPIO_ReadPin (IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET)
404
if (HAL_GPIO_ReadPin(IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET)
434
    {
405
{
435
      Latch_Timer = HAL_GetTick () + IGNITION_OFF_TIMEOUT;
406
        Latch_Timer = HAL_GetTick() + IGNITION_OFF_TIMEOUT;
436
    }
407
}
437
  else
408
else
438
    {
409
{
439
      /* if the ignition has been off for a while, then turn off power */
410
        /* if the ignition has been off for a while, then turn off power */
440
      if (HAL_GetTick () > Latch_Timer)
411
        if (HAL_GetTick() > Latch_Timer)
441
        {
412
        {
442
          HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin,
413
                HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin,
443
                             GPIO_PIN_RESET);
414
                                GPIO_PIN_RESET);
444
        }
415
        }
445
    }
416
}
446
 
417
 
447
  uint32_t timeout = 0;  //
418
uint32_t timeout = 0;  //
448
  // PLX decoder protocols
419
// PLX decoder protocols
449
  char PLXPacket = 0;
420
char PLXPacket = 0;
450
  for (i = 0; i < MAXRDG; i++)
421
for (i = 0; i < MAXRDG; i++)
451
    {
422
{
452
      Max[i] = 0;
423
        Max[i] = 0;
453
      Min[i] = 0xFFF; // 12 bit max value
424
        Min[i] = 0xFFF; // 12 bit max value
454
    }
425
}
455
 
426
 
456
  int PLXPtr = 0;
427
int PLXPtr = 0;
457
 
428
 
458
  while (1)
429
while (1)
459
    {
430
{
460
// poll switche
431
// poll switche
461
      HandleSwitches ();
432
        HandleSwitches();
462
// Handle the bluetooth pairing function by pressing both buttons.
433
// Handle the bluetooth pairing function by pressing both buttons.
463
      if ((push_pos[0] == 1) && (push_pos[1] == 1))
434
        if ((push_pos[0] == 1) && (push_pos[1] == 1))
464
        {
435
        {
465
          HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_SET);
436
                HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_SET);
466
        }
437
        }
467
      else
438
        else
468
        {
439
        {
469
          HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin,
440
                HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_RESET);
470
                             GPIO_PIN_RESET);
-
 
471
 
441
 
472
        }
442
        }
473
 
443
 
474
      uint16_t cc = SerialCharsReceived (&uc1);
444
        uint16_t cc = SerialCharsReceived(&uc1);
475
      int chr;
445
        int chr;
476
      if (cc == 0)
446
        if (cc == 0)
477
        {
447
        {
478
          timeout++;
448
                timeout++;
479
          if (timeout % 1000 == 0)
449
                if (timeout % 1000 == 0)
480
            {
450
                {
481
              PutCharSerial (&uc3, '+');
451
                        PutCharSerial(&uc3, '+');
482
            }
452
                }
483
          if (timeout > 60000)
453
                if (timeout > 60000)
484
            {
454
                {
485
              // do turn off screen
455
                        // do turn off screen
486
            }
456
                }
487
 
457
 
488
        }
458
        }
489
      for (chr = 0; chr < cc; chr++)
459
        for (chr = 0; chr < cc; chr++)
490
        {
460
        {
491
          char c = GetCharSerial (&uc1);
461
                char c = GetCharSerial(&uc1);
492
          timeout = 0;
462
                timeout = 0;
493
 
463
 
494
          if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
464
                if (c == PLX_Start) // at any time if the start byte appears, reset the pointers
495
            {
-
 
496
              PLXPtr = 0;    // reset the pointer
-
 
497
              PLXPacket = 1;
-
 
498
            }
-
 
499
          else if (c == PLX_Stop)
-
 
500
            {
-
 
501
              if (PLXPacket)
-
 
502
                {
465
                {
503
                  // we can now decode the selected parameter
-
 
504
                  PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total
-
 
505
                  // saturate the rotary switch position
-
 
506
 
-
 
507
                  int DataVal;
-
 
508
                  // process min/max
-
 
509
                  for (i = 0; i < PLXItems; i++)
-
 
510
                    {
-
 
511
                      // Send item to BT
466
                        PLXPtr = 0;    // reset the pointer
512
                      uint16_t addr = ConvPLX (Data.Sensor[i].AddrH,
-
 
513
                                               Data.Sensor[i].AddrL);
-
 
514
                      uint8_t inst = Data.Sensor[i].Instance;
-
 
515
                      uint16_t reading = ConvPLX (Data.Sensor[i].ReadingH,
-
 
516
                                                  Data.Sensor[i].ReadingL);
-
 
517
 
-
 
518
                      char outbuff[100];
-
 
519
                      int cnt = simple_sprintf (outbuff, "%d,%d,%d\n", addr, inst,
-
 
520
                                         reading);
-
 
521
                      int i;
467
                        PLXPacket = 1;
522
                      for (i = 0; i < cnt; i++)
-
 
523
 
-
 
524
                        {
-
 
525
                          PutCharSerial (&uc3, outbuff[i]);
-
 
526
                        }
468
                }
527
                      DataVal = ConvPLX (Data.Sensor[i].ReadingH,
-
 
528
                                         Data.Sensor[i].ReadingL);
-
 
529
                      if (DataVal > Max[i])
469
                else if (c == PLX_Stop)
530
                        {
470
                {
531
                          Max[i] = DataVal;
471
                        if (PLXPacket)
532
                        }
-
 
533
                      if (DataVal < Min[i])
-
 
534
                        {
472
                        {
-
 
473
                                // we can now decode the selected parameter
-
 
474
                                PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total
-
 
475
                                // saturate the rotary switch position
-
 
476
 
-
 
477
                                int DataVal;
-
 
478
                                // process min/max
-
 
479
                                for (i = 0; i < PLXItems; i++)
-
 
480
                                {
-
 
481
                                        // Send item to BT
-
 
482
                                        uint16_t addr = ConvPLX(Data.Sensor[i].AddrH,
-
 
483
                                                        Data.Sensor[i].AddrL);
-
 
484
                                        uint8_t inst = Data.Sensor[i].Instance;
-
 
485
                                        uint16_t reading = ConvPLX(Data.Sensor[i].ReadingH,
-
 
486
                                                        Data.Sensor[i].ReadingL);
-
 
487
 
-
 
488
                                        char outbuff[100];
-
 
489
                                        int cnt = small_sprintf(outbuff, "%d,%d,%d\n", addr, inst,
-
 
490
                                                        reading);
-
 
491
                                        int i;
-
 
492
                                        for (i = 0; i < cnt; i++)
-
 
493
 
-
 
494
                                        {
-
 
495
                                                PutCharSerial(&uc3, outbuff[i]);
-
 
496
                                        }
-
 
497
                                        DataVal = ConvPLX(Data.Sensor[i].ReadingH,
-
 
498
                                                        Data.Sensor[i].ReadingL);
-
 
499
                                        if (DataVal > Max[i])
-
 
500
                                        {
-
 
501
                                                Max[i] = DataVal;
-
 
502
                                        }
-
 
503
                                        if (DataVal < Min[i])
-
 
504
                                        {
535
                          Min[i] = DataVal;
505
                                                Min[i] = DataVal;
-
 
506
                                        }
-
 
507
                                }
-
 
508
 
-
 
509
                                // now to display the information
-
 
510
                                int suppress = DisplayCurrent(0, -1);
-
 
511
                                DisplayCurrent(1, suppress);
536
                        }
512
                        }
537
                    }
513
                        PLXPtr = 0;
-
 
514
                        PLXPacket = 0;
538
 
515
                }
539
                  // now to display the information
516
                else if (c > PLX_Stop) // illegal char, restart reading
-
 
517
                {
-
 
518
                        PLXPacket = 0;
-
 
519
                        PLXPtr = 0;
-
 
520
                }
540
                  int suppress = DisplayCurrent (0, -1);
521
                else if (PLXPtr < sizeof(Data.Bytes))
-
 
522
                {
541
                  DisplayCurrent (1, suppress);
523
                        Data.Bytes[PLXPtr++] = c;
542
                }
524
                }
543
              PLXPtr = 0;
-
 
544
              PLXPacket = 0;
-
 
545
            }
-
 
546
          else if (c > PLX_Stop) // illegal char, restart reading
-
 
547
            {
-
 
548
              PLXPacket = 0;
-
 
549
              PLXPtr = 0;
-
 
550
            }
-
 
551
          else if (PLXPtr < sizeof(Data.Bytes))
-
 
552
            {
-
 
553
              Data.Bytes[PLXPtr++] = c;
-
 
554
            }
-
 
555
        }
525
        }
556
 
526
 
557
      HAL_Delay (1);
527
        HAL_Delay(1);
558
    }
528
}
559
  /* USER CODE END WHILE */
529
  /* USER CODE END WHILE */
560
 
530
 
561
  /* USER CODE BEGIN 3 */
531
  /* USER CODE BEGIN 3 */
562
 
532
 
563
  /* USER CODE END 3 */
533
  /* USER CODE END 3 */
Line 703... Line 673...
703
  __HAL_RCC_GPIOA_CLK_ENABLE();
673
  __HAL_RCC_GPIOA_CLK_ENABLE();
704
  __HAL_RCC_GPIOC_CLK_ENABLE();
674
  __HAL_RCC_GPIOC_CLK_ENABLE();
705
  __HAL_RCC_GPIOB_CLK_ENABLE();
675
  __HAL_RCC_GPIOB_CLK_ENABLE();
706
 
676
 
707
  /*Configure GPIO pin Output Level */
677
  /*Configure GPIO pin Output Level */
708
  HAL_GPIO_WritePin(GPIOA, SPI1_NSS1_Pin|SPI1CD_Pin|BT_BUTTON_Pin, GPIO_PIN_RESET);
678
  HAL_GPIO_WritePin(GPIOA, SPI_NSS1_Pin|SPI1CD_Pin|BT_BUTTON_Pin, GPIO_PIN_RESET);
709
 
679
 
710
  /*Configure GPIO pin Output Level */
680
  /*Configure GPIO pin Output Level */
711
  HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin|SPI_NSS2_Pin|POWER_LATCH_Pin|USB_PWR_Pin, GPIO_PIN_RESET);
681
  HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin|SPI_NSS2_Pin|POWER_LATCH_Pin|USB_PWR_Pin, GPIO_PIN_RESET);
712
 
682
 
713
  /*Configure GPIO pins : SPI1_NSS1_Pin SPI1CD_Pin BT_BUTTON_Pin */
683
  /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin BT_BUTTON_Pin */
714
  GPIO_InitStruct.Pin = SPI1_NSS1_Pin|SPI1CD_Pin|BT_BUTTON_Pin;
684
  GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin|BT_BUTTON_Pin;
715
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
685
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
716
  GPIO_InitStruct.Pull = GPIO_NOPULL;
686
  GPIO_InitStruct.Pull = GPIO_NOPULL;
717
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
687
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
718
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
688
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
719
 
689
 
Line 728... Line 698...
728
  GPIO_InitStruct.Pin = SW1_PUSH_Pin;
698
  GPIO_InitStruct.Pin = SW1_PUSH_Pin;
729
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
699
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
730
  GPIO_InitStruct.Pull = GPIO_NOPULL;
700
  GPIO_InitStruct.Pull = GPIO_NOPULL;
731
  HAL_GPIO_Init(SW1_PUSH_GPIO_Port, &GPIO_InitStruct);
701
  HAL_GPIO_Init(SW1_PUSH_GPIO_Port, &GPIO_InitStruct);
732
 
702
 
733
  /*Configure GPIO pins : SW_I_Pin SW1_Q_Pin SW2_PUSH_Pin */
703
  /*Configure GPIO pins : SW1_I_Pin SW1_Q_Pin SW2_PUSH_Pin */
734
  GPIO_InitStruct.Pin = SW_I_Pin|SW1_Q_Pin|SW2_PUSH_Pin;
704
  GPIO_InitStruct.Pin = SW1_I_Pin|SW1_Q_Pin|SW2_PUSH_Pin;
735
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
705
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
736
  GPIO_InitStruct.Pull = GPIO_NOPULL;
706
  GPIO_InitStruct.Pull = GPIO_NOPULL;
737
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
707
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
738
 
708
 
739
  /*Configure GPIO pins : SW2_I_Pin SW2_Q_Pin IGNITION_Pin */
709
  /*Configure GPIO pins : SW2_I_Pin SW2_Q_Pin IGNITION_Pin */
Line 754... Line 724...
754
  * @retval None
724
  * @retval None
755
  */
725
  */
756
void Error_Handler(void)
726
void Error_Handler(void)
757
{
727
{
758
  /* USER CODE BEGIN Error_Handler */
728
  /* USER CODE BEGIN Error_Handler */
759
  /* User can add his own implementation to report the HAL error return state */
729
/* User can add his own implementation to report the HAL error return state */
760
  while (1)
730
while (1)
761
    {
731
{
762
    }
732
}
763
  /* USER CODE END Error_Handler */
733
  /* USER CODE END Error_Handler */
764
}
734
}
765
 
735
 
766
#ifdef USE_FULL_ASSERT
736
#ifdef USE_FULL_ASSERT
767
 
737
 
Line 773... Line 743...
773
   * @retval None
743
   * @retval None
774
   */
744
   */
775
void assert_failed(uint8_t* file, uint32_t line)
745
void assert_failed(uint8_t* file, uint32_t line)
776
{
746
{
777
  /* USER CODE BEGIN 6 */
747
  /* USER CODE BEGIN 6 */
778
    /* User can add his own implementation to report the file name and line number,
748
/* User can add his own implementation to report the file name and line number,
779
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
749
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
780
  /* USER CODE END 6 */
750
  /* USER CODE END 6 */
781
 
751
 
782
}
752
}
783
 
753
 
784
#endif
754
#endif