Headers for MAX32630FTHR Demo Board sample programs
generalinterface.cpp@0:a6a8578e95af, 2019-07-30 (annotated)
- Committer:
- Lugs
- Date:
- Tue Jul 30 02:44:30 2019 +0000
- Revision:
- 0:a6a8578e95af
actually working header-using version of mini piano player
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lugs | 0:a6a8578e95af | 1 | #include "generalinterface.h" |
Lugs | 0:a6a8578e95af | 2 | |
Lugs | 0:a6a8578e95af | 3 | Serial daplink(P2_1,P2_0); |
Lugs | 0:a6a8578e95af | 4 | USBSerial microUSB; |
Lugs | 0:a6a8578e95af | 5 | SDBlockDevice bd(P0_5, P0_6, P0_4, P0_7); |
Lugs | 0:a6a8578e95af | 6 | FATFileSystem fs("fs"); |
Lugs | 0:a6a8578e95af | 7 | USBMSD_BD msd(&bd); |
Lugs | 0:a6a8578e95af | 8 | |
Lugs | 0:a6a8578e95af | 9 | void clearSerialStream() |
Lugs | 0:a6a8578e95af | 10 | { |
Lugs | 0:a6a8578e95af | 11 | char c; |
Lugs | 0:a6a8578e95af | 12 | while(daplink.readable()) { |
Lugs | 0:a6a8578e95af | 13 | c = daplink.getc(); |
Lugs | 0:a6a8578e95af | 14 | wait_ms(1); |
Lugs | 0:a6a8578e95af | 15 | } |
Lugs | 0:a6a8578e95af | 16 | } |
Lugs | 0:a6a8578e95af | 17 | |
Lugs | 0:a6a8578e95af | 18 | bool getInput(int maxSize,char *inputArray) |
Lugs | 0:a6a8578e95af | 19 | { |
Lugs | 0:a6a8578e95af | 20 | int i; |
Lugs | 0:a6a8578e95af | 21 | char c; |
Lugs | 0:a6a8578e95af | 22 | clearSerialStream(); |
Lugs | 0:a6a8578e95af | 23 | for(i=0; i<maxSize && c!='\r'; i++) { |
Lugs | 0:a6a8578e95af | 24 | c = daplink.getc(); |
Lugs | 0:a6a8578e95af | 25 | daplink.putc(c); |
Lugs | 0:a6a8578e95af | 26 | inputArray[i] = c; |
Lugs | 0:a6a8578e95af | 27 | } |
Lugs | 0:a6a8578e95af | 28 | if(i == maxSize) { |
Lugs | 0:a6a8578e95af | 29 | return 0; |
Lugs | 0:a6a8578e95af | 30 | } else { |
Lugs | 0:a6a8578e95af | 31 | return 1; |
Lugs | 0:a6a8578e95af | 32 | } |
Lugs | 0:a6a8578e95af | 33 | } |
Lugs | 0:a6a8578e95af | 34 | |
Lugs | 0:a6a8578e95af | 35 | void startFileSystem() |
Lugs | 0:a6a8578e95af | 36 | { |
Lugs | 0:a6a8578e95af | 37 | printf("\f---STARTING FILESYSTEM...---\r\n"); |
Lugs | 0:a6a8578e95af | 38 | |
Lugs | 0:a6a8578e95af | 39 | Thread::wait(100); |
Lugs | 0:a6a8578e95af | 40 | |
Lugs | 0:a6a8578e95af | 41 | // Try to mount the filesystem |
Lugs | 0:a6a8578e95af | 42 | printf("Mounting the filesystem... "); |
Lugs | 0:a6a8578e95af | 43 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 44 | int err = fs.mount(&bd); |
Lugs | 0:a6a8578e95af | 45 | printf("%s\r\n", (err ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 46 | if (err) { |
Lugs | 0:a6a8578e95af | 47 | // Reformat if we can't mount the filesystem |
Lugs | 0:a6a8578e95af | 48 | // this should only happen on the first boot |
Lugs | 0:a6a8578e95af | 49 | printf("No filesystem found, formatting... "); |
Lugs | 0:a6a8578e95af | 50 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 51 | err = fs.reformat(&bd); |
Lugs | 0:a6a8578e95af | 52 | printf("%s\r\n", (err ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 53 | } |
Lugs | 0:a6a8578e95af | 54 | |
Lugs | 0:a6a8578e95af | 55 | // Open the numbers file |
Lugs | 0:a6a8578e95af | 56 | printf("Opening \"/fs/numbers.txt\"... "); |
Lugs | 0:a6a8578e95af | 57 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 58 | FILE *f = fopen("/fs/numbers.txt", "r+"); |
Lugs | 0:a6a8578e95af | 59 | printf("%s\r\n", (!f ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 60 | if (!f) { |
Lugs | 0:a6a8578e95af | 61 | // Create the numbers file if it doesn't exist |
Lugs | 0:a6a8578e95af | 62 | printf("No file found, creating a new file... "); |
Lugs | 0:a6a8578e95af | 63 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 64 | f = fopen("/fs/numbers.txt", "w+"); |
Lugs | 0:a6a8578e95af | 65 | printf("%s\r\n", (!f ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 66 | |
Lugs | 0:a6a8578e95af | 67 | for (int i = 0; i < 10; i++) { |
Lugs | 0:a6a8578e95af | 68 | printf("\rWriting numbers (%d/%d)... ", i, 10); |
Lugs | 0:a6a8578e95af | 69 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 70 | err = fprintf(f, " %d\r\n", i); |
Lugs | 0:a6a8578e95af | 71 | if (err < 0) { |
Lugs | 0:a6a8578e95af | 72 | printf("Fail :(\r\n"); |
Lugs | 0:a6a8578e95af | 73 | } |
Lugs | 0:a6a8578e95af | 74 | } |
Lugs | 0:a6a8578e95af | 75 | printf("\rWriting numbers (%d/%d)... OK\r\n", 10, 10); |
Lugs | 0:a6a8578e95af | 76 | |
Lugs | 0:a6a8578e95af | 77 | printf("Seeking file... "); |
Lugs | 0:a6a8578e95af | 78 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 79 | err = fseek(f, 0, SEEK_SET); |
Lugs | 0:a6a8578e95af | 80 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 81 | } |
Lugs | 0:a6a8578e95af | 82 | |
Lugs | 0:a6a8578e95af | 83 | // Go through and increment the numbers |
Lugs | 0:a6a8578e95af | 84 | for (int i = 0; i < 10; i++) { |
Lugs | 0:a6a8578e95af | 85 | printf("\rIncrementing numbers (%d/%d)... ", i, 10); |
Lugs | 0:a6a8578e95af | 86 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 87 | |
Lugs | 0:a6a8578e95af | 88 | // Get current stream position |
Lugs | 0:a6a8578e95af | 89 | long pos = ftell(f); |
Lugs | 0:a6a8578e95af | 90 | |
Lugs | 0:a6a8578e95af | 91 | // Parse out the number and increment |
Lugs | 0:a6a8578e95af | 92 | int32_t number; |
Lugs | 0:a6a8578e95af | 93 | fscanf(f, "%d", &number); |
Lugs | 0:a6a8578e95af | 94 | number += 1; |
Lugs | 0:a6a8578e95af | 95 | |
Lugs | 0:a6a8578e95af | 96 | // Seek to beginning of number |
Lugs | 0:a6a8578e95af | 97 | fseek(f, pos, SEEK_SET); |
Lugs | 0:a6a8578e95af | 98 | |
Lugs | 0:a6a8578e95af | 99 | // Store number |
Lugs | 0:a6a8578e95af | 100 | fprintf(f, " %d\r\n", number); |
Lugs | 0:a6a8578e95af | 101 | } |
Lugs | 0:a6a8578e95af | 102 | printf("\rIncrementing numbers (%d/%d)... OK\r\n", 10, 10); |
Lugs | 0:a6a8578e95af | 103 | |
Lugs | 0:a6a8578e95af | 104 | // Close the file which also flushes any cached writes |
Lugs | 0:a6a8578e95af | 105 | printf("Closing \"/fs/numbers.txt\"... "); |
Lugs | 0:a6a8578e95af | 106 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 107 | err = fclose(f); |
Lugs | 0:a6a8578e95af | 108 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 109 | |
Lugs | 0:a6a8578e95af | 110 | // Display the root directory |
Lugs | 0:a6a8578e95af | 111 | printf("Opening the root directory... "); |
Lugs | 0:a6a8578e95af | 112 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 113 | DIR *d = opendir("/fs/"); |
Lugs | 0:a6a8578e95af | 114 | printf("%s\r\n", (!d ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 115 | |
Lugs | 0:a6a8578e95af | 116 | printf("root directory:\r\n"); |
Lugs | 0:a6a8578e95af | 117 | while (true) { |
Lugs | 0:a6a8578e95af | 118 | struct dirent *e = readdir(d); |
Lugs | 0:a6a8578e95af | 119 | if (!e) { |
Lugs | 0:a6a8578e95af | 120 | break; |
Lugs | 0:a6a8578e95af | 121 | } |
Lugs | 0:a6a8578e95af | 122 | printf(" %s\r\n", e->d_name); |
Lugs | 0:a6a8578e95af | 123 | } |
Lugs | 0:a6a8578e95af | 124 | |
Lugs | 0:a6a8578e95af | 125 | printf("Closing the root directory... "); |
Lugs | 0:a6a8578e95af | 126 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 127 | err = closedir(d); |
Lugs | 0:a6a8578e95af | 128 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 129 | |
Lugs | 0:a6a8578e95af | 130 | // Display the numbers file |
Lugs | 0:a6a8578e95af | 131 | printf("Opening \"/fs/numbers.txt\"... "); |
Lugs | 0:a6a8578e95af | 132 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 133 | f = fopen("/fs/numbers.txt", "r"); |
Lugs | 0:a6a8578e95af | 134 | printf("%s\r\n", (!f ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 135 | |
Lugs | 0:a6a8578e95af | 136 | printf("numbers:\r\n"); |
Lugs | 0:a6a8578e95af | 137 | while (!feof(f)) { |
Lugs | 0:a6a8578e95af | 138 | int c = fgetc(f); |
Lugs | 0:a6a8578e95af | 139 | printf("%c", c); |
Lugs | 0:a6a8578e95af | 140 | } |
Lugs | 0:a6a8578e95af | 141 | |
Lugs | 0:a6a8578e95af | 142 | printf("\rClosing \"/fs/numbers.txt\"... "); |
Lugs | 0:a6a8578e95af | 143 | fflush(stdout); |
Lugs | 0:a6a8578e95af | 144 | err = fclose(f); |
Lugs | 0:a6a8578e95af | 145 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 146 | |
Lugs | 0:a6a8578e95af | 147 | |
Lugs | 0:a6a8578e95af | 148 | |
Lugs | 0:a6a8578e95af | 149 | |
Lugs | 0:a6a8578e95af | 150 | // Switch to MSD |
Lugs | 0:a6a8578e95af | 151 | // printf("Unmounting... "); |
Lugs | 0:a6a8578e95af | 152 | // fflush(stdout); |
Lugs | 0:a6a8578e95af | 153 | // err = fs.unmount(); |
Lugs | 0:a6a8578e95af | 154 | // printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 155 | |
Lugs | 0:a6a8578e95af | 156 | |
Lugs | 0:a6a8578e95af | 157 | printf("Starting MSD... "); |
Lugs | 0:a6a8578e95af | 158 | msd.disk_initialize(); |
Lugs | 0:a6a8578e95af | 159 | err = msd.connect(); |
Lugs | 0:a6a8578e95af | 160 | printf("%s\r\n", (err < 0 ? "Fail :(" : "OK")); |
Lugs | 0:a6a8578e95af | 161 | } |