14 #include "../adc/adc.h"
15 #include "../dac/dac.h"
16 #include "../leds/leds.h"
22 static sensorData_t sensorData;
23 static xSemaphoreHandle semaphore;
26 // Return 1 if got a copy, 0 if not.
28 int sensorsCopyData (sensorData_t *dst)
30 if (xSemaphoreTake (semaphore, 100 / portTICK_RATE_MS) == pdTRUE)
32 memcpy (dst, &sensorData, sizeof (sensorData));
33 xSemaphoreGive (semaphore);
37 memset (dst, 0, sizeof (sensorData_t));
44 portTASK_FUNCTION (vSensorsTask, pvParameters __attribute__ ((unused)))
46 portTickType xTickCount;
48 int adcLastValue = -1;
51 int dacDirection = -16;
53 memset (&sensorData, 0, sizeof (sensorData));
55 vSemaphoreCreateBinary (semaphore);
57 xTickCount = xTaskGetTickCount ();
63 vTaskDelayUntil (&xTickCount, 100 / portTICK_RATE_MS);
68 // Adjust the DAC value so we output a slow sine wave
70 if ((dacValue <= 0) || (dacValue >= (1024 - dacDirection)))
71 dacDirection = 0 - dacDirection;
74 dacValue += dacDirection;
77 // Read the current ADC value, keep only top 2 bits. If it changes,
78 // tell the LED task to change the blink rate.
80 if ((adcValue = (adcRead0_3 () >> 8)) != adcLastValue)
82 xQueueSend (xLEDQueue, &adcValue, (portTickType) 0);
83 adcLastValue = adcValue;
88 // Update the sensors data
90 if (xSemaphoreTake (semaphore, portMAX_DELAY) == pdTRUE)
92 sensorData.sensorCount++;
93 sensorData.adcChanges = adcChanged;
95 xSemaphoreGive (semaphore);
99 // get and display last X, Y, and Z accelerometer readings,
100 // the start another conversion
102 printf("%04x %04x %04x\n", adcReadX(), adcReadY(), adcReadZ());