From 7a24078e1861cab66ff96489d1fcc6757e5a1a1d Mon Sep 17 00:00:00 2001 From: Ea-r-th <39779954+Ea-r-th@users.noreply.github.com> Date: Thu, 6 Nov 2025 19:58:38 -0800 Subject: [PATCH] Most functionality for seat working --- .../Peripheral/ADC/Reg/SHAL_ADC_REG_L432KC.h | 2 +- .../Core/SHAL_CORE.cpp | 0 .../EXT/SHAL_EXTI_CALLBACK.cpp | 0 .../Peripheral/ADC/SHAL_ADC.cpp | 0 .../Peripheral/GPIO/SHAL_GPIO.cpp | 0 .../Peripheral/I2C/SHAL_I2C.cpp | 0 .../Peripheral/Timer/SHAL_TIM.cpp | 0 .../Peripheral/Timer/SHAL_TIM_CALLBACK.cpp | 0 .../Peripheral/UART/SHAL_UART.cpp | 0 .../System/system_stm32f0xx.c | 0 SHAL/Src/main.cpp | 117 +++++++++++++----- 11 files changed, 84 insertions(+), 35 deletions(-) rename SHAL/Src/{STM32F0XX => STM32F0xx}/Core/SHAL_CORE.cpp (100%) rename SHAL/Src/{STM32F0XX => STM32F0xx}/EXT/SHAL_EXTI_CALLBACK.cpp (100%) rename SHAL/Src/{STM32F0XX => STM32F0xx}/Peripheral/ADC/SHAL_ADC.cpp (100%) rename SHAL/Src/{STM32F0XX => STM32F0xx}/Peripheral/GPIO/SHAL_GPIO.cpp (100%) rename SHAL/Src/{STM32F0XX => STM32F0xx}/Peripheral/I2C/SHAL_I2C.cpp (100%) rename SHAL/Src/{STM32F0XX => STM32F0xx}/Peripheral/Timer/SHAL_TIM.cpp (100%) rename SHAL/Src/{STM32F0XX => STM32F0xx}/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp (100%) rename SHAL/Src/{STM32F0XX => STM32F0xx}/Peripheral/UART/SHAL_UART.cpp (100%) rename SHAL/Src/{STM32F0XX => STM32F0xx}/System/system_stm32f0xx.c (100%) diff --git a/SHAL/Include/Peripheral/ADC/Reg/SHAL_ADC_REG_L432KC.h b/SHAL/Include/Peripheral/ADC/Reg/SHAL_ADC_REG_L432KC.h index 5e85ffb..05befeb 100644 --- a/SHAL/Include/Peripheral/ADC/Reg/SHAL_ADC_REG_L432KC.h +++ b/SHAL/Include/Peripheral/ADC/Reg/SHAL_ADC_REG_L432KC.h @@ -16,7 +16,7 @@ #define NUM_ADC_CHANNELS 16 enum class SHAL_ADC_Channel : uint32_t { - CH0, + CH0 = 0, CH1, CH2, CH3, diff --git a/SHAL/Src/STM32F0XX/Core/SHAL_CORE.cpp b/SHAL/Src/STM32F0xx/Core/SHAL_CORE.cpp similarity index 100% rename from SHAL/Src/STM32F0XX/Core/SHAL_CORE.cpp rename to SHAL/Src/STM32F0xx/Core/SHAL_CORE.cpp diff --git a/SHAL/Src/STM32F0XX/EXT/SHAL_EXTI_CALLBACK.cpp b/SHAL/Src/STM32F0xx/EXT/SHAL_EXTI_CALLBACK.cpp similarity index 100% rename from SHAL/Src/STM32F0XX/EXT/SHAL_EXTI_CALLBACK.cpp rename to SHAL/Src/STM32F0xx/EXT/SHAL_EXTI_CALLBACK.cpp diff --git a/SHAL/Src/STM32F0XX/Peripheral/ADC/SHAL_ADC.cpp b/SHAL/Src/STM32F0xx/Peripheral/ADC/SHAL_ADC.cpp similarity index 100% rename from SHAL/Src/STM32F0XX/Peripheral/ADC/SHAL_ADC.cpp rename to SHAL/Src/STM32F0xx/Peripheral/ADC/SHAL_ADC.cpp diff --git a/SHAL/Src/STM32F0XX/Peripheral/GPIO/SHAL_GPIO.cpp b/SHAL/Src/STM32F0xx/Peripheral/GPIO/SHAL_GPIO.cpp similarity index 100% rename from SHAL/Src/STM32F0XX/Peripheral/GPIO/SHAL_GPIO.cpp rename to SHAL/Src/STM32F0xx/Peripheral/GPIO/SHAL_GPIO.cpp diff --git a/SHAL/Src/STM32F0XX/Peripheral/I2C/SHAL_I2C.cpp b/SHAL/Src/STM32F0xx/Peripheral/I2C/SHAL_I2C.cpp similarity index 100% rename from SHAL/Src/STM32F0XX/Peripheral/I2C/SHAL_I2C.cpp rename to SHAL/Src/STM32F0xx/Peripheral/I2C/SHAL_I2C.cpp diff --git a/SHAL/Src/STM32F0XX/Peripheral/Timer/SHAL_TIM.cpp b/SHAL/Src/STM32F0xx/Peripheral/Timer/SHAL_TIM.cpp similarity index 100% rename from SHAL/Src/STM32F0XX/Peripheral/Timer/SHAL_TIM.cpp rename to SHAL/Src/STM32F0xx/Peripheral/Timer/SHAL_TIM.cpp diff --git a/SHAL/Src/STM32F0XX/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp b/SHAL/Src/STM32F0xx/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp similarity index 100% rename from SHAL/Src/STM32F0XX/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp rename to SHAL/Src/STM32F0xx/Peripheral/Timer/SHAL_TIM_CALLBACK.cpp diff --git a/SHAL/Src/STM32F0XX/Peripheral/UART/SHAL_UART.cpp b/SHAL/Src/STM32F0xx/Peripheral/UART/SHAL_UART.cpp similarity index 100% rename from SHAL/Src/STM32F0XX/Peripheral/UART/SHAL_UART.cpp rename to SHAL/Src/STM32F0xx/Peripheral/UART/SHAL_UART.cpp diff --git a/SHAL/Src/STM32F0XX/System/system_stm32f0xx.c b/SHAL/Src/STM32F0xx/System/system_stm32f0xx.c similarity index 100% rename from SHAL/Src/STM32F0XX/System/system_stm32f0xx.c rename to SHAL/Src/STM32F0xx/System/system_stm32f0xx.c diff --git a/SHAL/Src/main.cpp b/SHAL/Src/main.cpp index 6965588..42acc1a 100644 --- a/SHAL/Src/main.cpp +++ b/SHAL/Src/main.cpp @@ -1,60 +1,85 @@ #include #include "SHAL.h" -GPIO_Key gpios[6] = { - GPIO_Key::A0, - GPIO_Key::A1, - GPIO_Key::A4, - GPIO_Key::A5, - GPIO_Key::A6, - GPIO_Key::A7, + +#define NUM_CHANNELS 8 + +SHAL_ADC_Channel channels[NUM_CHANNELS] = { + SHAL_ADC_Channel::CH5, + SHAL_ADC_Channel::CH6, + SHAL_ADC_Channel::CH8, + SHAL_ADC_Channel::CH9, + SHAL_ADC_Channel::CH10, + SHAL_ADC_Channel::CH11, + SHAL_ADC_Channel::CH12, + SHAL_ADC_Channel::CH7 }; -uint16_t vals[6] = {0,0,0,0,0,0}; +uint16_t vals[NUM_CHANNELS] = {0,0,0,0,0,0,0,0}; uint8_t currentSensor = 0; bool isAlarmBeeping = false; - bool prevIsCalibrateButtonHigh = false; -uint16_t sensorThresholds[6] = {4096,4096,4096,4096,4096,4096}; +uint16_t sensorThresholds[NUM_CHANNELS] = {}; +int buzzer_beepCount = 0; +bool isBeepingForCalibration = false; -int cyclesPerPrint = 4; +int cyclesPerPrint = 2; int currentCycle = 0; +bool areSensorRequirementsMetCurrent = false; +bool areSensorRequirementsMetPrevious = false; + void getSensorData(){ - vals[currentSensor] = GPIOManager::get(gpios[currentSensor]).analogRead(SHAL_ADC_SampleTime::C8); + vals[currentSensor] = SHAL_ADC1.singleConvertSingle(channels[currentSensor]); - if(currentSensor == 5 && currentCycle == cyclesPerPrint - 1){ - char buff[64]; - sprintf(buff, "%d, %d, %d, %d, %d, %d\r\n", vals[0],vals[1],vals[2],vals[3],vals[4],vals[5]); + if(currentSensor == (NUM_CHANNELS - 1) && currentCycle == cyclesPerPrint - 1){ + char buff[125]; + sprintf(buff, "5:%d,6:%d,8:%d,9:%d,10:%d,11:%d,12:%d,7:%d\r\n", vals[0],vals[1],vals[2],vals[3],vals[4],vals[5],vals[6],vals[7]); SHAL_UART2.sendString(buff); } - currentSensor = (currentSensor + 1) % 6; + currentSensor = (currentSensor + 1) % NUM_CHANNELS; currentCycle = (currentCycle + 1) % cyclesPerPrint; } +void startBeeping(){ + SHAL_TIM1.start(); + SHAL_TIM6.start(); +} + +void stopBeeping(){ + SHAL_TIM1.stop(); + SHAL_TIM6.stop(); + isAlarmBeeping = false; + isBeepingForCalibration = false; +} + void calibrateThresholds(){ for(int i = 0; i < 6; i++){ - uint16_t sensorVal = GPIOManager::get(gpios[i]).analogRead(SHAL_ADC_SampleTime::C8); - sensorThresholds[i] = sensorVal; - SHAL_delay_ms(80); + uint16_t sensorVal = SHAL_ADC1.singleConvertSingle(channels[currentSensor]); + sensorThresholds[i] = (sensorVal / 5) * 4; } - char buff[80]; - sprintf(buff, "Thresholds: %d, %d, %d, %d, %d, %d\r\n", sensorThresholds[0],sensorThresholds[1],sensorThresholds[2],sensorThresholds[3],sensorThresholds[4],sensorThresholds[5]); - SHAL_UART2.sendString(buff); } - void PWMToggle(){ + if(isBeepingForCalibration && buzzer_beepCount > 2){ + isBeepingForCalibration = false; + buzzer_beepCount = 0; + SHAL_TIM6.stop(); //Reset timer 6 + SHAL_TIM1.stop(); //Stop buzzer + SHAL_TIM6.init(4000000,400); + } + if(!isAlarmBeeping){ SHAL_TIM1.start(); + buzzer_beepCount++; } else{ SHAL_TIM1.stop(); @@ -68,7 +93,16 @@ void buttonHoldCallback(){ SHAL_TIM7.stop(); //Stop this timer SHAL_TIM2.stop(); //Stop reading from ADC + + buzzer_beepCount = 0; + isBeepingForCalibration = true; + + SHAL_TIM6.init(4000000,80); + SHAL_TIM6.start(); + calibrateThresholds(); + SHAL_TIM1.start(); + SHAL_TIM2.start(); //Restart value checks } @@ -76,17 +110,18 @@ int main() { SHAL_init(); - SHAL_UART2.init(UART_Pair_Key::Tx2A2_Rx2A3); - SHAL_UART2.begin(115200); + //SHAL_UART2.init(UART_Pair_Key::Tx2A2_Rx2A3); + //SHAL_UART2.begin(115200); PIN(A0).setPinMode(PinMode::ANALOG_MODE); PIN(A1).setPinMode(PinMode::ANALOG_MODE); + PIN(A2).setPinMode(PinMode::ANALOG_MODE); + PIN(A3).setPinMode(PinMode::ANALOG_MODE); PIN(A4).setPinMode(PinMode::ANALOG_MODE); PIN(A5).setPinMode(PinMode::ANALOG_MODE); PIN(A6).setPinMode(PinMode::ANALOG_MODE); PIN(A7).setPinMode(PinMode::ANALOG_MODE); - PIN(B0).setAlternateFunction(GPIO_Alternate_Function_Mapping::B0_TIM1CH2N); SHAL_TIM2.init(4000000,400); @@ -94,20 +129,20 @@ int main() { SHAL_TIM2.enableInterrupt(); SHAL_TIM2.start(); - SHAL_TIM1.init(300,999); + PIN(B0).setAlternateFunction(GPIO_Alternate_Function_Mapping::B0_TIM1CH2N); + + SHAL_TIM1.init(0,2400); SHAL_TIM1.setPWMMode(SHAL_Timer_Channel::CH2,SHAL_TIM_Output_Compare_Mode::PWMMode1,SHAL_Timer_Channel_Main_Output_Mode::Disabled,SHAL_Timer_Channel_Complimentary_Output_Mode::Polarity_Reversed); - SHAL_TIM1.setPWMDutyCycle(499); - SHAL_TIM1.start(); + SHAL_TIM1.setPWMDutyCycle(900); + //PIN(B0).setPinMode(PinMode::OUTPUT_MODE); SHAL_TIM6.init(4000000,400); SHAL_TIM6.setCallbackFunc(PWMToggle); SHAL_TIM6.enableInterrupt(); - SHAL_TIM6.start(); - SHAL_TIM7.init(4000000,6000); + SHAL_TIM7.init(4000000,4500); SHAL_TIM7.setCallbackFunc(buttonHoldCallback); SHAL_TIM7.enableInterrupt(); - //SHAL_TIM7.start(); PIN(B6).setPinMode(PinMode::INPUT_MODE); PIN(B3).setPinMode(PinMode::OUTPUT_MODE); //Test @@ -116,20 +151,34 @@ int main() { while (true) { //Retarded polling based button methods cause EXTI decided to not work on L432KC for some stupid reason at the last second if(!(PIN(B6).digitalRead() == 1)){ - //SHAL_UART2.sendString("High\r\n"); + areSensorRequirementsMetCurrent = true; + + if(!areSensorRequirementsMetPrevious){ + startBeeping(); + } + /* if(!prevIsCalibrateButtonHigh){ SHAL_TIM7.start(); } prevIsCalibrateButtonHigh = true; + */ + } else{ - //SHAL_UART2.sendString("Low\r\n"); + areSensorRequirementsMetCurrent = false; + if(areSensorRequirementsMetPrevious){ + stopBeeping(); + } + + /* if(prevIsCalibrateButtonHigh){ //Button released SHAL_TIM7.stop(); } prevIsCalibrateButtonHigh = false; + */ } + areSensorRequirementsMetPrevious = areSensorRequirementsMetCurrent; } } \ No newline at end of file