/
main.c
142 lines (122 loc) · 3.76 KB
/
main.c
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
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <cfaf128128b-0145t.h>
#include <unistd.h>
#pragma pack(push, 1)
typedef struct tagBITMAPFILEHEADER
{
short bfType; //specifies the file type
int bfSize; //specifies the size in bytes of the bitmap file
short bfReserved1; //reserved; must be 0
short bfReserved2; //reserved; must be 0
int bOffBits; //specifies the offset in bytes from the bitmapfileheader to the bitmap bits
}BITMAPFILEHEADER;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct tagBITMAPINFOHEADER
{
int biSize; //specifies the number of bytes required by the struct
int biWidth; //specifies width in pixels
int biHeight; //species height in pixels
short biPlanes; //specifies the number of color planes, must be 1
short biBitCount; //specifies the number of bit per pixel
int biCompression; //specifies the type of compression
int biSizeImage; //size of image in bytes
int biXPelsPerMeter; //number of pixels per meter in x axis
int biYPelsPerMeter; //number of pixels per meter in y axis
int biClrUsed; //number of colors used by the bitmap
int biClrImportant; //number of colors that are important
}BITMAPINFOHEADER;
#pragma pack(pop)
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr; //our file pointer
BITMAPFILEHEADER bitmapFileHeader; //our bitmap file header
unsigned char *bitmapImage; //store image data
int imageIdx=0; //image index counter
//open filename in read binary mode
filePtr = fopen(filename,"rb");
if (filePtr == NULL) {
printf("Couldn't open file\n");
return NULL;
}
//read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER),1,filePtr);
//verify that this is a bmp file by check bitmap id
if (bitmapFileHeader.bfType !=0x4D42)
{
printf("Expected 0x4d42, found: %x\n", bitmapFileHeader.bfType);
printf("File is not a bmp\n");
fclose(filePtr);
return NULL;
}
//read the bitmap info header
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,filePtr);
//move file point to the begging of bitmap data
fseek(filePtr, bitmapFileHeader.bOffBits, SEEK_SET);
//allocate enough memory for the bitmap image data
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
//verify memory allocation
if (!bitmapImage)
{
printf("Error 2\n");
free(bitmapImage);
fclose(filePtr);
return NULL;
}
//read in the bitmap image data
fread(bitmapImage,bitmapInfoHeader->biSizeImage,1,filePtr);
//make sure bitmap image data was read
if (bitmapImage == NULL)
{
printf("Error 3\n");
fclose(filePtr);
return NULL;
}
for(imageIdx=0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3){
tft_writeColor(bitmapImage[imageIdx+2], bitmapImage[imageIdx+1], bitmapImage[imageIdx]);
}
//close file and return bitmap iamge data
fclose(filePtr);
return bitmapImage;
}
static void pabort(const char *s){
perror(s);
abort();
}
int main(int argc, char *argv[])
{
BITMAPINFOHEADER bitmapInfoHeader;
//unsigned char *bitmapData;
int ret=0;
ret = init_tft(1);
if(ret==-1)
{
printf("init error: %i\n", errno);
pabort("Unable to initialize the display");
}
while(1)
{
setOrientation(0);
// Write to GRAM
SPI3W_sendCommand(0x2C);
for(int i=0; i<128; i++) {
for(int j=0; j<128; j++) {
tft_writeColor(255, 0, 0);
}
}
for(int x=0;x<4;x++)
{
setOrientation(x);
SPI3W_sendCommand(0x2C);
LoadBitmapFile("CFAF128128B-0145T.bmp",&bitmapInfoHeader);
}
}
return ret;
}