Subversion Repositories ChibiGauge

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 6
Line 33... Line 33...
33
//#include "test.h"
33
//#include "test.h"
34
#include "shell.h"
34
#include "shell.h"
35
#include "evtimer.h"
35
#include "evtimer.h"
36
#include "chprintf.h"
36
#include "chprintf.h"
37
 
37
 
38
 
-
 
39
 
-
 
40
#if (HAL_USE_SERIAL_USB == TRUE)
38
#if (HAL_USE_SERIAL_USB == TRUE)
41
#include "usbcfg.h"
39
#include "usbcfg.h"
42
#endif
40
#endif
43
 
41
 
44
 
-
 
45
#include "useAdc.h"
42
#include "useAdc.h"
46
 
43
 
47
 
-
 
48
#include "shellCmds.h"
44
#include "shellCmds.h"
49
 
45
 
-
 
46
#include "ap_math.h"
-
 
47
#include "spiInterface.h"
-
 
48
#include "SSD1306.h"
-
 
49
#include "font.h"
-
 
50
 
-
 
51
#include "vl53l0x_api.h"
-
 
52
#include "vl53l0x_platform.h"
-
 
53
#include "required_version.h"
50
 
54
 
-
 
55
 
51
static MUTEX_DECL(mutexDisplay);
56
static MUTEX_DECL( mutexDisplay);
-
 
57
 
-
 
58
unsigned lidar = 0;
52
 
59
 
53
/*===========================================================================*/
60
/*===========================================================================*/
54
/* Command line related.                                                     */
61
/* Command line related.                                                     */
55
/*===========================================================================*/
62
/*===========================================================================*/
56
 
63
 
57
#define SHELL_WA_SIZE   THD_WORKING_AREA_SIZE(2048)
64
#define SHELL_WA_SIZE   THD_WORKING_AREA_SIZE(1024)
58
#define TEST_WA_SIZE    THD_WORKING_AREA_SIZE(256)
65
#define TEST_WA_SIZE    THD_WORKING_AREA_SIZE(256)
59
 
66
 
60
 
-
 
61
 
-
 
62
static const ShellConfig shell_cfg1 = {
67
static const ShellConfig shell_cfg1 = {
63
#if (HAL_USE_SERIAL_USB == TRUE)
68
#if (HAL_USE_SERIAL_USB == TRUE)
64
  (BaseSequentialStream *)&SDU1,
69
                (BaseSequentialStream*) &SDU1,
65
#else
70
#else
66
  (BaseSequentialStream *)&SD1,
71
  (BaseSequentialStream *)&SD1,
67
#endif
72
#endif
68
  shellCommands
73
                shellCommands };
69
};
-
 
70
////////
74
////////
71
// end of shell stuff
75
// end of shell stuff
72
 
76
 
73
uint16_t sampIndex;
77
uint16_t sampIndex;
74
 
78
 
75
void setDriveA(uint8_t bit)
79
void setDriveA(uint8_t bit) {
76
{
-
 
77
        if(bit)
80
        if (bit) {
78
        {
-
 
79
            palSetPad(GPIOA, GPIOA_A1);
81
                palSetPad(GPIOA, GPIOA_A1);
80
           palClearPad(GPIOA, GPIOA_A2);
82
                palClearPad(GPIOA, GPIOA_A2);
81
        }
-
 
82
        else
83
        } else {
83
        {
-
 
84
                palClearPad(GPIOA, GPIOA_A1);
84
                palClearPad(GPIOA, GPIOA_A1);
85
                palSetPad(GPIOA, GPIOA_A2);
85
                palSetPad(GPIOA, GPIOA_A2);
86
        }
86
        }
87
}
87
}
88
 
88
 
89
void setDriveB(uint8_t bit)
89
void setDriveB(uint8_t bit) {
90
{
-
 
91
        if(bit)
90
        if (bit) {
92
        {
-
 
93
           palSetPad(GPIOA, GPIOA_B1);
91
                palSetPad(GPIOA, GPIOA_B1);
94
           palClearPad(GPIOA, GPIOA_B2);
92
                palClearPad(GPIOA, GPIOA_B2);
95
        }
-
 
96
        else
93
        } else {
97
        {
-
 
98
                palClearPad(GPIOA, GPIOA_B1);
94
                palClearPad(GPIOA, GPIOA_B1);
99
                palSetPad(GPIOA, GPIOA_B2);
95
                palSetPad(GPIOA, GPIOA_B2);
100
        }
96
        }
101
}
97
}
102
 
