You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
8.4 KiB
268 lines
8.4 KiB
#include "main.h"
|
|
|
|
#include <stdbool.h> //定义布尔
|
|
#include <string.h>
|
|
|
|
#include "board.h"
|
|
//
|
|
#include "../zes8p5066lib/basic.h"
|
|
#include "../zes8p5066lib/gpio.h"
|
|
#include "../zes8p5066lib/key.h"
|
|
#include "../zes8p5066lib/systicket.h"
|
|
#include "../zes8p5066lib/uart0.h"
|
|
#include "test.h"
|
|
#if 0
|
|
|
|
#define PWM_FREQHZ 25000 //硬件pwm频率
|
|
/***********************************************************************************************************************
|
|
* =====================================================函数声明====================================================== *
|
|
***********************************************************************************************************************/
|
|
void onkey(zkey_t* key, zkey_state_t key_state);
|
|
/***********************************************************************************************************************
|
|
* =====================================================全局变量====================================================== *
|
|
***********************************************************************************************************************/
|
|
|
|
|
|
|
|
void onkey(zkey_t* key, zkey_state_t key_state) {
|
|
if (key == &s_keys[2] /*左3*/ && zks_falling_edge == key_state) {
|
|
ozone_pwm_control_update();
|
|
printf("onkey2\n");
|
|
}
|
|
if (key == &s_keys[0] /*左2*/ && zks_falling_edge == key_state) {
|
|
ozone_pwm_control_update();
|
|
printf("onkey0\n");
|
|
}
|
|
if (key == &s_keys[1] /*左2*/ && zks_falling_edge == key_state) {
|
|
}
|
|
if (key == &s_keys[3] /*左2*/ && zks_falling_edge == key_state) {
|
|
printf("onkey3\n");
|
|
ozone_pwm_control_update();
|
|
}
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* =================================================INTERNAL_FUNCTION================================================= *
|
|
***********************************************************************************************************************/
|
|
void UART0_IRQHandler(void) {
|
|
/**
|
|
* @brief 接收来自串口的数据并做回显
|
|
*
|
|
*/
|
|
// rx_data = UART_RecByte(UART0);
|
|
// if (!s_rxbuf_is_ready) {
|
|
// if (rx_count < sizeof(rx_buffer) - 1) {
|
|
// rx_buffer[rx_count++] = rx_data;
|
|
// uart0_lastreceive_ticket = get_sys_ticket();
|
|
// }
|
|
// }
|
|
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_TB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_RB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_FE);
|
|
}
|
|
|
|
/**
|
|
* @brief
|
|
*
|
|
* @param freq
|
|
* @param us
|
|
*/
|
|
|
|
float get_duty_by_freq_and_valid_time(uint32_t freq, uint32_t us) {
|
|
float period = 1000000.0 / freq;
|
|
float duty = us * 100 / period;
|
|
return duty;
|
|
}
|
|
|
|
void ozone_pwm_control_update() {
|
|
printf("freqhz:%d duty:%d us\n", freqhz, dutyus);
|
|
set_pwm_modbul_freq_duty2(freqhz, get_duty_by_freq_and_valid_time(freqhz, dutyus));
|
|
}
|
|
|
|
#define DO_IT_EACH_MS(time) \
|
|
{ \
|
|
static uint32_t __ticket = 0; \
|
|
if (port_haspassedms(__ticket) > (time)) { \
|
|
__ticket = get_sys_ticket();
|
|
|
|
#define END() \
|
|
} \
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* ======================================================主函数=======================================================
|
|
***********************************************************************************************************************/
|
|
|
|
bool strcontains(const char* sonstr, const char* fatherstr) { return (strncmp(sonstr, fatherstr, strlen(sonstr)) == 0); }
|
|
|
|
void process_rx_order() {
|
|
if (strcontains("AT+PWM_FREQ_UP_FINE_TUNING", rx_buffer)) {
|
|
freqhz += 50;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_FREQ_DOWN_FINE_TUNING", rx_buffer)) {
|
|
freqhz -= 50;
|
|
if (freqhz <= 0) freqhz = 1;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_FREQ_UP", rx_buffer)) {
|
|
freqhz += 1000;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_FREQ_DOWN", rx_buffer)) {
|
|
freqhz -= 1000;
|
|
if (freqhz <= 0) freqhz = 1;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_DUTY_UP", rx_buffer)) {
|
|
dutyus += 1;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_DUTY_DOWN", rx_buffer)) {
|
|
dutyus -= 1;
|
|
if (dutyus < 0) dutyus = 0;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM=", rx_buffer)) {
|
|
/**
|
|
* AT+PWM=25000,8
|
|
*/
|
|
int _dutyus = 0;
|
|
int _freq = 0;
|
|
sscanf(rx_buffer, "AT+PWM=%d,%d", &_freq, &_dutyus);
|
|
if (_dutyus < 0 || _freq < 10000) {
|
|
printf("Error\n");
|
|
return;
|
|
}
|
|
dutyus = _dutyus;
|
|
freqhz = _freq;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+MAT2=", rx_buffer)) {
|
|
/**
|
|
* AT+PWM=25000,8
|
|
*/
|
|
int mat2 = 0;
|
|
sscanf(rx_buffer, "AT+MAT2=%d", &mat2);
|
|
set_pwm_mat2(mat2);
|
|
} else if (strcontains("AT+FAN=", rx_buffer)) {
|
|
/**
|
|
* AT+PWM=25000,8
|
|
*/
|
|
int mat2 = 0;
|
|
sscanf(rx_buffer, "AT+FAN=%d", &mat2);
|
|
if (mat2 > 0) {
|
|
port_fan_set(true);
|
|
|
|
} else {
|
|
port_fan_set(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void try_process_rx_order() {
|
|
if (port_haspassedms(uart0_lastreceive_ticket) > 100) {
|
|
if (rx_count != 0) {
|
|
s_rxbuf_is_ready = true;
|
|
process_rx_order();
|
|
rx_count = 0;
|
|
memset(rx_buffer, 0, sizeof(rx_buffer));
|
|
s_rxbuf_is_ready = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
float power_filter(float nowpower, float factor) {
|
|
static float last = 0;
|
|
float now_raw = nowpower;
|
|
float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor);
|
|
last = now;
|
|
return now;
|
|
}
|
|
|
|
uint16_t capture(int timeoff);
|
|
#endif
|
|
void onkey(zkey_t* key, zkey_state_t key_state);
|
|
|
|
int a = 34;
|
|
static zkey_t s_keys[] = {
|
|
ZKEY_INIT("powerkey", port_gpio_get_power_key_state), //电源按键
|
|
ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
|
|
ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
|
|
ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
|
|
};
|
|
zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
|
|
|
|
void onkey(zkey_t* key, zkey_state_t key_state) {
|
|
if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
|
|
printf("on %s \n", key->name);
|
|
} else if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
|
|
printf("on %s \n", key->name);
|
|
} else if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
|
|
printf("on %s \n", key->name);
|
|
} else if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
|
|
printf("on %s \n", key->name);
|
|
}
|
|
}
|
|
|
|
void systicket_do_debug_light_state(void) {
|
|
static uint32_t lastprocess = 0;
|
|
static uint8_t debug_led_state = 1;
|
|
if (systicket_haspassedms(lastprocess) > 300) {
|
|
lastprocess = systicket_get_now_ms();
|
|
debug_led_state = !debug_led_state;
|
|
port_debug_set(debug_led_state);
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
SystemInit(); //配置系统时钟
|
|
DeviceClockAllEnable(); //打开所有外设时钟
|
|
systicket_init();
|
|
|
|
//
|
|
zgpio_init_all_gpio();
|
|
port_init();
|
|
|
|
printf("==========OZONE_GENERATOR==========\n");
|
|
printf("= manufactor: iflytop\n");
|
|
printf("= version : %s\n", VERSION);
|
|
printf("=\n");
|
|
|
|
/**
|
|
* @brief 模块初始化
|
|
*/
|
|
zkey_init(&key_module);
|
|
|
|
/**
|
|
* @brief 初始化所有设备状态
|
|
*/
|
|
|
|
port_debug_set(false);
|
|
port_fan_set(false);
|
|
port_led0_set(false);
|
|
port_led1_set(false);
|
|
port_led2_set(false);
|
|
port_led3_set(false);
|
|
port_led_r_set(false);
|
|
port_led_g_set(false);
|
|
port_led_b_set(false);
|
|
//
|
|
port_fan_set(true);
|
|
port_ozone_pwm_set_duty(30000, 10 * 1000);
|
|
port_ozone_pwm_start();
|
|
// port_ozone_pwm_stop
|
|
while (true) {
|
|
//调试指示灯逻辑
|
|
systicket_do_debug_light_state();
|
|
//按键扫描逻辑
|
|
DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
|
|
END();
|
|
|
|
#if 1 //打印功率
|
|
DO_IT_EACH_MS(300) { //
|
|
// printf("v :fan:%f,ozone:%f\n", port_adc_get_fan_voltage(), port_adc_get_ozone_generator_voltage());
|
|
printf("power:fan:%f,ozone:%f\n", port_adc_get_fan_power(), port_adc_get_ozone_generator_power());
|
|
}
|
|
END();
|
|
|
|
test_all_light();
|
|
#endif
|
|
|
|
// test_all_light();
|
|
}
|
|
}
|