-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathFileIO.h
More file actions
482 lines (417 loc) · 20 KB
/
FileIO.h
File metadata and controls
482 lines (417 loc) · 20 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* @cond
* The following section will be excluded from the documentation.
*/
/* *********************************************************************************************************************
PicoMite MMBasic
FileIO.h
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
#ifndef __FILEIO_H
#define __FILEIO_H
#ifdef __cplusplus
extern "C"
{
#endif
/* ============================================================================
* Includes
* ============================================================================ */
#include "ff.h"
#ifdef rp2350
#include "upng.h"
#endif
/* ============================================================================
* Constants - Flash storage regions
* ============================================================================ */
#define SAVED_OPTIONS_FLASH 5
#define LIBRARY_FLASH 6
#define SAVED_VARS_FLASH 7
#define PROGRAM_FLASH 8
#define RoundUptoPage(a) ((((uint64_t)a) + (uint64_t)(255)) & (uint64_t)(~(255))) // round up to the nearest page
/* ============================================================================
* Constants - File types
* ============================================================================ */
enum
{
NONEFILE,
FLASHFILE,
FATFSFILE
};
/* ============================================================================
* Type definitions - File table union
* ============================================================================ */
typedef union uFileTable
{
unsigned int com;
FIL *fptr;
lfs_file_t *lfsptr;
} u_file;
/* ============================================================================
* Type definitions - Options structure
* ============================================================================ */
typedef struct
{
int width;
int height;
int bitsPerPixel;
int linesProcessed;
bool success;
char errorMsg[256];
} BMP_Result;
struct option_s
{
/* Basic settings */
int Magic;
char Autorun;
char Tab;
char Invert;
char Listcase; // 8 bytes
/* Memory configuration */
unsigned int PROG_FLASH_SIZE;
unsigned int HEAP_SIZE;
/* Display dimensions */
#ifndef PICOMITEVGA
char Height;
char Width;
#else
short d2;
#endif
/* Display configuration */
unsigned char DISPLAY_TYPE;
char DISPLAY_ORIENTATION; // 12-20 bytes
/* Security and communication */
int PIN;
int Baudrate;
int8_t ColourCode;
unsigned char MOUSE_CLOCK;
unsigned char MOUSE_DATA;
char spare;
int CPU_Speed;
unsigned int Telnet; // Also stores size of program flash (start of LIBRARY code)
/* Color settings */
int DefaultFC, DefaultBC; // Default colors
short version; // 40 bytes
/* Keyboard configuration */
unsigned char KEYBOARD_CLOCK;
unsigned char KEYBOARD_DATA;
unsigned char continuation;
unsigned char LOCAL_KEYBOARD;
unsigned char KeyboardBrightness;
uint8_t special; // used for special board configurations
/* Font and RTC */
unsigned char DefaultFont;
unsigned char KeyboardConfig;
unsigned char RTC_Clock;
unsigned char RTC_Data; // 60 bytes
/* Platform-specific configuration */
#if PICOMITERP2350
unsigned char LCD_CLK;
unsigned char LCD_MOSI;
unsigned char LCD_MISO;
char dummy; // 64 bytes
#endif
#if defined(PICOMITE) && !defined(rp2350)
char dummy[4]; // 64 bytes
#endif
#ifdef PICOMITEWEB
uint16_t TCP_PORT;
uint16_t ServerResponceTime;
#endif
#ifdef PICOMITEVGA
int16_t X_TILE;
int16_t Y_TILE;
#endif
/* SPI LCD pins */
unsigned char LCD_CD;
unsigned char LCD_CS;
unsigned char LCD_Reset;
/* Touch screen configuration */
unsigned char TOUCH_CS;
unsigned char TOUCH_IRQ;
char TOUCH_SWAPXY;
unsigned char repeat;
char disabletftp; // 72 bytes
/* Touch calibration */
#ifndef PICOMITEVGA
int TOUCH_XZERO;
int TOUCH_YZERO;
float TOUCH_XSCALE;
float TOUCH_YSCALE; // 88 bytes
#else
short Height;
short Width;
char dummy[12];
#endif
/* GUI or HDMI configuration */
#ifdef GUICONTROLS
uint8_t MaxCtrls;
unsigned char spare3[3];
#else
uint8_t HDMIclock;
uint8_t HDMId0;
uint8_t HDMId1;
uint8_t HDMId2;
#endif
/* Flash and SD card */
unsigned int FlashSize; // 96 bytes
unsigned char SD_CS;
unsigned char SYSTEM_MOSI;
unsigned char SYSTEM_MISO;
unsigned char SYSTEM_CLK;
/* Display backlight and console */
unsigned char DISPLAY_BL;
unsigned char DISPLAY_CONSOLE;
unsigned char TOUCH_Click;
char LCD_RD; // Used for RD pin for SSD1963, 104 bytes
/* Audio configuration */
unsigned char AUDIO_L;
unsigned char AUDIO_R;
unsigned char AUDIO_SLICE;
unsigned char SDspeed;
unsigned char pins[3]; // General use storage for CFunctions
/* Touch and display */
unsigned char TOUCH_CAP;
unsigned char SSD_DATA;
unsigned char THRESHOLD_CAP;
unsigned char audio_i2s_data;
unsigned char audio_i2s_bclk;
char LCDVOP;
char I2Coffset;
unsigned char NoHeartbeat;
char Refresh;
/* System I2C and RTC */
unsigned char SYSTEM_I2C_SDA;
unsigned char SYSTEM_I2C_SCL;
unsigned char RTC;
char PWM; // 124 bytes
/* Interrupt pins */
unsigned char INT1pin;
unsigned char INT2pin;
unsigned char INT3pin;
unsigned char INT4pin;
/* SD card pins */
unsigned char SD_CLK_PIN;
unsigned char SD_MOSI_PIN;
unsigned char SD_MISO_PIN;
/* Serial console */
unsigned char SerialConsole; // 132 bytes
unsigned char SerialTX;
unsigned char SerialRX;
/* Keyboard lock status */
unsigned char numlock;
unsigned char capslock; // 136 bytes
/* Library flash size */
unsigned int LIBRARY_FLASH_SIZE; // 140 bytes
/* Audio pins */
unsigned char AUDIO_CLK_PIN;
unsigned char AUDIO_MOSI_PIN;
unsigned char SYSTEM_I2C_SLOW;
unsigned char AUDIO_CS_PIN; // 144 bytes
/* Network configuration (PICOMITEWEB) */
#ifdef PICOMITEWEB
uint16_t UDP_PORT;
uint16_t UDPServerResponceTime;
char hostname[28];
char ipaddress[16];
char mask[16];
char gateway[16];
#else
float mousespeed;
unsigned char x[76]; // 229 bytes
#endif
/* Miscellaneous pins and settings */
unsigned short GPSBaud;
unsigned char GPSRX;
unsigned char GPSTX;
unsigned char heartbeatpin;
unsigned char PSRAM_CS_PIN;
unsigned char BGR;
unsigned char NoScroll;
unsigned char CombinedCS;
unsigned char USBKeyboard;
unsigned char VGA_HSYNC;
unsigned char VGA_BLUE; // 236 bytes
/* Additional audio pins */
unsigned char AUDIO_MISO_PIN;
unsigned char AUDIO_DCS_PIN;
unsigned char AUDIO_DREQ_PIN;
unsigned char AUDIO_RESET_PIN;
/* SSD display pins */
unsigned char SSD_DC;
unsigned char SSD_WR;
unsigned char SSD_RD;
signed char SSD_RESET; // 244 bytes
/* Display and reset settings */
unsigned char BackLightLevel;
unsigned char NoReset;
unsigned char AllPins;
unsigned char modbuff; // 248 bytes
/* Keyboard repeat settings */
short RepeatStart;
short RepeatRate;
int modbuffsize; // 256 bytes
/* Function keys and network credentials */
unsigned char F1key[MAXKEYLEN];
unsigned char F5key[MAXKEYLEN];
unsigned char F6key[MAXKEYLEN];
unsigned char F7key[MAXKEYLEN];
unsigned char F8key[MAXKEYLEN];
unsigned char F9key[MAXKEYLEN];
unsigned char SSID[MAXKEYLEN];
unsigned char PASSWORD[MAXKEYLEN]; // 768 bytes
/* Platform identification and extensions */
unsigned char platform[32];
uint8_t BACKLIGHT_KBD; // *EB*
uint8_t BACKLIGHT_LCD; // *EB*
uint16_t D4; // *EB*
unsigned char extensions[92]; // 896 bytes == 7 XMODEM blocks
/* NOTE: To enable older CFunctions to run, any new options MUST be added at the end of the list */
} __attribute__((packed));
/* ============================================================================
* External variables - File tables and options
* ============================================================================ */
extern union uFileTable FileTable[MAXOPENFILES + 1];
extern struct option_s Option;
extern unsigned char filesource[MAXOPENFILES + 1];
/* ============================================================================
* External variables - Flash and file system
* ============================================================================ */
extern unsigned char *CFunctionFlash, *CFunctionLibrary;
extern const uint8_t *flash_option_contents;
extern volatile uint32_t realflashpointer;
extern int FlashLoad;
extern int FatFSFileSystemSave;
extern int FSerror;
extern int lfs_FileFnbr;
extern struct lfs_config pico_lfs_cfg;
extern const uint8_t *flash_target_contents;
extern int BMPfnbr;
/* ============================================================================
* External variables - Error handling
* ============================================================================ */
extern int OptionFileErrorAbort;
/* ============================================================================
* Function declarations - File operations
* ============================================================================ */
void MMfopen(unsigned char *fname, unsigned char *mode, int fnbr);
void MMfclose(int fnbr);
unsigned char MMfputc(unsigned char c, int fnbr);
int MMfgetc(int filenbr);
int MMfeof(int filenbr);
void MMgetline(int filenbr, char *p);
int ExistsFile(char *p);
int ExistsDir(char *p, char *q, int *filesystem);
int FileSize(char *p);
extern bool (*linecallback)(int *imagewidth, int *imageheight, uint32_t *linedata, int *linenumber);
extern BMP_Result decodeBMP(bool topdown);
void decodeBMPheader(int *width, int *height);
/* ============================================================================
* Function declarations - File management
* ============================================================================ */
int FindFreeFileNbr(void);
void CloseAllFiles(void);
int ForceFileClose(int fnbr);
void ErrorCheck(int fnbr);
int FileEOF(int fnbr);
/* ============================================================================
* Function declarations - Character and string I/O
* ============================================================================ */
char FileGetChar(int fnbr);
char FilePutChar(char c, int fnbr);
// void FilePutStr(int count, char *c, int fnbr);
void FilePutData(char *c, int fnbr, int n);
int __not_in_flash_func(FileGetData)(int fnbr, void *buff, int count, unsigned int *read);
/* ============================================================================
* Function declarations - File positioning
* ============================================================================ */
void positionfile(int fnbr, int idx, bool noread);
/* ============================================================================
* Function declarations - Program loading
* ============================================================================ */
int FileLoadProgram(unsigned char *fname, bool chain);
int FileLoadCMM2Program(char *fname, bool message);
/* ============================================================================
* Function declarations - Options and configuration
* ============================================================================ */
void LoadOptions(void);
void SaveOptions(void);
void ResetOptions(bool startup);
/* ============================================================================
* Function declarations - Flash operation safety wrappers
* ============================================================================ */
#ifdef rp2350
void safe_flash_range_erase(uint32_t flash_offs, size_t count);
void safe_flash_range_program(uint32_t flash_offs, const uint8_t *data, size_t count);
#else
#define safe_flash_range_erase flash_range_erase
#define safe_flash_range_program flash_range_program
#endif
/* ============================================================================
* Function declarations - Flash operations
* ============================================================================ */
void FlashWriteInit(int region);
void FlashWriteBlock(void);
void FlashWriteWord(unsigned int i);
void FlashWriteByte(unsigned char b);
void FlashWriteAlign(void);
void FlashWriteAlignWord(void);
void FlashWriteClose(void);
void FlashSetAddress(int address);
void ResetAllFlash(void);
void ResetFlashStorage(int umount);
/* ============================================================================
* Function declarations - Saved variables
* ============================================================================ */
void ClearSavedVars(void);
/* ============================================================================
* Function declarations - Data compression
* ============================================================================ */
void CrunchData(unsigned char **p, int c);
/* ============================================================================
* Function declarations - SD card
* ============================================================================ */
void CheckSDCard(void);
/* ============================================================================
* Function declarations - Interrupt control
* ============================================================================ */
void disable_interrupts_pico(void);
void enable_interrupts_pico(void);
/* ============================================================================
* Function declarations - File system utilities
* ============================================================================ */
int drivecheck(char *p, int *waste);
void getfullfilename(char *fname, char *q);
char *GetCWD(void);
unsigned short hashversion(void);
/* ============================================================================
* Function declarations - Output
* ============================================================================ */
void MMPrintString(char *s);
void CheckAbort(void);
// Convert RGB888 to RGB121 with dithering
uint8_t rgb888_to_rgb121_dither(int16_t r, int16_t g, int16_t b);
// Convert RGB888 to RGB332 with dithering
uint8_t rgb888_to_rgb332_dither(int16_t r, int16_t g, int16_t b);
// Convert RGB888 to RGB222 with dithering
uint8_t rgb888_to_rgb222_dither(int16_t r, int16_t g, int16_t b);
#ifdef __cplusplus
}
#endif
#endif /* __FILEIO_H */
/* @endcond */