98
 
103
 
99
 
-
 
100
// dial settings
104
volatile int origin = 0;;
101
volatile int origin = 0;
-
 
102
;
105
volatile int target = 630;
103
volatile int target = 0;
106
volatile int count = 0;
104
volatile int count = 0;
107
 
105
 
-
 
106
volatile bool pause;
-
 
107
//
-
 
108
 
-
 
109
 
-
 
110
void setPause(bool p) {
-
 
111
        pause = p;
-
 
112
}
-
 
113
 
108
static THD_WORKING_AREA(waThread1, 512);
114
static THD_WORKING_AREA(waThread1, 512);
109
static msg_t Thread1(void *arg) {
115
static void Thread1(void *arg) {
110
 
116
 
111
        (void) arg;
117
        (void) arg;
112
        unsigned const fast=10 ;
118
        unsigned const fast = 10;
113
        unsigned const slow = 40;
119
        unsigned const slow = 30;
114
        unsigned const range = slow-fast;
120
        unsigned const range = slow - fast;
115
     unsigned  del = fast;
121
        unsigned del = fast;
116
     int step  = 1;
122
        int step = 1;
117
   chRegSetThreadName("Step");
123
        chRegSetThreadName("Step");
118
        while (TRUE) {
124
        while (TRUE) {
119
 
125
 
-
 
126
                while (pause)
-
 
127
                        chThdSleep(1000);
-
 
128
 
120
                switch  (count % 4)
129
                switch (count % 4) {
-
 
130
                case 0:
-
 
131
                        setDriveA(1);
-
 
132
                        setDriveB(0);
-
 
133
                        break;
-
 
134
                case 1:
-
 
135
                        setDriveA(1);
-
 
136
                        setDriveB(1);
121
                {
137
                        break;
-
 
138
                case 2:
122
                case 0: setDriveA(1); setDriveB(0); break;
139
                        setDriveA(0);
123
                case 1: setDriveA(1); setDriveB(1); break;
140
                        setDriveB(1);
-
 
141
                        break;
-
 
142
                case 3:
124
                case 2: setDriveA(0); setDriveB(1); break;
143
                        setDriveA(0);
125
                case 3: setDriveA(0); setDriveB(0); break;
144
                        setDriveB(0);
-
 
145
                        break;
126
                }
146
                }
127
                chThdSleep(del);
-
 
128
                count = count + step;
-
 
129
 
147
 
130
                // all this calculates minimum distance from
148
                // all this calculates minimum distance from
131
                // target or origin
149
                // target or origin
132
        int d1= count-origin;
150
                int d1 = count - origin;
-
 
151
                if (d1 < 0)
133
        if(d1<0) d1 = -d1;
152
                        d1 = -d1;
134
        int d2= count-target;
153
                int d2 = count - target;
-
 
154
                if (d2 < 0)
135
        if(d2<0) d2 = -d2;
155
                        d2 = -d2;
136
        // finally, minimum distance
156
                // finally, minimum distance
137
        int dist  = d1<d2 ? d1 : d2;
157
                int dist = d1 < d2 ? d1 : d2;
138
 
-
 
139
 
158
 
140
        del = fast ;
159
                del = fast;
141
        if(dist < range) // inside lower bound of distance
160
                if (dist < range) // inside lower bound of distance
142
        {
161
                                {
143
                del = slow-dist;
162
                        del = slow - dist;
144
        }
163
                }
-
 
164
                chThdSleep(del);
145
 
165
 
146
                if(count < target ) { step = 1; }
166
                if (count < target) {
-
 
167
                        step = 1;
-
 
168
                }
147
                if(count > target ) { step =-1; }
169
                if (count > target) {
-
 
170
                        step = -1;
-
 
171
                }
148
                if(count == target) { step = 0; }
172
                if (count == target) {
-
 
173
                        step = 0;
-
 
174
                }
-
 
175
                count = count + step;
149
 
176
 
150
        }
177
        }
151
        return 0;
178
 
152
}
179
}
153
 
180
 
154
/*
181
/*
155
 * Command Shell Thread
182
 * Command Shell Thread
156
 */
183
 */
