#include #include "gpio.h" #include "ui.h" #include "keypad.h" #include "global.h" #include "time.h" void __limit_freq_range() { uint16_t limit_min = (global_data.rf_mode == G_RF_MODE_AM) ? G_AM_FREQ_MIN : G_FM_FREQ_MIN; uint16_t limit_max = (global_data.rf_mode == G_RF_MODE_AM) ? G_AM_FREQ_MAX : G_FM_FREQ_MAX; if (global_data.freq < limit_min) { global_data.freq = limit_min; } else if (global_data.freq > limit_max) { global_data.freq = limit_max; } } void __send_freq() { // TODO: 发送频率 } void __send_volumn() { // TODO: 发送音量 } void __switch_to_fm() { // TODO: 切换到FM模式 } void __switch_to_am() { // TODO: 切换到AM模式 } void app_init() { // 程序开始时执行一次 ui_screen_main(); kp_query(); printf("\n====start====\n"); } void app_main_loop() { // 反复被调用执行 uint8_t event = kp_query(); uint8_t event_type = kp_Type(event); uint8_t event_value = kp_Value(event); if (event_type == kp_ROTATE_RIGHT || event_type == kp_ROTATE_LEFT) { if (global_data.point_mode & G_PMOD_MODE_MASK) { // 指针模式调整 uint8_t p_pos = bit_fetch(global_data.point_mode, G_POMD_POS_BOFF, G_POMD_POS_MASK); if (global_data.point_mode & G_PMOD_PMOVE_MASK) { // 调整指针位置 if (event_type == kp_ROTATE_RIGHT) { p_pos += 100; p_pos -= event_value; } else { p_pos += event_value; } p_pos %= 5; // 0~4, 右边第一个数值为0号位置 bit_replace(global_data.point_mode, p_pos, G_POMD_POS_BOFF, G_POMD_POS_MASK); printf("Adjust Point Pos: %u\n", p_pos); } else { // 调整指针位置的数字 uint16_t num_freq; if (global_data.rf_mode == G_RF_MODE_AM) { num_freq = global_data.freq; } else { num_freq = global_data.freq * 5; } /* adjust number at all */ uint16_t scale = 1; if (p_pos == 0 && global_data.rf_mode == G_RF_MODE_FM) { scale = 5; } else { uint8_t tmp; for (tmp = 0; tmp < p_pos; tmp ++) { scale *= 10; } } if (event_type == kp_ROTATE_RIGHT) { num_freq += scale * event_value; } else { if (scale * event_value < num_freq) { num_freq -= scale * event_value; } else { num_freq = 0; } } uint16_t limit_min = (global_data.rf_mode == G_RF_MODE_AM) ? G_AM_FREQ_MIN : G_FM_FREQ_MIN * 5; uint16_t limit_max = (global_data.rf_mode == G_RF_MODE_AM) ? G_AM_FREQ_MAX : G_FM_FREQ_MAX * 5; if (num_freq > limit_max || num_freq < limit_min) { return; // ignore if overflow } /* adjust number one by one */ // uint8_t scale = 1; // uint16_t tmp; // uint16_t num_lower, num_upper; // uint8_t inc = (p_pos == 0 && global_data.rf_mode == G_RF_MODE_FM) ? 5 : 1; // uint8_t lim = 10; // if (p_pos == 4) { // if (global_data.rf_mode == G_RF_MODE_AM) { // lim = 3; // } else { // lim = 1; // } // } // for (tmp = 0; tmp < p_pos; tmp ++) { // scale *= 10; // } // num_lower = num_freq % scale; // num_upper = num_freq / scale / 10; // tmp = num_freq / scale % 10; // if (event_type == kp_ROTATE_RIGHT) { // tmp += (event_value * inc); // } else { // tmp += lim % 100; // tmp -= (event_value * inc); // } // tmp %= lim; // num_freq = (num_upper * scale * 10) + (tmp * scale) + num_lower; if (global_data.rf_mode == G_RF_MODE_AM) { global_data.freq = num_freq; } else { global_data.freq = num_freq / 5; } printf("Adjust Freq: %u\n", global_data.freq); __limit_freq_range(); __send_freq(); } } else { // 直接调整频率 if (event_type == kp_ROTATE_RIGHT) { global_data.freq += event_value; } else { global_data.freq -= event_value; } printf("Adjust Freq: %u\n", global_data.freq); __limit_freq_range(); __send_freq(); } ui_com_freq_digital(1, 0); } else if (event_type == kp_SHORT_CLICK) { if (global_data.point_mode & G_PMOD_MODE_MASK) { if (event_value == kp_KEYENCODER) { if (global_data.point_mode & G_PMOD_PMOVE_MASK) { // 退出移动指针模式 bit_clear(global_data.point_mode, G_PMOD_PMOVE_MASK); printf("Exit Pointer Move Mode.\n"); } else { // 进入移动指针模式 bit_set(global_data.point_mode, G_PMOD_PMOVE_MASK); uint8_t blink_state = (ticks_ms() / UI_BLINK_MS) % 2; // 该时间段指示条消失 if (blink_state) { bit_set(global_data.point_mode, G_PMOD_PBLINK_MASK); } else { bit_clear(global_data.point_mode, G_PMOD_PBLINK_MASK); } printf("Enter Pointer Move Mode.\n"); } ui_com_freq_digital(1, 0); } } else if (event_value == kp_KEY1) { // 切换FM和AM if (global_data.rf_mode == G_RF_MODE_AM) { global_data.rf_mode = G_RF_MODE_FM; global_data.freq = G_FM_FREQ_MIN; __switch_to_fm(); } else { global_data.rf_mode = G_RF_MODE_AM; global_data.freq = G_AM_FREQ_MIN; __switch_to_am(); } ui_com_fm_am(1); ui_com_freq_digital(1, 0); } } else if (event_type == kp_LONG_PRESS) { if (event_value == kp_KEYENCODER) { if (global_data.point_mode & G_PMOD_MODE_MASK) { // 退出指针模式 bit_clear(global_data.point_mode, G_PMOD_MODE_MASK); printf("Exit Pointer Mode.\n"); } else { // 进入指针模式 bit_set(global_data.point_mode, G_PMOD_MODE_MASK); bit_clear(global_data.point_mode, G_PMOD_PMOVE_MASK); printf("Enter Pointer Mode.\n"); } ui_com_freq_digital(1, 0); } } else if (event_type != kp_NOP) { printf("event: %u, key: %u\n", event_type, event_value); } ui_screen_main_animation(); }