|
| 1 | +/** |
| 2 | + * @file main.c |
| 3 | + * @author Giulio (giulio@glgprograms.it) |
| 4 | + * @brief Kickstart and setup code |
| 5 | + * @version 0.1 |
| 6 | + * @date 2022-03-03 |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2022 |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#include <avr/interrupt.h> |
| 13 | +#include <avr/io.h> |
| 14 | +#include <util/delay.h> |
| 15 | + |
| 16 | +#include "src/clock.h" |
| 17 | +#include "src/floppy_main.h" |
| 18 | +#include "src/prompt.h" |
| 19 | +#include "util/port.h" |
| 20 | + |
| 21 | +#include "src/sdcard.h" |
| 22 | +#include "src/sdcard_gpio.h" |
| 23 | +#include "src/sdcard_nic.h" |
| 24 | +#include "src/sdcard_fat.h" |
| 25 | + |
| 26 | +#include "src/oled_gui.h" |
| 27 | +#include "src/oled_lcd.h" |
| 28 | +#include "src/encoder.h" |
| 29 | + |
| 30 | +#include "debug.h" |
| 31 | + |
| 32 | +// Enable GUI |
| 33 | +#define DISPLAY_ENABLED (true) |
| 34 | + |
| 35 | +static void cli_mainloop() __attribute__((noreturn)); |
| 36 | +static void gui_mainloop() __attribute__((noreturn)); |
| 37 | +static void display_update_file_list(uint16_t fat_idx); |
| 38 | + |
| 39 | +int main() |
| 40 | +{ |
| 41 | + // If display is not connected or disabled, use CLI |
| 42 | + bool cli_mode = true; |
| 43 | + |
| 44 | + // Set up PLL to reach desired clock speed |
| 45 | + clk_init(); |
| 46 | + |
| 47 | + // Configure SDcard I/O |
| 48 | + sdcard_init(); |
| 49 | + |
| 50 | + // Configure prompt uart |
| 51 | + prompt_init(); |
| 52 | + |
| 53 | + // Configure floppy |
| 54 | + floppy_init(); |
| 55 | + |
| 56 | + // Initialize OLED display |
| 57 | + if (DISPLAY_ENABLED) |
| 58 | + cli_mode = !oled_init(); |
| 59 | + |
| 60 | + PMIC.CTRL |= PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm; |
| 61 | + sei(); |
| 62 | + |
| 63 | + if (cli_mode) |
| 64 | + cli_mainloop(); |
| 65 | + else |
| 66 | + gui_mainloop(); |
| 67 | +} |
| 68 | + |
| 69 | +static void cli_mainloop() |
| 70 | +{ |
| 71 | + while (1) |
| 72 | + { |
| 73 | + prompt_main(); |
| 74 | + |
| 75 | + // Do nothing if drive is disabled |
| 76 | + if (floppy_drive_enabled() && nic_file_selected()) |
| 77 | + { |
| 78 | + debug_printP(PSTR("Reading enabled\n\r")); |
| 79 | + floppy_main(); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +static void gui_mainloop() |
| 85 | +{ |
| 86 | + encoder_init(); |
| 87 | + |
| 88 | + // Currently "hovered" fat entry |
| 89 | + uint16_t fat_entry; |
| 90 | + // Currently selected folder and file names |
| 91 | + char file_name[FAT_FILENAME_STRLEN] = ""; |
| 92 | + char cwd_name[FAT_FILENAME_STRLEN] = ""; |
| 93 | + |
| 94 | + oled_gui_volume_name(NULL, false); |
| 95 | + |
| 96 | + while (1) |
| 97 | + { |
| 98 | + if (!sdcard_present()) |
| 99 | + { |
| 100 | + oled_gui_error("Please insert an SD card"); |
| 101 | + |
| 102 | + while (!sdcard_present()) |
| 103 | + ; |
| 104 | + // "Debouncing", assure that card is correctly inserted |
| 105 | + _delay_ms(500); |
| 106 | + } |
| 107 | + |
| 108 | + oled_gui_error("Initializing SD card, please wait"); |
| 109 | + |
| 110 | + // Initialize SD card |
| 111 | + if (!sdcard_card_init() || !sdcard_fat_init()) |
| 112 | + { |
| 113 | + oled_gui_error("Error: SDcard failed in initialization"); |
| 114 | + // Must remove SDcard to proceed |
| 115 | + while (sdcard_present()) |
| 116 | + ; |
| 117 | + |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + oled_gui_volume_name(NULL, sdcard_locked()); |
| 122 | + oled_gui_cwd_currentfile(NULL, NULL); |
| 123 | + |
| 124 | + // Populate file list on home screen |
| 125 | + fat_entry = 0xFFFF; |
| 126 | + // TODO if fail, error: no file in this SDCARD |
| 127 | + fat_next(&fat_entry); |
| 128 | + display_update_file_list(fat_entry); |
| 129 | + |
| 130 | + while (sdcard_present()) |
| 131 | + { |
| 132 | + int8_t delta = encoder_update(); |
| 133 | + // Update file pointer and eventually update file list |
| 134 | + if (delta != 0) |
| 135 | + { |
| 136 | + if (delta > 0) fat_next(&fat_entry); |
| 137 | + if (delta < 0) fat_prev(&fat_entry); |
| 138 | + display_update_file_list(fat_entry); |
| 139 | + } |
| 140 | + |
| 141 | + if (encoder_clicked()) |
| 142 | + { |
| 143 | + if (fat_is_directory(fat_entry)) |
| 144 | + { |
| 145 | + // Get directory name |
| 146 | + fat_get_entry_name(cwd_name, fat_entry); |
| 147 | + // Enter in directory |
| 148 | + fat_cwd(fat_entry); |
| 149 | + // Reset pointer in directory entry table, then validate for first file |
| 150 | + fat_entry = 0xFFFF; |
| 151 | + fat_next(&fat_entry); |
| 152 | + // Update display with folder content |
| 153 | + display_update_file_list(fat_entry); |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + bool ro = sdcard_locked() || fat_is_ro(fat_entry); |
| 158 | + nic_build_fat(fat_entry); |
| 159 | + floppy_write_protect(ro); |
| 160 | + fat_get_entry_name(file_name, fat_entry); |
| 161 | + // FIXME strip extension |
| 162 | + for(uint8_t i = 0; i <= 8; i++) |
| 163 | + if (file_name[i] == '.') |
| 164 | + { |
| 165 | + file_name[i] = '\0'; |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + oled_gui_cwd_currentfile(cwd_name, file_name); |
| 170 | + } |
| 171 | + |
| 172 | + // Do nothing if drive is disabled |
| 173 | + if (floppy_drive_enabled() && nic_file_selected()) |
| 174 | + { |
| 175 | + debug_printP(PSTR("Reading enabled\n\r")); |
| 176 | + |
| 177 | + oled_gui_set_busyflag(true); |
| 178 | + |
| 179 | + // While floppy is selected, floppy_main will never return |
| 180 | + floppy_main(); |
| 181 | + |
| 182 | + oled_gui_set_busyflag(false); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +static void display_update_file_list(uint16_t fat_idx) |
| 189 | +{ |
| 190 | + // Invert for first file (hovered) |
| 191 | + oled_invert(true); |
| 192 | + |
| 193 | + int i; |
| 194 | + |
| 195 | + for (i = 0; i < 6; i++) |
| 196 | + { |
| 197 | + char filename[FAT_FILENAME_STRLEN]; |
| 198 | + |
| 199 | + fat_get_entry_name(filename, fat_idx); |
| 200 | + |
| 201 | + oled_gui_fileline(filename, fat_is_directory(fat_idx), fat_is_ro(fat_idx), i); |
| 202 | + |
| 203 | + oled_invert(false); |
| 204 | + |
| 205 | + // If no more files, clean lines then stop |
| 206 | + if (!fat_next(&fat_idx)) break; |
| 207 | + } |
| 208 | + |
| 209 | + for (i++; i < 6; i++) |
| 210 | + oled_gui_fileline(NULL, false, false, i); |
| 211 | +} |
0 commit comments