157
static THD_WORKING_AREA(waThread2, 512);
184
static THD_WORKING_AREA(waThread2, 512);
158
static msg_t Thread2(void *arg) {
185
static void Thread2(void *arg) {
159
        thread_t *shelltp = NULL;
186
        thread_t *shelltp = NULL;
160
 
187
 
161
        chRegSetThreadName("Shell ");
188
        chRegSetThreadName("Shell ");
162
/*
189
        /*
163
           * in this demo it just performs
190
         * in this demo it just performs
164
           * a shell respawn upon its termination.
191
         * a shell respawn upon its termination.
165
           */
192
         */
166
          while (true) {
193
        while (true) {
167
            if (!shelltp) {
194
                if (!shelltp) {
168
        #if (HAL_USE_SERIAL_USB == TRUE)
195
#if (HAL_USE_SERIAL_USB == TRUE)
169
              if (SDU1.config->usbp->state == USB_ACTIVE) {
196
                        if (SDU1.config->usbp->state == USB_ACTIVE) {
170
                /* Spawns a new shell.*/
197
                                /* Spawns a new shell.*/
171
                shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1);
198
                                shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell",
-
 
199
                                                NORMALPRIO, shellThread, (void*) &shell_cfg1);
172
              }
200
                        }
173
        #else
201
#else
174
                shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1);
202
                shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1);
175
        #endif
203
        #endif
176
            }
-
 
177
            else {
204
                } else {
178
              /* If the previous shell exited.*/
205
                        /* If the previous shell exited.*/
179
              if (chThdTerminatedX(shelltp)) {
206
                        if (chThdTerminatedX(shelltp)) {
180
                /* Recovers memory of the previous shell.*/
207
                                /* Recovers memory of the previous shell.*/
181
                chThdRelease(shelltp);
208
                                chThdRelease(shelltp);
182
                shelltp = NULL;
209
                                shelltp = NULL;
183
              }
210
                        }
184
            }
211
                }
185
            chThdSleepMilliseconds(500);
212
                chThdSleepMilliseconds(500);
186
          }
213
        }
187
 
214
 
188
        return MSG_OK;
-
 
189
}
215
}
190
 
216
 
-
 
217
static THD_WORKING_AREA(waThread3, 1024);
-
 
218
static void Thread3(void *arg) {
-
 
219
 
-
 
220
    VL53L0X_Dev_t MyDevice;
-
 
221
    VL53L0X_Dev_t *pMyDevice = &MyDevice;
-
 
222
    setPause(true);
-
 
223
    int Status = VL53L0XdeviceSetup(pMyDevice);
-
 
224
    setPause(false);
-
 
225
      VL53L0X_RangingMeasurementData_t RangingMeasurementData;
-
 
226
 
-
 
227
        while(1){
191
 
228
 
-
 
229
                chThdSleep(chTimeMS2I(100));
-
 
230
                setPause(true);
-
 
231
        Status = VL53L0X_PerformSingleRangingMeasurement(pMyDevice,
-
 
232
                        &RangingMeasurementData);
-
 
233
        setPause(false);
192
 
234
 
-
 
235
         if (Status != VL53L0X_ERROR_NONE) continue;
193
 
236
 
-
 
237
 
-
 
238
         lidar  =  RangingMeasurementData.RangeMilliMeter;
-
 
239
 
-
 
240
 
-
 
241
 
-
 
242
        }
-
 
243
}
194
/*
244
/*
195
 * Application entry point.
245
 * Application entry point.
196
 */
246
 */
