-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPovled.ino
More file actions
174 lines (145 loc) · 3.99 KB
/
Copy pathPovled.ino
File metadata and controls
174 lines (145 loc) · 3.99 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <SPI.h>
#include <FastLED.h>
#include <SD.h>
FASTLED_USING_NAMESPACE
#define DATA_PIN 7
#define CLK_PIN 6
#define LED_TYPE APA102
#define COLOR_ORDER BGR
//#define NUM_LEDS 144
#define NUM_LEDS 64
#define SD_SELECT 4
CRGB image[NUM_LEDS][NUM_LEDS];
CRGB leds[NUM_LEDS];
File imageFile;
unsigned long image_start_address = 0;
unsigned long image_end_address = 0;
unsigned long image_ptr = 0;
int image_row = 0;
int frame_counter = 0;
unsigned long last_time = 0;
// Code stolen from: https://arduino.stackexchange.com/questions/19795/how-to-read-bitmap-image-on-arduino
struct bmp_file_header_t {
uint16_t signature;
uint32_t file_size;
uint16_t reserved[2];
uint32_t image_offset;
};
struct bmp_image_header_t {
uint32_t header_size;
uint32_t image_width;
uint32_t image_height;
uint16_t color_planes;
uint16_t bits_per_pixel;
uint32_t compression_method;
uint32_t image_size;
uint32_t horizontal_resolution;
uint32_t vertical_resolution;
uint32_t colors_in_palette;
uint32_t important_colors;
};
bmp_file_header_t fileHeader;
bmp_image_header_t imageHeader;
#define BRIGHTNESS 255
#define FRAMES_PER_SECOND 120
void setup() {
// Setup serial
Serial.begin(9600);
// Setup LEDs
delay(100); // 3 second delay for recovery
FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
// Setup SD
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("SD init failed");
return;
}
Serial.println("SD Init done.");
loadImage();
}
void loadImage(){
// char filename[] = "0002.bmp";
char filename[] = "tree.bmp";
if (!SD.exists(filename))
{
Serial.println("File does not exist");
}
else
{
Serial.println("File exists");
}
imageFile = SD.open(filename, FILE_READ);
// Read the file header
imageFile.read(&fileHeader, sizeof(fileHeader));
imageFile.read(&imageHeader, sizeof(imageHeader));
inspectImage();
image_start_address = fileHeader.image_offset;
image_ptr = image_start_address;
image_end_address = image_start_address + imageHeader.image_size;
// Load image into memory
imageFile.seek(image_start_address);
for (int i = 0; i < NUM_LEDS; i++)
{
for (int j = 0; j < NUM_LEDS; j++)
{
byte r, g, b;
r = imageFile.read();
b = imageFile.read();
g = imageFile.read();
// image[i][j] = (b > 0 || g > 0 || r > 0);
image[j][i].red = r;
image[j][i].green = g;
image[j][i].blue = b;
}
}
}
void inspectImage(){
Serial.print("Signature: ");
Serial.println(fileHeader.signature);
Serial.print("File Size: ");
Serial.println(fileHeader.file_size);
Serial.print("Image Offset: ");
Serial.println(fileHeader.image_offset);
Serial.println("- - - - - - - - - - - -");
Serial.print("Image Width: ");
Serial.println(imageHeader.image_width);
Serial.print("Image Height: ");
Serial.println(imageHeader.image_height);
Serial.print("Color Planes: ");
Serial.println(imageHeader.color_planes);
Serial.print("Bits Per Pixel: ");
Serial.println(imageHeader.bits_per_pixel);
Serial.print("image_size: ");
Serial.println(imageHeader.image_size);
Serial.print("Horizontal Resolution: ");
Serial.println(imageHeader.horizontal_resolution);
Serial.print("Vertical Resolution: ");
Serial.println(imageHeader.vertical_resolution);
}
void loop()
{
nextRow();
}
void nextRow()
{
image_row++;
if (image_row >= NUM_LEDS)
{
image_row = 0;
frame_counter++;
// #define FRAME_AGG 100
// if (frame_counter == FRAME_AGG)
// {
// unsigned long new_time = micros();
// long double fps = ((long double)FRAME_AGG / ((long double)new_time - (long double)last_time)) * 1000 * 1000;
// Serial.print((int)fps);
// Serial.println();
// frame_counter = 0;
// delay(5);
// last_time = micros();
// }
}
// Copy image to leds array
memmove( &leds[0], &image[image_row][0], NUM_LEDS * sizeof(CRGB));
FastLED.show();
}