discoverpixy
screen_filetest.c
Go to the documentation of this file.
1 /**************************************************************************************************************************************
2 * Project: discoverpixy
3 * Website: https://github.com/t-moe/discoverpixy
4 * Authors: Aaron Schmocker, Timo Lang
5 * Institution: BFH Bern University of Applied Sciences
6 * File: common/app/screen_filetest.c
7 *
8 * Version History:
9 * Date Autor Email SHA Changes
10 * 2015-05-10 timolang@gmail.com e2bce8f Added filesystem module, tests and implementation for it in emulator.
11 * 2015-05-10 timolang@gmail.com 790f602 Added bitmap decoding/drawing example
12 * 2015-05-10 timolang@gmail.com 21edc56 Added doxyfile (doxygen) for the common folder. Started with doxygen comments for app and tft module.
13 * 2015-05-15 timolang@gmail.com 9a16865 Added doxgen comments to filesyste, checkbox, numupdown and screen module. And some minor changes to the other modules.
14 * 2015-05-15 timolang@gmail.com 85f1aee Changed filetest to use new bitmap draw method.
15 *
16 **************************************************************************************************************************************/
17 
18 #include "screen_filetest.h"
19 #include "button.h"
20 #include "tft.h"
21 #include "filesystem.h"
22 #include <stdlib.h>
23 
24 
26 
27 static void b_back_cb(void* button)
28 {
30 }
31 
32 
33 static void image_test();
34 
35 static void enter(void* screen)
36 {
37  tft_clear(HEX(0xBABECD));
38 
39  //Back button
40  b_back.base.x1 = 10; //Start X of Button
41  b_back.base.y1 = 200; //Start Y of Button
42  b_back.base.x2 = AUTO; //b_back.base.x1+160; //Auto Calculate X2 with String Width
43  b_back.base.y2 = AUTO; //Auto Calculate Y2 with String Height
44  b_back.txtcolor = WHITE; //Set foreground color
45  b_back.bgcolor = HEX(0xAE1010); //Set background color (Don't take 255 or 0 on at least one channel, to make shadows possible)
46  b_back.font = 0; //Select Font
47  b_back.text = "Back"; //Set Text (For formatted strings take sprintf)
48  b_back.callback = b_back_cb; //Call b_back_cb as Callback
49  gui_button_add(&b_back); //Register Button (and run the callback from now on)
50 
51  tft_draw_line(10, 30, 310, 30, BLACK);
52  tft_print_line(10, 18, BLUE, TRANSPARENT, 0, "Name D H RO Date Time Size");
53 
54  int y = 33;
55 
57 
58  if (dir == NULL) {
59  return;
60  }
61 
62  for (int i = 0; i < dir->num_files; i++) {
63  FILE_STRUCT* file = &(dir->files[i]);
64  tft_print_formatted(10, y,
65  (file->fattrib & F_DIR) ? GREEN : RED,
66  TRANSPARENT, 0, "%-13s%c %c %s %02u%02u%02u %02u:%02u:%02u %u",
67  file->fname,
68  (file->fattrib & F_DIR) ? 'D' : ' ',
69  (file->fattrib & F_HID) ? 'H' : ' ',
70  (file->fattrib & F_RDO) ? "R " : "RW",
71  file->fdate.day,
72  file->fdate.month,
73  (file->fdate.year + 1980) % 100,
74  file->ftime.hour,
75  file->ftime.min,
76  file->ftime.sec * 2,
77  file->fsize);
78  y += 14;
79  }
80 
82 
83  y += 14;
84 
85  FILE_HANDLE* file = filesystem_file_open("test.txt");
86 
87  if (file == NULL) {
88  tft_print_line(10, y, BLUE, TRANSPARENT, 0, "Could not open test.txt");
89  } else {
90  char buf [30];
91  int size = (file->fsize > 30) ? 29 : file->fsize - 1;
92  FILE_STATUS st = filesystem_file_read(file, buf, size);
93 
94  if (st == F_OK) {
95  buf[file->fpos] = '\0';
96  tft_print_formatted(10, y, BLUE, TRANSPARENT, 0, "test.txt contains \"%s\"", buf);
97  long num = strtol(&(buf[file->fpos - 4]), NULL, 0);
98  num++;
99 
100  y += 14;
101 
102  if (filesystem_file_seek(file, file->fpos - 4) != F_OK) {
103  tft_print_formatted(10, y, BLUE, TRANSPARENT, 0, "Could not seek to %d", file->fpos - 4);
104  } else {
105  sprintf(buf, "%04d", num);
106 
107  if (filesystem_file_write(file, buf, 4) != F_OK) {
108  tft_print_formatted(10, y, BLUE, TRANSPARENT, 0, "Could not write new number %d", num);
109  } else {
110  tft_print_formatted(10, y, BLUE, TRANSPARENT, 0, "New number written %d", num);
111  }
112  }
113  } else {
114  tft_print_line(10, y, BLUE, TRANSPARENT, 0, "Could not read from test.txt");
115  }
116 
117  }
118 
119  filesystem_file_close(file);
120 
121  image_test();
122 }
123 
124 static void leave(void* screen)
125 {
126  gui_button_remove(&b_back);
127 }
128 
129 static void update(void* screen)
130 {
131 }
132 
133 
135  enter,
136  leave,
137  update
138 };
139 
140 
142 {
143  return &screen;
144 }
145 
146 static void image_test()
147 {
148 
149 
150  if (!tft_draw_bitmap_file_unscaled(250, 170, "cpu.bmp")) {
151  tft_print_line(10, 180, BLUE, TRANSPARENT, 0, "Could not open cpu.bmp");
152  }
153 
154  tft_draw_rectangle(250 - 1, 170 - 1, 250 - 1 + 64, 170 - 1 + 64, BLACK);
155 }
const char * text
The label of the button.
Definition: button.h:61
static BUTTON_STRUCT b_back
uint16_t txtcolor
The 16-bit text color.
Definition: button.h:59
#define AUTO
Use this value instead of x2, y2 in the BUTTON_STRUCT to autocalculate the button width/height...
Definition: button.h:65
FILE_STATUS filesystem_file_seek(FILE_HANDLE *handle, uint32_t offset)
Definition: filesystem.c:43
uint32_t fsize
The total file size in bytes.
Definition: filesystem.h:84
void tft_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition: tft.c:50
unsigned sec
second/2 (0..29)
Definition: filesystem.h:55
uint16_t y1
Top Left Y-Coordinate of Area.
Definition: touch.h:75
static void leave(void *screen)
void tft_print_formatted(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, const char *format,...)
Definition: tft.c:111
bool gui_button_add(BUTTON_STRUCT *button)
Definition: button.c:133
void tft_print_line(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, const char *text)
Definition: tft.c:98
FILE_DATE_STRUCT fdate
Last modified date.
Definition: filesystem.h:63
unsigned day
day (1..31)
Definition: filesystem.h:46
uint8_t fattrib
File/Directory Attributes.
Definition: filesystem.h:65
uint16_t x1
Top Left X-Coordinate of Area.
Definition: touch.h:74
#define TRANSPARENT
Definition: tft.h:66
uint16_t bgcolor
The 16-bit background color of the button.
Definition: button.h:57
static void update(void *screen)
TOUCH_AREA_STRUCT base
Basic geometry of the button. You only need to set the x1, y1, x2, y2 members of this struct...
Definition: button.h:56
#define HEX(h)
Definition: tft.h:60
uint16_t y2
Bottom Right Y-Coordinate of Area.
Definition: touch.h:77
FILE_HANDLE * filesystem_file_open(const char *filename)
Definition: filesystem.c:33
void filesystem_file_close(FILE_HANDLE *handle)
Definition: filesystem.c:38
SCREEN_STRUCT * get_screen_filetest()
bool tft_draw_bitmap_file_unscaled(uint16_t x, uint16_t y, const char *filename)
Definition: tft.c:123
char * fname
File/Directory name.
Definition: filesystem.h:66
void tft_clear(uint16_t color)
Definition: tft.c:45
uint16_t x2
Bottom Right X-Coordinate of Area.
Definition: touch.h:76
void gui_button_remove(BUTTON_STRUCT *button)
Definition: button.c:184
unsigned year
year from 1980 (0..127)
Definition: filesystem.h:44
uint16_t num_files
Number of files/directories in this directory.
Definition: filesystem.h:74
unsigned month
month (1..12)
Definition: filesystem.h:45
#define RED
Definition: tft.h:50
FILE_STATUS filesystem_file_write(FILE_HANDLE *handle, uint8_t *buf, uint32_t size)
Definition: filesystem.c:53
static void b_back_cb(void *button)
#define WHITE
Definition: tft.h:53
unsigned min
minute (0..59
Definition: filesystem.h:54
uint32_t fpos
The current byte-position in the file.
Definition: filesystem.h:83
File is hidden.
Definition: filesystem.h:34
unsigned hour
hour (0..23)
Definition: filesystem.h:53
static SCREEN_STRUCT screen
Everything ok.
Definition: filesystem.h:91
FILE_STRUCT * files
An array with num_files FILE_STRUCT entries.
Definition: filesystem.h:75
void tft_draw_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition: tft.c:61
DIRECTORY_STRUCT * filesystem_dir_open(const char *path)
Definition: filesystem.c:23
#define BLUE
Definition: tft.h:52
It's a directory and not a file.
Definition: filesystem.h:36
File is readonly. You cannot write to it.
Definition: filesystem.h:33
BUTTON_CALLBACK callback
Callback which is executed when the button is pressed.
Definition: button.h:58
bool gui_screen_back()
Definition: screen.c:85
uint8_t font
The number of the font to use.
Definition: button.h:60
FILE_STATUS
Definition: filesystem.h:90
static void enter(void *screen)
#define GREEN
Definition: tft.h:51
void filesystem_dir_close(DIRECTORY_STRUCT *dir)
Definition: filesystem.c:28
static void image_test()
FILE_TIME_STRUCT ftime
Last modified time.
Definition: filesystem.h:64
FILE_STATUS filesystem_file_read(FILE_HANDLE *handle, uint8_t *buf, uint32_t size)
Definition: filesystem.c:48
#define BLACK
Definition: tft.h:54
uint32_t fsize
File size in bytes. 0 for directories.
Definition: filesystem.h:62