197
int main(void) {
247
int main(void) {
198
//  struct EventListener el0, el1;
248
//  struct EventListener el0, el1;
Line 206... Line 256...
206
         */
256
         */
207
        halInit();
257
        halInit();
208
        chSysInit();
258
        chSysInit();
209
 
259
 
210
#if (HAL_USE_SERIAL_USB == TRUE)
260
#if (HAL_USE_SERIAL_USB == TRUE)
211
  /*
261
        /*
212
   * Initializes a serial-over-USB CDC driver.
262
         * Initializes a serial-over-USB CDC driver.
213
   */
263
         */
214
  sduObjectInit(&SDU1);
264
        sduObjectInit(&SDU1);
215
  sduStart(&SDU1, &serusbcfg);
265
        sduStart(&SDU1, &serusbcfg);
216
 
266
 
217
#if  HAL_USE_USB_DUAL_CDC == TRUE
267
#if  HAL_USE_USB_DUAL_CDC == TRUE
218
  sduObjectInit(&SDU2);
268
        sduObjectInit(&SDU2);
219
  sduStart(&SDU2, &serusbcfg2);
269
        sduStart(&SDU2, &serusbcfg2);
220
#endif
270
#endif
221
 
271
 
222
  /*
272
        /*
223
   * Activates the USB driver and then the USB bus pull-up on D+.
273
         * Activates the USB driver and then the USB bus pull-up on D+.
224
   * Note, a delay is inserted in order to not have to disconnect the cable
274
         * Note, a delay is inserted in order to not have to disconnect the cable
225
   * after a reset.
275
         * after a reset.
226
   */
276
         */
227
  usbDisconnectBus(serusbcfg.usbp);
277
        usbDisconnectBus(serusbcfg.usbp);
228
  chThdSleepMilliseconds(1000);
278
        chThdSleepMilliseconds(1000);
229
  usbStart(serusbcfg.usbp, &usbcfg);
279
        usbStart(serusbcfg.usbp, &usbcfg);
230
  usbConnectBus(serusbcfg.usbp);
280
        usbConnectBus(serusbcfg.usbp);
231
#else
281
#else
232
  /*
282
  /*
233
   * Initializes serial port.
283
   * Initializes serial port.
234
   */
284
   */
235
  sdStart(&SD2, NULL);
285
  sdStart(&SD2, NULL);
236
#endif /* HAL_USE_SERIAL_USB */
286
#endif /* HAL_USE_SERIAL_USB */
237
 
287
 
-
 
288
        useAdc();
238
 
289
 
239
   useAdc();
-
 
240
 
290
 
241
        /*
291
        /*
242
         * Shell manager initialization.
292
         * Shell manager initialization.
243
         */
293
         */
244
        shellInit();
294
//      shellInit();
-
 
295
        /*
-
 
296
         * initialise approximate maths
-
 
297
         */
-
 
298
        ap_init();
-
 
299
 
-
 
300
        chMtxLock(&mutexDisplay);
-
 
301
 
-
 
302
        /* start the SPI hardware for display */
-
 
303
        ssd1306spiInit();
-
 
304
 
-
 
305
        ssd1306_begin( SSD1306_SWITCHCAPVCC, 0) ;
-
 
306
 
-
 
307
 
-
 
308
        clearDisplay();
-
 
309
 
-
 
310
        display();
245
 
311
 
-
 
312
         chMtxUnlock(&mutexDisplay);
246
 
313
 
-
 
314
 
247
         /*
315
        /*
248
         * Creates the PLL thread
316
         * Creates the PLL thread
249
         */
317
         */
250
        chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
318
        chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
251
 
319
 
-
 
320
        chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
252
 
321
 
-
 
322
//      chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO, Thread3, NULL);
253
 
323
 
-
 
324
    // reset gauge
-
 
325
        origin = 540;
-
 
326
        count  = 540;
-
 
327
        target = 0;
254
        chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
328
        chThdSleepMilliseconds(1000);
-
 
329
        target = 0;
255
 
330
 
-
 
331
        int frac = 0;
256
        /* start the SPI hardware for display */
332
        /* start the SPI hardware for display */
257
        while(1)
333
        while (1) {
258
{
-
 
259
 
334
 
260
        // read the dial
335
           chThdSleepMilliseconds(100);
-
 
336
        unsigned const SCALE = frac * 32 + 16;
261
        adcSample();
337
                frac = ++frac %10;
262
 
338
 
-
 
339
                // read the dial
-
 
340
                 adcSample();
263
 
341
 
-
 
342
                origin = count;
264
 
343
 
265
        origin = count;
344
                target = target +1;
266
 
345
 
267
    target = getAdc(0)*630L/4096L;
346
                // target = lidar * 630L / 2000L;
268
 
347
 
-
 
348
        if (target > 540) target = 0;
269
 
349
 
270
 
350
 
271
         chThdSleep(chTimeMS2I(100));
351
                chMtxLock(&mutexDisplay);
272
 
352
 
-
 
353
            font_gotoxy(0,0);
-
 
354
            clearDisplay();
-
 
355
 
-
 
356
        //    print_scaled_string("cm:",0,16,4,SCALE);
273
}
357
 
-
 
358
 
-
 
359
            print_digits(0, 0, 4, 1, target,SCALE);
-
 
360
 
-
 
361
            chMtxUnlock(&mutexDisplay);
274
 
362
 
-
 
363
            display();
-
 
364
 
-
 
365
 
-
 
366
 
-
 
367
        }
275
 
368
 
276
}
369
}