This project is a DIY mbed version of your favorite childhood game
Dependencies: mbed 4DGL-uLCD-SE LCD_fonts SDFileSystem MMA8452
SaveFile.cpp@0:bce994168592, 2019-04-14 (annotated)
- Committer:
- aklaussen
- Date:
- Sun Apr 14 01:39:35 2019 +0000
- Revision:
- 0:bce994168592
publish working code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aklaussen | 0:bce994168592 | 1 | #include <vector> |
aklaussen | 0:bce994168592 | 2 | #include <string> |
aklaussen | 0:bce994168592 | 3 | |
aklaussen | 0:bce994168592 | 4 | |
aklaussen | 0:bce994168592 | 5 | #include "mbed.h" |
aklaussen | 0:bce994168592 | 6 | #include "uLCD_4DGL.h" |
aklaussen | 0:bce994168592 | 7 | #include "SDFileSystem.h" |
aklaussen | 0:bce994168592 | 8 | |
aklaussen | 0:bce994168592 | 9 | #include "SaveFile.h" |
aklaussen | 0:bce994168592 | 10 | |
aklaussen | 0:bce994168592 | 11 | extern uLCD_4DGL uLCD; |
aklaussen | 0:bce994168592 | 12 | extern SDFileSystem sd; |
aklaussen | 0:bce994168592 | 13 | |
aklaussen | 0:bce994168592 | 14 | int save_image() { |
aklaussen | 0:bce994168592 | 15 | char fn[100]; |
aklaussen | 0:bce994168592 | 16 | sprintf(fn, "/sd/%d", get_highest_number(read_file_names())); |
aklaussen | 0:bce994168592 | 17 | FILE *fp = fopen(fn, "wb"); |
aklaussen | 0:bce994168592 | 18 | if (fp == NULL) { |
aklaussen | 0:bce994168592 | 19 | printf("Couldn't open file"); |
aklaussen | 0:bce994168592 | 20 | return -1; |
aklaussen | 0:bce994168592 | 21 | } |
aklaussen | 0:bce994168592 | 22 | printf("Grabbing Pixel Values!"); |
aklaussen | 0:bce994168592 | 23 | int pix_buf[128]; |
aklaussen | 0:bce994168592 | 24 | for (int i = 0; i < 128; i++) { |
aklaussen | 0:bce994168592 | 25 | for (int j = 0; j < 128; j++) { |
aklaussen | 0:bce994168592 | 26 | pix_buf[j] = color_convert(uLCD.read_pixel(i,j)); |
aklaussen | 0:bce994168592 | 27 | //printf("Pixel Color is 0x%08x\r\n", color_convert(pix_buf[j])); |
aklaussen | 0:bce994168592 | 28 | } |
aklaussen | 0:bce994168592 | 29 | //printf("Writing line %d", i); |
aklaussen | 0:bce994168592 | 30 | fwrite(pix_buf, sizeof(int), 128, fp); |
aklaussen | 0:bce994168592 | 31 | } |
aklaussen | 0:bce994168592 | 32 | fclose(fp); |
aklaussen | 0:bce994168592 | 33 | return 0; |
aklaussen | 0:bce994168592 | 34 | } |
aklaussen | 0:bce994168592 | 35 | |
aklaussen | 0:bce994168592 | 36 | int read_image(const char* fn) { |
aklaussen | 0:bce994168592 | 37 | FILE * fp = fopen(fn, "rb"); |
aklaussen | 0:bce994168592 | 38 | if (fp == NULL) |
aklaussen | 0:bce994168592 | 39 | return -1; |
aklaussen | 0:bce994168592 | 40 | int pix = 0; |
aklaussen | 0:bce994168592 | 41 | for (int i = 0; i < 128; i++) { |
aklaussen | 0:bce994168592 | 42 | for (int j = 0; j < 128; j++) { |
aklaussen | 0:bce994168592 | 43 | fread(&pix, sizeof(int), 1, fp); |
aklaussen | 0:bce994168592 | 44 | uLCD.pixel(i, j, pix); |
aklaussen | 0:bce994168592 | 45 | } |
aklaussen | 0:bce994168592 | 46 | } |
aklaussen | 0:bce994168592 | 47 | |
aklaussen | 0:bce994168592 | 48 | return 0; |
aklaussen | 0:bce994168592 | 49 | } |
aklaussen | 0:bce994168592 | 50 | |
aklaussen | 0:bce994168592 | 51 | int color_convert(int pixel) { |
aklaussen | 0:bce994168592 | 52 | int red_mask = 0xF800; |
aklaussen | 0:bce994168592 | 53 | int green_mask = 0x7E0; |
aklaussen | 0:bce994168592 | 54 | int blue_mask = 0x1F; |
aklaussen | 0:bce994168592 | 55 | int red_value = (pixel & red_mask) >> 11; |
aklaussen | 0:bce994168592 | 56 | int green_value = (pixel & green_mask) >> 5; |
aklaussen | 0:bce994168592 | 57 | int blue_value = (pixel & blue_mask); |
aklaussen | 0:bce994168592 | 58 | // Expand to 8-bit values. |
aklaussen | 0:bce994168592 | 59 | int red = red_value << 3; |
aklaussen | 0:bce994168592 | 60 | int green = green_value << 2; |
aklaussen | 0:bce994168592 | 61 | int blue = blue_value << 3; |
aklaussen | 0:bce994168592 | 62 | int color = (red << 16) | (green << 8) | (blue); |
aklaussen | 0:bce994168592 | 63 | return color; |
aklaussen | 0:bce994168592 | 64 | } |
aklaussen | 0:bce994168592 | 65 | std::vector<std::string> read_file_names() |
aklaussen | 0:bce994168592 | 66 | { |
aklaussen | 0:bce994168592 | 67 | char* dir = "/sd/"; |
aklaussen | 0:bce994168592 | 68 | DIR *dp; |
aklaussen | 0:bce994168592 | 69 | struct dirent *dirp; |
aklaussen | 0:bce994168592 | 70 | dp = opendir(dir); |
aklaussen | 0:bce994168592 | 71 | //read all directory and file names in current directory into filename vector |
aklaussen | 0:bce994168592 | 72 | std::vector<std::string> filenames; |
aklaussen | 0:bce994168592 | 73 | while((dirp = readdir(dp)) != NULL) { |
aklaussen | 0:bce994168592 | 74 | filenames.push_back(string(dirp->d_name)); |
aklaussen | 0:bce994168592 | 75 | } |
aklaussen | 0:bce994168592 | 76 | closedir(dp); |
aklaussen | 0:bce994168592 | 77 | return filenames; |
aklaussen | 0:bce994168592 | 78 | } |
aklaussen | 0:bce994168592 | 79 | int get_highest_number (std::vector<std::string> filenames) { |
aklaussen | 0:bce994168592 | 80 | return filenames.size(); |
aklaussen | 0:bce994168592 | 81 | } |