1#include "../../Inc/Sensors/AnalogSensor.h"
46 analogSensor->
channel = channel;
49 GPIO_InitTypeDef GPIOXout_Struct = {0};
50 GPIOXout_Struct.Mode = GPIO_MODE_ANALOG;
51 GPIOXout_Struct.Pull = GPIO_NOPULL;
52 GPIOXout_Struct.Speed = GPIO_SPEED_FREQ_HIGH;
55 if (channel >= 0 && channel <= 5) {
57 __HAL_RCC_GPIOA_CLK_ENABLE();
58 GPIOXout_Struct.Pin = GPIO_PIN_0 << channel;
60 }
else if (channel == 6 || channel == 7) {
62 __HAL_RCC_GPIOA_CLK_ENABLE();
63 GPIOXout_Struct.Pin = GPIO_PIN_6 << (channel - 6);
65 }
else if (channel == 8 || channel == 9) {
67 __HAL_RCC_GPIOB_CLK_ENABLE();
68 GPIOXout_Struct.Pin = GPIO_PIN_0 << (channel - 8);
70 }
else if (channel >= 10 && channel <= 13) {
72 __HAL_RCC_GPIOC_CLK_ENABLE();
73 GPIOXout_Struct.Pin = GPIO_PIN_0 << (channel - 10);
75 }
else if (channel == 14 || channel == 15) {
77 __HAL_RCC_GPIOC_CLK_ENABLE();
78 GPIOXout_Struct.Pin = GPIO_PIN_4 << (channel - 14);
114void ProcessADCData(uint16_t* adc1_data, uint16_t* adc2_data, uint16_t* adc3_data) {
118 for (
int i = 0; i < 6; i++) {
119 sample.
adc[i] = adc1_data[i];
123 sample.
adc[6] = adc2_data[0];
124 sample.
adc[7] = adc2_data[1];
125 sample.
adc[8] = adc2_data[2];
126 sample.
adc[9] = adc2_data[3];
127 sample.
adc[14] = adc2_data[4];
128 sample.
adc[15] = adc2_data[5];
131 for (
int i = 0; i < 4; i++) {
132 sample.
adc[i + 10] = adc3_data[i];
139 snprintf(
uart_buf,
sizeof(
uart_buf),
"ADC0: %4d, ADC7: %4d, ADC10: %4d\r\n",
140 sample.
adc[0], sample.
adc[7], sample.
adc[10]);
176 for (uint32_t i = 0; i < samples_to_copy; i++) {
181 return samples_to_copy;
int getAnalogSensorData(AnalogSensor *sensor)
Retrieves analog sensor data for a specific channel.
void initAnalogSensor(AnalogSensor *analogSensor, const char *name, int hz, int channel)
Initializes an analog sensor and configures the corresponding GPIO pin.
void ProcessADCData(uint16_t *adc1_data, uint16_t *adc2_data, uint16_t *adc3_data)
Processes ADC data from all three ADCs and stores it in the circular buffer.
ADCSample getLatestSample(void)
Retrieves the latest ADC sample from the buffer.
uint32_t getRecentSamples(ADCSample *samples, uint32_t num_samples)
Retrieves a specified number of recent samples from the buffer.
static void addSampleToBuffer(ADCSample sample)
Adds a sample to the circular buffer.
static uint32_t buffer_count
static ADCSample adc_circular_buffer[BUFFER_SIZE]
static uint32_t buffer_tail
static uint32_t buffer_head
char uart_buf[UART_BUF_SIZE]
void initSensor(Sensor *sensor, const char *name, int hz, SensorType type)
Initializes a sensor with the given parameters.
uint16_t adc[ADC_CHANNELS]