-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
162 lines (114 loc) · 3.44 KB
/
main.cpp
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
/*GR-SAKURA Sketch Template Version: V1.08*/
#include <rxduino.h>
#include <sdmmc.h>
#include "JPEGCamera.h"
// CS Pin of SD card
#define SDCS 22
SDMMC mySD;
////////////// JPEGCamera ///////////////
#define READ_BUFFER_SIZE 64
char filename[12] = "pic0.jpg";
//JPEGCameraインスタンスを作成
JPEGCamera camera;
//受信バッファ
unsigned char response[READ_BUFFER_SIZE];
//受信データサイズ
unsigned int count=0;
//撮影したJPGE画像サイズ
uint16_t size=0;
//次に読み込むJPEG画像のアドレス
unsigned int address=0;
//void dateTime(uint16_t* date, uint16_t* time);
void changeResolution(JPEGCamera::ImageSize size){
Serial.println("change image size");
if( camera.imageSize2( size ) == false ){
Serial.println("change image size failed");
while(1);
}
}
void takepicture( void ){
Serial.println("taking picture...");
if( camera.takePicture() == false ){
Serial.println("fail to take picture");
while(1);
}
Serial.println("ok.");
Serial.println("taking size...");
if( camera.getSize(&size) == false ){
Serial.println("getSize failed");
while(1);
}
Serial.print(size);
Serial.println();
unsigned int count = 0;
while(mySD.exists(filename)){
count++;
sprintf(filename, "pic%d.jpg", count);
}
File dataFile = mySD.open(filename, FILE_WRITE);
Serial.print("filename is ");
Serial.println(filename);
//開始アドレスを0に、size分読み込むまで繰り返す
address = 0;
while(address < size)
{
//READ_BUFFER_SIZE byte読み込む
count = camera.readData(response, READ_BUFFER_SIZE, address);
if( count ){
//SDに書き込む
for (int i =0 ; i< READ_BUFFER_SIZE;i+=2){
dataFile.write(response[i]);
dataFile.write(response[i+1]);
if((response[i] == 0xFF) && (response[i+1] == 0xD9)){//JPGの終わりを検出
break;
}
}
//受信したデータサイズ分、アドレスを更新する
address += count;
} else {
Serial.println("readData failed");
Serial.flush();
while(1);
}
delay(2); //これがないとread failする
}
//撮影停止(一旦止めないと新しい画像は撮影されない)
camera.stopPictures();
dataFile.close();
Serial.println("Success to save picture");
}
void setup( void )
{
pinMode(PIN_LED0, OUTPUT);
pinMode(PIN_LED1, OUTPUT);
pinMode(PIN_LED2, OUTPUT);
pinMode(PIN_LED3, OUTPUT);
digitalWrite(PIN_LED0, 0);
digitalWrite(PIN_LED1, 0);
digitalWrite(PIN_LED2, 0);
digitalWrite(PIN_LED3, 0);
// PC用
Serial.begin(115200,SCI_USB0);
while(!Serial.available()); // 入力を待つ
Serial.read();
// Camera
Serial2.begin(38400, SCI_SCI0P2x); // to JPEG camera
Serial.println("Camera Reset");
if( camera.reset() == false ){
Serial.println("reset failed");
while(1);
}
Serial.println("Initializing SD card...");
mySD.begin(SDCS);
Serial.println("OK");
//少しまってから撮影を開始する
delay(2000);
// 解像度の変更
changeResolution(JPEGCamera::IMG_SZ_160x120);
delay(1000);
}
void loop( void )
{
takepicture();
delay(3000); // 少し待つ
}