1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
| #include "HAL_layer.h" #include "device_drivers.h" #include "service_layer.h" #include <stdio.h>
void SystemClock_Config(void); void GPIO_Config(void); void ADC_Config(void); void SPI_Config(void); void Timer_Config(void);
PowerSwitch_HandleTypeDef power_switch_dev; VoltageSensor_HandleTypeDef voltage_sensor_dev; CurrentSensor_HandleTypeDef current_sensor_dev; TemperatureSensor_HandleTypeDef temperature_sensor_dev; OLED_HandleTypeDef oled_dev; PowerMonitorService_HandleTypeDef power_monitor_service; DisplayService_HandleTypeDef display_service; PowerControlService_HandleTypeDef power_control_service;
int main() { SystemClock_Config(); GPIO_Config(); ADC_Config(); SPI_Config(); Timer_Config();
PowerSwitch_Init(&power_switch_dev); VoltageSensor_Init(&voltage_sensor_dev); CurrentSensor_Init(¤t_sensor_dev); TemperatureSensor_Init(&temperature_sensor_dev); OLED_Init(&oled_dev);
PowerMonitorService_Init(&power_monitor_service); DisplayService_Init(&display_service, &power_monitor_service); PowerControlService_Init(&power_control_service);
power_monitor_service.voltage_sensor = voltage_sensor_dev; power_monitor_service.current_sensor = current_sensor_dev; power_monitor_service.temperature_sensor = temperature_sensor_dev; display_service.oled_screen = oled_dev;
PowerControlService_PowerOff(&power_control_service);
while (1) { static uint32_t last_update_time = 0; uint32_t current_time = HAL_GetTick(); if (current_time - last_update_time >= 100) { last_update_time = current_time; PowerMonitorService_UpdateData(&power_monitor_service); DisplayService_UpdateDisplay(&display_service); }
if () { if () { PowerControlService_PowerOn(&power_control_service); } else { PowerControlService_PowerOff(&power_control_service); } }
} }
void SystemClock_Config(void) { }
void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStruct_PowerSwitch; GPIO_InitStruct_PowerSwitch.Mode = GPIO_MODE_OUTPUT; GPIO_InitStruct_PowerSwitch.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(&GPIO_InitStruct_PowerSwitch); HAL_GPIO_WritePin(&GPIO_InitStruct_PowerSwitch, GPIO_PIN_RESET);
GPIO_InitTypeDef GPIO_InitStruct_OLED_CS; GPIO_InitStruct_OLED_CS.Mode = GPIO_MODE_OUTPUT; GPIO_InitStruct_OLED_CS.Pull = GPIO_PULLUP; HAL_GPIO_Init(&GPIO_InitStruct_OLED_CS);
GPIO_InitTypeDef GPIO_InitStruct_OLED_DC; GPIO_InitStruct_OLED_DC.Mode = GPIO_MODE_OUTPUT; GPIO_InitStruct_OLED_DC.Pull = GPIO_PULLUP; HAL_GPIO_Init(&GPIO_InitStruct_OLED_DC);
GPIO_InitTypeDef GPIO_InitStruct_OLED_RESET; GPIO_InitStruct_OLED_RESET.Mode = GPIO_MODE_OUTPUT; GPIO_InitStruct_OLED_RESET.Pull = GPIO_PULLUP; HAL_GPIO_Init(&GPIO_InitStruct_OLED_RESET);
}
void ADC_Config(void) { ADC_InitTypeDef ADC_InitStruct_Voltage; HAL_ADC_Init(&ADC_InitStruct_Voltage);
ADC_InitTypeDef ADC_InitStruct_Current; HAL_ADC_Init(&ADC_InitStruct_Current); }
void SPI_Config(void) { SPI_InitTypeDef SPI_InitStruct_OLED; HAL_SPI_Init(&SPI_InitStruct_OLED); }
void Timer_Config(void) { TIM_InitTypeDef TIM_InitStruct_System; HAL_TIM_Base_Init(&TIM_InitStruct_System); HAL_TIM_Base_Start(&TIM_InitStruct_System); }
#include "device_drivers.h"
void PowerSwitch_Init(PowerSwitch_HandleTypeDef *hps) { HAL_GPIO_Init(&hps->gpio_power_switch); }
void PowerSwitch_TurnOn(PowerSwitch_HandleTypeDef *hps) { HAL_GPIO_WritePin(&hps->gpio_power_switch, GPIO_PIN_SET); }
void PowerSwitch_TurnOff(PowerSwitch_HandleTypeDef *hps) { HAL_GPIO_WritePin(&hps->gpio_power_switch, GPIO_PIN_RESET); }
#include "device_drivers.h"
void VoltageSensor_Init(VoltageSensor_HandleTypeDef *hvs) { HAL_ADC_Init(&hvs->adc_voltage); }
float VoltageSensor_ReadVoltage(VoltageSensor_HandleTypeDef *hvs) { uint16_t adc_value = HAL_ADC_ReadChannel(); float voltage = (float)adc_value * * hvs->voltage_divider_ratio; return voltage; }
#include "device_drivers.h"
void CurrentSensor_Init(CurrentSensor_HandleTypeDef *hcs) { HAL_ADC_Init(&hcs->adc_current); }
float CurrentSensor_ReadCurrent(CurrentSensor_HandleTypeDef *hcs) { uint16_t adc_value = HAL_ADC_ReadChannel(); float current = (float)adc_value * hcs->adc_vref / hcs->adc_resolution / hcs->shunt_resistance / ; return current; }
#include "device_drivers.h"
float PowerCalculator_CalculatePower(float voltage, float current) { return voltage * current; }
#include "service_layer.h" #include "device_drivers.h"
void PowerMonitorService_Init(PowerMonitorService_HandleTypeDef *hpms) { }
void PowerMonitorService_UpdateData(PowerMonitorService_HandleTypeDef *hpms) { hpms->voltage = VoltageSensor_ReadVoltage(&hpms->voltage_sensor); hpms->current = CurrentSensor_ReadCurrent(&hpms->current_sensor); hpms->power = PowerCalculator_CalculatePower(hpms->voltage, hpms->current); hpms->temperature = TemperatureSensor_ReadTemperature(&hpms->temperature_sensor); }
float PowerMonitorService_GetVoltage(PowerMonitorService_HandleTypeDef *hpms) { return hpms->voltage; }
float PowerMonitorService_GetCurrent(PowerMonitorService_HandleTypeDef *hpms) { return hpms->current; }
float PowerMonitorService_GetPower(PowerMonitorService_HandleTypeDef *hpms) { return hpms->power; }
float PowerMonitorService_GetTemperature(PowerMonitorService_HandleTypeDef *hpms) { return hpms->temperature; }
#include "service_layer.h" #include "device_drivers.h" #include <stdio.h>
void DisplayService_Init(DisplayService_HandleTypeDef *hds, PowerMonitorService_HandleTypeDef *hpms) { hds->power_monitor_service = hpms; OLED_Init(&hds->oled_screen); OLED_ClearDisplay(&hds->oled_screen); }
void DisplayService_UpdateDisplay(DisplayService_HandleTypeDef *hds) { OLED_ClearDisplay(&hds->oled_screen);
char buffer[50]; float voltage = PowerMonitorService_GetVoltage(hds->power_monitor_service); float current = PowerMonitorService_GetCurrent(hds->power_monitor_service); float power = PowerMonitorService_GetPower(hds->power_monitor_service); float temperature = PowerMonitorService_GetTemperature(hds->power_monitor_service);
sprintf(buffer, "Voltage: %.2f V", voltage); OLED_WriteString(&hds->oled_screen, 0, 0, buffer, 8, 1);
sprintf(buffer, "Current: %.2f A", current); OLED_WriteString(&hds->oled_screen, 0, 10, buffer, 8, 1);
sprintf(buffer, "Power: %.2f W", power); OLED_WriteString(&hds->oled_screen, 0, 20, buffer, 8, 1);
sprintf(buffer, "Temp: %.1f C", temperature); OLED_WriteString(&hds->oled_screen, 0, 30, buffer, 8, 1);
OLED_UpdateDisplay(&hds->oled_screen); }
#include "service_layer.h" #include "device_drivers.h"
void PowerControlService_Init(PowerControlService_HandleTypeDef *hpcs) { }
void PowerControlService_PowerOn(PowerControlService_HandleTypeDef *hpcs) { PowerSwitch_TurnOn(&hpcs->power_switch); }
void PowerControlService_PowerOff(PowerControlService_HandleTypeDef *hpcs) { PowerSwitch_TurnOff(&hpcs->power_switch); }
|