Subversion Repositories ChibiGauge

Rev

Rev 2 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
  3.  2011,2012 Giovanni Di Sirio.
  4.  
  5.  This file is part of ChibiOS/RT.
  6.  
  7.  ChibiOS/RT is free software; you can redistribute it and/or modify
  8.  it under the terms of the GNU General Public License as published by
  9.  the Free Software Foundation; either version 3 of the License, or
  10.  (at your option) any later version.
  11.  
  12.  ChibiOS/RT is distributed in the hope that it will be useful,
  13.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  GNU General Public License for more details.
  16.  
  17.  You should have received a copy of the GNU General Public License
  18.  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20.  ---
  21.  
  22.  A special exception to the GPL can be applied should you wish to distribute
  23.  a combined work that includes ChibiOS/RT, without being obliged to provide
  24.  the source code for any proprietary components. See the file exception.txt
  25.  for full details of how and when the exception can be applied.
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. #include "ch.h"
  32. #include "hal.h"
  33. //#include "test.h"
  34. #include "shell.h"
  35. #include "evtimer.h"
  36. #include "chprintf.h"
  37.  
  38. #if (HAL_USE_SERIAL_USB == TRUE)
  39. #include "usbcfg.h"
  40. #endif
  41.  
  42. #include "useAdc.h"
  43.  
  44. #include "shellCmds.h"
  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"
  54.  
  55.  
  56. static MUTEX_DECL( mutexDisplay);
  57.  
  58. unsigned lidar = 0;
  59.  
  60. /*===========================================================================*/
  61. /* Command line related.                                                     */
  62. /*===========================================================================*/
  63.  
  64. #define SHELL_WA_SIZE   THD_WORKING_AREA_SIZE(1024)
  65. #define TEST_WA_SIZE    THD_WORKING_AREA_SIZE(256)
  66.  
  67. static const ShellConfig shell_cfg1 = {
  68. #if (HAL_USE_SERIAL_USB == TRUE)
  69.                 (BaseSequentialStream*) &SDU1,
  70. #else
  71.   (BaseSequentialStream *)&SD1,
  72. #endif
  73.                 shellCommands };
  74. ////////
  75. // end of shell stuff
  76.  
  77. uint16_t sampIndex;
  78.  
  79. void setDriveA(uint8_t bit) {
  80.         if (bit) {
  81.                 palSetPad(GPIOA, GPIOA_A1);
  82.                 palClearPad(GPIOA, GPIOA_A2);
  83.         } else {
  84.                 palClearPad(GPIOA, GPIOA_A1);
  85.                 palSetPad(GPIOA, GPIOA_A2);
  86.         }
  87. }
  88.  
  89. void setDriveB(uint8_t bit) {
  90.         if (bit) {
  91.                 palSetPad(GPIOA, GPIOA_B1);
  92.                 palClearPad(GPIOA, GPIOA_B2);
  93.         } else {
  94.                 palClearPad(GPIOA, GPIOA_B1);
  95.                 palSetPad(GPIOA, GPIOA_B2);
  96.         }
  97. }
  98.  
  99.  
  100. // dial settings
  101. volatile int origin = 0;
  102. ;
  103. volatile int target = 0;
  104. volatile int count = 0;
  105.  
  106. volatile bool pause;
  107. //
  108.  
  109.  
  110. void setPause(bool p) {
  111.         pause = p;
  112. }
  113.  
  114. static THD_WORKING_AREA(waThread1, 512);
  115. static void Thread1(void *arg) {
  116.  
  117.         (void) arg;
  118.         unsigned const fast = 10;
  119.         unsigned const slow = 30;
  120.         unsigned const range = slow - fast;
  121.         unsigned del = fast;
  122.         int step = 1;
  123.         chRegSetThreadName("Step");
  124.         while (TRUE) {
  125.  
  126.                 while (pause)
  127.                         chThdSleep(1000);
  128.  
  129.                 switch (count % 4) {
  130.                 case 0:
  131.                         setDriveA(1);
  132.                         setDriveB(0);
  133.                         break;
  134.                 case 1:
  135.                         setDriveA(1);
  136.                         setDriveB(1);
  137.                         break;
  138.                 case 2:
  139.                         setDriveA(0);
  140.                         setDriveB(1);
  141.                         break;
  142.                 case 3:
  143.                         setDriveA(0);
  144.                         setDriveB(0);
  145.                         break;
  146.                 }
  147.  
  148.                 // all this calculates minimum distance from
  149.                 // target or origin
  150.                 int d1 = count - origin;
  151.                 if (d1 < 0)
  152.                         d1 = -d1;
  153.                 int d2 = count - target;
  154.                 if (d2 < 0)
  155.                         d2 = -d2;
  156.                 // finally, minimum distance
  157.                 int dist = d1 < d2 ? d1 : d2;
  158.  
  159.                 del = fast;
  160.                 if (dist < range) // inside lower bound of distance
  161.                                 {
  162.                         del = slow - dist;
  163.                 }
  164.                 chThdSleep(del);
  165.  
  166.                 if (count < target) {
  167.                         step = 1;
  168.                 }
  169.                 if (count > target) {
  170.                         step = -1;
  171.                 }
  172.                 if (count == target) {
  173.                         step = 0;
  174.                 }
  175.                 count = count + step;
  176.  
  177.         }
  178.  
  179. }
  180.  
  181. /*
  182.  * Command Shell Thread
  183.  */
  184. static THD_WORKING_AREA(waThread2, 512);
  185. static void Thread2(void *arg) {
  186.         thread_t *shelltp = NULL;
  187.  
  188.         chRegSetThreadName("Shell ");
  189.         /*
  190.          * in this demo it just performs
  191.          * a shell respawn upon its termination.
  192.          */
  193.         while (true) {
  194.                 if (!shelltp) {
  195. #if (HAL_USE_SERIAL_USB == TRUE)
  196.                         if (SDU1.config->usbp->state == USB_ACTIVE) {
  197.                                 /* Spawns a new shell.*/
  198.                                 shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell",
  199.                                                 NORMALPRIO, shellThread, (void*) &shell_cfg1);
  200.                         }
  201. #else
  202.                 shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1);
  203.         #endif
  204.                 } else {
  205.                         /* If the previous shell exited.*/
  206.                         if (chThdTerminatedX(shelltp)) {
  207.                                 /* Recovers memory of the previous shell.*/
  208.                                 chThdRelease(shelltp);
  209.                                 shelltp = NULL;
  210.                         }
  211.                 }
  212.                 chThdSleepMilliseconds(500);
  213.         }
  214.  
  215. }
  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){
  228.  
  229.                 chThdSleep(chTimeMS2I(100));
  230.                 setPause(true);
  231.         Status = VL53L0X_PerformSingleRangingMeasurement(pMyDevice,
  232.                         &RangingMeasurementData);
  233.         setPause(false);
  234.  
  235.          if (Status != VL53L0X_ERROR_NONE) continue;
  236.  
  237.  
  238.          lidar  =  RangingMeasurementData.RangeMilliMeter;
  239.  
  240.  
  241.  
  242.         }
  243. }
  244. /*
  245.  * Application entry point.
  246.  */
  247. int main(void) {
  248. //  struct EventListener el0, el1;
  249.  
  250.         /*
  251.          * System initializations.
  252.          * - HAL initialization, this also initializes the configured device drivers
  253.          *   and performs the board-specific initializations.
  254.          * - Kernel initialization, the main() function becomes a thread and the
  255.          *   RTOS is active.
  256.          */
  257.         halInit();
  258.         chSysInit();
  259.  
  260. #if (HAL_USE_SERIAL_USB == TRUE)
  261.         /*
  262.          * Initializes a serial-over-USB CDC driver.
  263.          */
  264.         sduObjectInit(&SDU1);
  265.         sduStart(&SDU1, &serusbcfg);
  266.  
  267. #if  HAL_USE_USB_DUAL_CDC == TRUE
  268.         sduObjectInit(&SDU2);
  269.         sduStart(&SDU2, &serusbcfg2);
  270. #endif
  271.  
  272.         /*
  273.          * Activates the USB driver and then the USB bus pull-up on D+.
  274.          * Note, a delay is inserted in order to not have to disconnect the cable
  275.          * after a reset.
  276.          */
  277.         usbDisconnectBus(serusbcfg.usbp);
  278.         chThdSleepMilliseconds(1000);
  279.         usbStart(serusbcfg.usbp, &usbcfg);
  280.         usbConnectBus(serusbcfg.usbp);
  281. #else
  282.   /*
  283.    * Initializes serial port.
  284.    */
  285.   sdStart(&SD2, NULL);
  286. #endif /* HAL_USE_SERIAL_USB */
  287.  
  288.         useAdc();
  289.  
  290.  
  291.         /*
  292.          * Shell manager initialization.
  293.          */
  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();
  311.  
  312.          chMtxUnlock(&mutexDisplay);
  313.  
  314.  
  315.         /*
  316.          * Creates the PLL thread
  317.          */
  318.         chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  319.  
  320.         chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
  321.  
  322. //      chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO, Thread3, NULL);
  323.  
  324.     // reset gauge
  325.         origin = 540;
  326.         count  = 540;
  327.         target = 0;
  328.         chThdSleepMilliseconds(1000);
  329.         target = 0;
  330.  
  331.         int frac = 0;
  332.         /* start the SPI hardware for display */
  333.         while (1) {
  334.  
  335.            chThdSleepMilliseconds(100);
  336.         unsigned const SCALE = frac * 32 + 16;
  337.                 frac = ++frac %10;
  338.  
  339.                 // read the dial
  340.                  adcSample();
  341.  
  342.                 origin = count;
  343.  
  344.                 target = target +1;
  345.  
  346.                 // target = lidar * 630L / 2000L;
  347.  
  348.         if (target > 540) target = 0;
  349.  
  350.  
  351.                 chMtxLock(&mutexDisplay);
  352.  
  353.             font_gotoxy(0,0);
  354.             clearDisplay();
  355.  
  356.         //    print_scaled_string("cm:",0,16,4,SCALE);
  357.  
  358.  
  359.             print_digits(0, 0, 4, 1, target,SCALE);
  360.  
  361.             chMtxUnlock(&mutexDisplay);
  362.  
  363.             display();
  364.  
  365.  
  366.  
  367.         }
  368.  
  369. }
  370.