From a6d091a7e54d4f4110e761161d766423b111900f Mon Sep 17 00:00:00 2001 From: Sheikah <> Date: Fri, 3 Feb 2023 01:22:15 +0800 Subject: [PATCH] many improvement --- .gitignore | 1 + Core/App/Graphic/Images/icon.h | 37 +++++++++++++ Core/App/Graphic/Images/img_stereo.h | 10 ++++ Core/App/Graphic/ui.c | 37 ++++++++----- Core/App/Graphic/ui.h | 2 + Core/App/app.c | 81 +++++++++++++++++++++++++--- Core/App/global.c | 5 +- Core/App/global.h | 6 ++- tools/img/stereo.pbm | 4 ++ tools/make_monoimg.py | 39 ++++++++++++++ tools/pbm.py | 2 +- 11 files changed, 201 insertions(+), 23 deletions(-) create mode 100644 Core/App/Graphic/Images/icon.h create mode 100644 Core/App/Graphic/Images/img_stereo.h create mode 100644 tools/img/stereo.pbm create mode 100644 tools/make_monoimg.py diff --git a/.gitignore b/.gitignore index 27edbc9..30decd2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ __pycache__ tools/**/*.png tools/**/*.psd tools/**/*.jpg +tools/**/*.xcf tools/**/*.c tools/**/*.h diff --git a/Core/App/Graphic/Images/icon.h b/Core/App/Graphic/Images/icon.h new file mode 100644 index 0000000..35008bc --- /dev/null +++ b/Core/App/Graphic/Images/icon.h @@ -0,0 +1,37 @@ +const uint16_t mute[][16] = { +{0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x05F8,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x60FF,0x0000,0x0000,0x04E0,0x0000,}, +{0x0000,0x0000,0x05F8,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x60FF,0x0000,0x05F0,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x05F8,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,0x04F0,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x60FF,0x60FF,0x60FF,0x60FF,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x60FF,0x05F8,0x0000,0x80FF,0x0000,0x0000,0x04F0,0x60FF,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x05F8,0x60FF,0x60FF,0x05F0,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,0x05F0,0x60FF,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,0x05F8,0x60FF,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x05F0,0x80FF,0x60FF,0x05F8,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x60FF,0x05F0,0x0000,0x60FF,0x0000,0x0000,0x05F8,0x60FF,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x60FF,0x60FF,0x60FF,0x60FF,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x05F0,0x0000,0x0000,0x0000,0x0000,0x80FF,0x0000,0x0000,0x60FF,0x04F8,0x0000,0x0000,0x0000,}, +{0x0000,0xA210,0x04F0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x60FF,0x0000,0x05F8,0x0000,0x0000,}, +{0x0000,0x04F0,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x60FF,0x0000,0x0000,0x05F8,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,}, +}; + +const uint16_t stereo[][16] = { +{0x0000,0x2000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x2000,}, +{0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x2000,0x4008,0x4008,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,}, +{0x0000,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,}, +{0x0000,0x60FF,0x0000,0x8010,0x6010,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,}, +{0x0000,0x60FF,0x4008,0x4008,0x60FF,0x0000,0x0000,0x80FF,0x60FF,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,0x0000,}, +{0x60FF,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,0x60FF,0x60FF,0x60FF,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,}, +{0x60FF,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,0x60FF,0x60FF,0x60FF,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,}, +{0x0000,0x60FF,0x0000,0x0000,0x60FF,0x0000,0x0000,0x60FF,0x60FF,0x0000,0x0000,0x60FF,0x0000,0x0000,0x80FF,0x0000,}, +{0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,}, +{0x2000,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,}, +{0x0000,0x0000,0x60FF,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x60FF,0x0000,0x0000,}, +{0x2000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,}, +{0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,}, +}; \ No newline at end of file diff --git a/Core/App/Graphic/Images/img_stereo.h b/Core/App/Graphic/Images/img_stereo.h new file mode 100644 index 0000000..f7c935e --- /dev/null +++ b/Core/App/Graphic/Images/img_stereo.h @@ -0,0 +1,10 @@ +#pragma once +#include + +const uint8_t IMG_STEREO[] = { + 0x0F, 0x09, 0x30, 0xCC, 0x03, 0x30, 0x48, 0x00, 0x30, 0x78, 0x78, 0x30, 0x00, 0x48, 0x30, 0x03, + 0xCC, 0x30, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, + 0x00, 0x00, +}; +const uint32_t DATA_SIZE_IMG_STEREO = 34; + diff --git a/Core/App/Graphic/ui.c b/Core/App/Graphic/ui.c index 8ae4a59..7ed34b5 100644 --- a/Core/App/Graphic/ui.c +++ b/Core/App/Graphic/ui.c @@ -4,6 +4,7 @@ #include "time.h" #include "st7735.h" #include "Images/digi18x32.h" +#include "Images/img_stereo.h" #include "monoimg.h" #include "bmfont.h" #include "asciifont.h" @@ -150,22 +151,16 @@ void ui_com_fm_am(uint8_t clear) { } } -void ui_com_progress_bar(uint8_t clear, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t border_color, uint16_t fill_color, uint32_t current, uint32_t total) { - if (clear) { - ST7735_FillRectangle(x, y, w, h, ST7735_BLACK); - } +void ui_com_progress_bar(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t border_color, uint16_t fill_color, uint32_t current, uint32_t total) { // border ST7735_FillRectangle(x, y, w, 1, border_color); ST7735_FillRectangle(x, y + h - 1, w, 1, border_color); ST7735_FillRectangle(x, y, 1, h, border_color); ST7735_FillRectangle(x + w - 1, y, 1, h, border_color); // bar - x += 1; - y += 1; - w -= 2; - h -= 2; - uint16_t bar_width = w * current / total; - ST7735_FillRectangle(x, y, bar_width, h, fill_color); + uint16_t bar_width = (w - 2) * current / total; + ST7735_FillRectangle(x + 1, y + 1, bar_width, h - 2, fill_color); + ST7735_FillRectangle(x + bar_width + 1, y + 1, (w - 2 - bar_width), h - 2, ST7735_BLACK); } void ui_com_vol_bar(uint8_t clear) { @@ -180,7 +175,13 @@ void ui_com_vol_bar(uint8_t clear) { bmf_draw_text(font_unifont_8x16, u8str("VOL"), 3, off_x, off_y, 32, 16, color_skyblue); // bar off_x = 1 + 32; - ui_com_progress_bar(clear, 33, off_y + 2, ST7735_WIDTH - 34 - 3, 13, ST7735_WHITE, color_skyblue, global_data.volumn, 16); + if (global_data.flag & G_FL_MUTE) { + color_skyblue = ST7735_COLOR565(0x20, 0x20, 0x20); + // ui_com_progress_bar(33, off_y + 2, ST7735_WIDTH - 34 - 3, 13, ST7735_WHITE, ST7735_COLOR565(0x20, 0x20, 0x20), G_VOLUMN_MAX, G_VOLUMN_MAX); + } else { + // ui_com_progress_bar(33, off_y + 2, ST7735_WIDTH - 34 - 3, 13, ST7735_WHITE, color_skyblue, global_data.volumn, G_VOLUMN_MAX); + } + ui_com_progress_bar(33, off_y + 2, ST7735_WIDTH - 34 - 3, 13, ST7735_WHITE, color_skyblue, global_data.volumn, G_VOLUMN_MAX); } void ui_com_sig_bar(uint8_t clear) { @@ -194,7 +195,18 @@ void ui_com_sig_bar(uint8_t clear) { bmf_draw_text(font_unifont_8x16, u8str("SIG"), 3, off_x, off_y, 32, 16, ST7735_GREEN); // bar off_x = 1 + 32; - ui_com_progress_bar(clear, 33, off_y + 1, ST7735_WIDTH - 34 - 3, 13, ST7735_WHITE, ST7735_GREEN, global_data.signal, 255); + ui_com_progress_bar(33, off_y + 1, ST7735_WIDTH - 34 - 3, 13, ST7735_WHITE, ST7735_GREEN, global_data.signal, G_RSSI_MAX); +} + +/* 图标 */ +void icon_stereo() { + mimg_Area area = mimg_get_tile_area(IMG_STEREO, 1, 1, 0); + // margin 1px from left top border + if (global_data.flag & G_FL_MONO) { + ST7735_FillRectangle(8, 25, area.w, area.h, ST7735_BLACK); + } else { + mimg_draw_with_bg(ST7735_DrawPixel, 8, 25, ST7735_YELLOW, ST7735_BLACK, IMG_STEREO, area); + } } /* 供外部调用的方法 */ @@ -206,6 +218,7 @@ void ui_screen_main() { ui_com_freq_digital(0, 0); ui_com_vol_bar(0); ui_com_sig_bar(0); + icon_stereo(); } void ui_screen_main_animation() { diff --git a/Core/App/Graphic/ui.h b/Core/App/Graphic/ui.h index 5c58dc9..32d8877 100644 --- a/Core/App/Graphic/ui.h +++ b/Core/App/Graphic/ui.h @@ -11,6 +11,8 @@ void ui_com_freq_digital(uint8_t clear, uint8_t only_pointer); void ui_com_fm_am(uint8_t clear); void ui_com_vol_bar(uint8_t clear); void ui_com_sig_bar(uint8_t clear); +/* 图标 */ +void icon_stereo(); /* 绘制主界面 */ void ui_screen_main(); void ui_screen_main_animation(); diff --git a/Core/App/app.c b/Core/App/app.c index 8b4a88a..21a551c 100644 --- a/Core/App/app.c +++ b/Core/App/app.c @@ -4,6 +4,7 @@ #include "keypad.h" #include "global.h" #include "time.h" +#include "kt0915.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; @@ -16,26 +17,69 @@ void __limit_freq_range() { } void __send_freq() { - // TODO: 发送频率 + if (global_data.rf_mode == G_RF_MODE_AM) { + KT0915_setFrequency(global_data.freq); + printf("Set Frequency to %u kHz\n", global_data.freq); + } else { + KT0915_setFrequency(global_data.freq * 50ul); + printf("Set Frequency to %lu kHz\n", global_data.freq * 50ul); + } } void __send_volumn() { - // TODO: 发送音量 + KT0915_setVolume(global_data.volumn); + printf("Set Volumn to %u\n", global_data.volumn); } void __switch_to_fm() { - // TODO: 切换到FM模式 + KT0915_setFM(G_FM_FREQ_MIN * 50ul, G_FM_FREQ_MAX * 50ul, G_FM_FREQ_MIN * 50ul, 50u); + printf("Switched to FM Mode\n"); + __send_freq(); } void __switch_to_am() { - // TODO: 切换到AM模式 + KT0915_setAM(G_AM_FREQ_MIN, G_AM_FREQ_MAX, G_AM_FREQ_MIN, 1, 0); + printf("Switched to AM Mode\n"); + __send_freq(); +} + +uint16_t __read_rssi() { + if (global_data.rf_mode == G_RF_MODE_AM) { + return (uint16_t)KT0915_getAmRssi() / 3; + } else { + return (uint16_t)KT0915_getFmRssi() / 3; + } +} + +void __init_kt0915() { + KT0915_enable(1); + KT0915_setup(OSCILLATOR_32KHZ, 0); + KT0915_setSoftMute(1); + KT0915_setMono(0); + uint16_t did = KT0915_getDeviceId(); + printf("kt0915 device id: %X\n", did); + __switch_to_fm(); + __send_volumn(); + KT0915_setSoftMute(0); +} + +static int32_t target_time_stamp = 0; +void timer_event() { + if (ticks_diff(ticks_ms(), target_time_stamp) > 0) { + target_time_stamp = ticks_add(target_time_stamp, 500); + // update rssi + global_data.signal = __read_rssi(); + ui_com_sig_bar(0); + } } void app_init() { // 程序开始时执行一次 - ui_screen_main(); - kp_query(); printf("\n====start====\n"); + target_time_stamp = ticks_add(ticks_ms(), 500); + kp_query(); + __init_kt0915(); + ui_screen_main(); } void app_main_loop() { @@ -128,6 +172,9 @@ void app_main_loop() { } } else { // 直接调整频率 + if (global_data.rf_mode == G_RF_MODE_FM) { + event_value = event_value * 2; + } if (event_type == kp_ROTATE_RIGHT) { global_data.freq += event_value; } else { @@ -171,6 +218,18 @@ void app_main_loop() { } ui_com_fm_am(1); ui_com_freq_digital(1, 0); + } else if (event_value == kp_KEY3) { + if (global_data.volumn < G_VOLUMN_MAX) { + global_data.volumn += 1; + __send_volumn(); + ui_com_vol_bar(0); + } + } else if (event_value == kp_KEY5) { + if (global_data.volumn > 0) { + global_data.volumn -= 1; + __send_volumn(); + ui_com_vol_bar(0); + } } } else if (event_type == kp_LONG_PRESS) { if (event_value == kp_KEYENCODER) { @@ -185,9 +244,19 @@ void app_main_loop() { printf("Enter Pointer Mode.\n"); } ui_com_freq_digital(1, 0); + } else if (event_value == kp_KEY5) { + if (global_data.flag & G_FL_MUTE) { + KT0915_setAudioMute(0); + CLEAR_BIT(global_data.flag, G_FL_MUTE); + } else { + KT0915_setAudioMute(1); + SET_BIT(global_data.flag, G_FL_MUTE); + } + ui_com_vol_bar(0); } } else if (event_type != kp_NOP) { printf("event: %u, key: %u\n", event_type, event_value); } + timer_event(); ui_screen_main_animation(); } diff --git a/Core/App/global.c b/Core/App/global.c index c15e127..d3208b2 100644 --- a/Core/App/global.c +++ b/Core/App/global.c @@ -3,9 +3,10 @@ GlobalData global_data = { 0b0000000000000000, //flag - 880 * 2, // freq + // 880 * 2, // freq + 893 * 2, // freq G_RF_MODE_FM, //rf_mode 16, // volumn - 97, //signal + 0, //signal 0b00000000 //point_mode }; diff --git a/Core/App/global.h b/Core/App/global.h index 8e156e9..c2e9927 100644 --- a/Core/App/global.h +++ b/Core/App/global.h @@ -3,14 +3,16 @@ #include #define G_RF_MODE_FM 0x00 #define G_RF_MODE_AM 0x01 -#define G_FM_FREQ_MIN 880*2 +#define G_FM_FREQ_MIN 330*2 #define G_FM_FREQ_MAX 1080*2 #define G_AM_FREQ_MIN 100 #define G_AM_FREQ_MAX 33000 #define G_VOLUMN_MIN 0 #define G_VOLUMN_MAX 31 +#define G_RSSI_MAX 0b11111 -#define G_FL_SIGNAL 0b0000000000000001 +#define G_FL_MUTE 0b0000000000000001 +#define G_FL_MONO 0b0000000000000010 #define G_PMOD_MODE_MASK 0b00000001 #define G_PMOD_PMOVE_MASK 0b00000010 diff --git a/tools/img/stereo.pbm b/tools/img/stereo.pbm new file mode 100644 index 0000000..eded937 --- /dev/null +++ b/tools/img/stereo.pbm @@ -0,0 +1,4 @@ +P4 +# Created by GIMP version 2.10.32 PNM plug-in +16 10 +  @IɓI@   \ No newline at end of file diff --git a/tools/make_monoimg.py b/tools/make_monoimg.py new file mode 100644 index 0000000..af96716 --- /dev/null +++ b/tools/make_monoimg.py @@ -0,0 +1,39 @@ +import framebuf +import pbm +import math +import argparse + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("pbm_image_file", help="PBM fotmat mono image.", type=argparse.FileType("rb")) + parser.add_argument("img_name", help="generated image name.") + args = parser.parse_args() + img_name = args.img_name.upper().replace("-", "_").replace(" ", "_").replace(".", "_") + iw, ih, _fmt, data, _ = pbm.read_image(args.pbm_image_file) + img = framebuf.FrameBuffer(data, iw, ih, framebuf.MONO_HLSB) + buffer_frame = bytearray(iw * math.ceil(ih / 8)) + frame = framebuf.FrameBuffer(buffer_frame, iw, ih, framebuf.MONO_VLSB) + for y in range(ih): + for x in range(iw): + frame.pixel(x, y, img.pixel(x, y)) + + data = bytearray() + data.append(iw - 1) # image width - 1 + data.append(ih - 1) # image height - 1 + data.extend(buffer_frame) # image data in MONO_VLSB format + f = open(f"img_{img_name.lower()}.h", "wt") + f.write("#pragma once\n") + f.write("#include \n") + f.write("\n") + f.write(f"const uint8_t IMG_{img_name}[] = {{") + for i, byte in enumerate(data): + if i % 16 == 0: + f.write("\n ") + f.write(f" 0x{byte:02X},") + f.write("\n};\n") + f.write(f"const uint32_t DATA_SIZE_IMG_{img_name} = {len(data)};\n") + f.write("\n") + f.close() + + print(f"Image Name: {img_name}") + print(f"Header File Name: img_{img_name.lower()}.h") \ No newline at end of file diff --git a/tools/pbm.py b/tools/pbm.py index a206b16..115f8e9 100644 --- a/tools/pbm.py +++ b/tools/pbm.py @@ -29,7 +29,7 @@ def _read_header(instream): instream.seek(0) mg = instream.read(2) if mg != b"P1" and mg != b"P4": - return (-1, -1, b"UNKNOWN", -1) + return (-1, -1, b"UNKNOWN", -1, b"") # state machine comment = bytearray() buffer = bytearray() # list to store bytes