Skip to content

Commit cddb73e

Browse files
authored
Merge pull request #27 from arduino-libraries/marqdevx/vision-shield/bitmap-SD
Add Vision Shield bitmap to SD sketch
2 parents 2e56c4b + 2972ebe commit cddb73e

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

Diff for: .github/workflows/compile-examples.yml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
- examples/Portenta H7 as a USB Host/LEDKeyboardController
4646
- examples/Portenta H7 as a WiFi Access Point/SimpleWebServer
4747
- examples/Setting Up Portenta H7 For Arduino/Blink
48+
- examples/Vision Shield to SD Card bmp/visionShieldBitmap
4849
4950
- fqbn: arduino:mbed_portenta:envie_m4
5051
sketch-paths: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "SDMMCBlockDevice.h" // Multi Media Card APIs
2+
#include "FATFileSystem.h" // Mbed API for portable and embedded systems
3+
SDMMCBlockDevice blockDevice;
4+
mbed::FATFileSystem fileSystem("fs");
5+
6+
#include "camera.h" // Arduino Mbed Core Camera APIs
7+
#include "himax.h" // Exclusive Camera library for the Portenta Vision Shield
8+
HM01B0 himax;
9+
Camera cam(himax);
10+
11+
FrameBuffer fb; // Buffer to save the capture
12+
13+
// Settings for our setup
14+
#define RES_H (unsigned int)240
15+
#define RES_W (unsigned int)320
16+
#define IMAGE_MODE CAMERA_GRAYSCALE
17+
#define IMAGE_BPP (unsigned int)8
18+
// Headers info
19+
#define HEADER_FILE_HEADER (unsigned int)14
20+
#define HEADER_DIB_SIZE (unsigned int)40
21+
#define HEADER_FULL_SIZE (HEADER_FILE_HEADER + HEADER_DIB_SIZE)
22+
#define PALETTE_SIZE (2 ^ IMAGE_BPP) * 4 // 4 bytes per color
23+
24+
void setup()
25+
{
26+
Serial.begin(115200);
27+
while (!Serial)
28+
;
29+
30+
// Mount SD Card
31+
mountSD();
32+
33+
// Init the cam QVGA, 30FPS, Grayscale
34+
if (!cam.begin(CAMERA_R320x240, IMAGE_MODE, 30))
35+
{
36+
Serial.println("Unable to find the camera");
37+
}
38+
39+
// Save the headers and the image data into the .bmp file
40+
parseData();
41+
}
42+
43+
void loop()
44+
{
45+
while (1)
46+
;
47+
}
48+
49+
// Mount File system block
50+
void mountSD()
51+
{
52+
Serial.println("Mounting SD Card...");
53+
54+
int error = fileSystem.mount(&blockDevice);
55+
if (error)
56+
{
57+
Serial.println("No SD Card found");
58+
while (1)
59+
;
60+
}
61+
}
62+
63+
void parseData()
64+
{
65+
unsigned char *imgData = NULL;
66+
int fileSize = HEADER_FILE_HEADER + RES_W * RES_H;
67+
68+
FILE *file = fopen("/fs/image.bmp", "w+");
69+
70+
// Get a Frame
71+
if (cam.grabFrame(fb, 3000) == 0)
72+
{
73+
// Save the raw image data (8bpp grayscale)
74+
imgData = fb.getBuffer();
75+
}
76+
else
77+
{
78+
Serial.println("could not grab the frame");
79+
while (1)
80+
;
81+
}
82+
// Bitmap structure (Head + DIB Head + ColorMap + binary image)
83+
unsigned char bitmapFileHeader[HEADER_FILE_HEADER];
84+
unsigned char bitmapDIBHeader[HEADER_DIB_SIZE];
85+
unsigned char colorMap[PALETTE_SIZE]; // Needed for <=8bpp grayscale bitmaps
86+
87+
// Set the file headers to 0
88+
memset(bitmapFileHeader, (unsigned char)(0), HEADER_FILE_HEADER);
89+
memset(bitmapDIBHeader, (unsigned char)(0), HEADER_DIB_SIZE);
90+
memset(colorMap, (unsigned char)(0), PALETTE_SIZE);
91+
92+
// Write the headers info
93+
// File header
94+
bitmapFileHeader[0] = 'B';
95+
bitmapFileHeader[1] = 'M';
96+
bitmapFileHeader[2] = (unsigned char)(fileSize);
97+
bitmapFileHeader[3] = (unsigned char)(fileSize >> 8);
98+
bitmapFileHeader[4] = (unsigned char)(fileSize >> 16);
99+
bitmapFileHeader[5] = (unsigned char)(fileSize >> 24);
100+
bitmapFileHeader[10] = (unsigned char)HEADER_FULL_SIZE + PALETTE_SIZE;
101+
102+
// Info header
103+
bitmapDIBHeader[0] = (unsigned char)(HEADER_DIB_SIZE);
104+
bitmapDIBHeader[4] = (unsigned char)(RES_W);
105+
bitmapDIBHeader[5] = (unsigned char)(RES_W >> 8);
106+
bitmapDIBHeader[8] = (unsigned char)(RES_H);
107+
bitmapDIBHeader[8] = (unsigned char)(RES_H >> 8);
108+
bitmapDIBHeader[14] = (unsigned char)(IMAGE_BPP);
109+
110+
// Color palette for grayscale Bitmaps (8bpp)
111+
for (int i = 0; i < (2 ^ IMAGE_BPP); i++)
112+
{
113+
colorMap[i * 4] = i;
114+
colorMap[i * 4 + 1] = i;
115+
colorMap[i * 4 + 2] = i;
116+
}
117+
118+
// Write theh bitmap file
119+
fwrite(bitmapFileHeader, 1, HEADER_FILE_HEADER, file);
120+
fwrite(bitmapDIBHeader, 1, HEADER_DIB_SIZE, file);
121+
fwrite(colorMap, 1, PALETTE_SIZE, file); // Color map
122+
fwrite(imgData, 1, RES_H * RES_W, file);
123+
124+
// Close the stream (bitmap file)
125+
fclose(file);
126+
}

0 commit comments

Comments
 (0)