Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "camera_app.h"
#include "main.h"
#include "k_bsp.h"
#include "k_rtc.h"
#include "k_storage.h"
External variables
Private macros
#define BMP_PIXEL16_TO_R
#define BMP_PIXEL16_TO_G
#define BMP_PIXEL16_TO_B
Private variables
BMPHeader_QQVGA24Bit
CameraError
Private function prototypes
CAMERA_Set_ContrastBrightness(uint32_t, uint32_t)
CAMERA_SelectEffect(uint32_t)
CAMERA_Init()
CAMERA_Suspend()
CAMERA_Resume()
CAMERA_Stop()
CAMERA_GetState()
BSP_CAMERA_ErrorCallback()
CAMERA_SaveToFile(uint8_t *)
RGB16toRGB24(uint8_t *, uint8_t *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesMB1063Modules/camera/camera_app.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file camera_app.c * @author MCD Application Team * @brief Camera application functions source file ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "camera_app.h" #include "main.h" #include "k_bsp.h" #include "k_rtc.h" #include "k_storage.h" 5 includes /** @addtogroup CAMERA_MODULE * @{ *//* ... */ /** @defgroup CAMERA_APPLICATION * @brief camera application routines * @{ *//* ... */ Includes /* External variables --------------------------------------------------------*/ extern CameraSettingsTypeDef CameraSettings; External variables /* Private typedef -----------------------------------------------------------*/ /* Private defines -----------------------------------------------------------*/ /* Private macros ------------------------------------------------------------*/ #define BMP_PIXEL16_TO_R(pixel) ((pixel & 0x1F) << 3) #define BMP_PIXEL16_TO_G(pixel) (((pixel >> 5) & 0x3F) << 2) #define BMP_PIXEL16_TO_B(pixel) (((pixel >> 11) & 0x1F) << 3) Private macros /* Private variables ---------------------------------------------------------*/ const uint8_t BMPHeader_QQVGA24Bit[] = { 0x42, 0x4D, /* Offet0: BMP Magic Number */ 0x36, 0x84, 0x03, 0x00, /* Offset2 : filesz : Size of the BMP file 240*320*3 + 54 */ 0x00, 0x00, 0x00, 0x00, /* Offset6, Offset8 : Reserved0, Reserved1 =0 */ 0x36, 0x00, 0x00, 0x00, /* Offset10 : bmp_offset: Offset of bitmap data (pixels) = 54 = 0x36 */ 0x28, 0x00, 0x00, 0x00, /* Offset14 : header_sz : The number of bytes in the header (from this point). */ 0x40, 0x01, 0x00, 0x00, /* Offset18 : width 320 */ 0xF0, 0x00, 0x00, 0x00, /* Offset2 : height 240 */ 0x01, 0x00, /* Offset26 : nplanes */ 0x18, 0x00, /* Offset24: Bits per Pixels */ 0x00, 0x00, 0x00, 0x00, /* Offset30: compress_type = 0 */ 0x00, 0x58, 0x02, 0x00, /* Offset34 : bmp bytes size*/ 0x00, 0x00, 0x00, 0x00, /* Offset38 : X Resolution : Pixel per meters = 0 */ 0x00, 0x00, 0x00, 0x00, /* Offset42 : Y Resolution : Pixel per meters */ 0x00, 0x00, 0x00, 0x00, /* Offset46 : Number of Colours = 0 */ 0x00, 0x00, 0x00, 0x00, /* Offset50 : Important Colours = 0 */ ...}; uint32_t CameraError = CAMERA_ERROR; Private variables /* Private function prototypes -----------------------------------------------*/ static void RGB16toRGB24(uint8_t *pDestBuffer, uint8_t *pSrcBuffer); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Set Contrast and brightness levels. * @param contrast_level: Contrast level * @param brightness_level: Brightness level * @retval None *//* ... */ void CAMERA_Set_ContrastBrightness(uint32_t contrast_level, uint32_t brightness_level) { uint32_t contrast = 0, brightness = 0; switch (contrast_level) { case 1: contrast = CAMERA_CONTRAST_LEVEL0; break;case 1: case 2: contrast = CAMERA_CONTRAST_LEVEL1; break;case 2: case 3: contrast = CAMERA_CONTRAST_LEVEL2; break;case 3: case 4: contrast = CAMERA_CONTRAST_LEVEL3; break;case 4: case 5: contrast = CAMERA_CONTRAST_LEVEL4; break;case 5: }switch (contrast_level) { ... } switch (brightness_level) { case 1: brightness = CAMERA_BRIGHTNESS_LEVEL0; break;case 1: case 2: brightness = CAMERA_BRIGHTNESS_LEVEL1; break;case 2: case 3: brightness = CAMERA_BRIGHTNESS_LEVEL2; break;case 3: case 4: brightness = CAMERA_BRIGHTNESS_LEVEL3; break;case 4: case 5: brightness = CAMERA_BRIGHTNESS_LEVEL4; break;case 5: }switch (brightness_level) { ... } BSP_CAMERA_ContrastBrightnessConfig(contrast, brightness); }{ ... } /** * @brief Select effect. * @param effect: Camera effect. * @retval None *//* ... */ void CAMERA_SelectEffect(uint32_t effect) { switch (effect) { case 0: CAMERA_Set_ContrastBrightness(CameraSettings.b.contrast, CameraSettings.b.brightness); break;case 0: case 1: BSP_CAMERA_BlackWhiteConfig(CAMERA_BLACK_WHITE_BW); break;case 1: case 2: BSP_CAMERA_BlackWhiteConfig(CAMERA_BLACK_WHITE_NEGATIVE); break;case 2: case 3: BSP_CAMERA_ColorEffectConfig(CAMERA_COLOR_EFFECT_ANTIQUE); break;case 3: case 4: BSP_CAMERA_ColorEffectConfig(CAMERA_COLOR_EFFECT_BLUE); break;case 4: case 5: BSP_CAMERA_ColorEffectConfig(CAMERA_COLOR_EFFECT_GREEN); break;case 5: case 6: BSP_CAMERA_ColorEffectConfig(CAMERA_COLOR_EFFECT_RED); break; case 6: }switch (effect) { ... } }{ ... } /** * @brief Initialize the camera module. * @param None * @retval None *//* ... */ void CAMERA_Init(void) { /* Disable AUDIO Clock, because of shared pins */ BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_SPEAKER, 0, 0); AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x200, 0x0); AUDIO_IO_Write(AUDIO_I2C_ADDRESS, 0x300, 0x6010); /* Initialize the Camera */ CameraError = BSP_CAMERA_Init(RESOLUTION_R320x240); if( CameraError != CAMERA_ERROR) { GUI_Delay(100); /* Start the capture */ BSP_CAMERA_ContinuousStart((uint8_t *)CAMERA_FRAME_BUFFER); CAMERA_Set_ContrastBrightness(CameraSettings.b.contrast, CameraSettings.b.brightness); }if (CameraError != CAMERA_ERROR) { ... } }{ ... } /** * @brief Suspend the camera capture. * @param None * @retval None *//* ... */ void CAMERA_Suspend(void) { if( CameraError != CAMERA_ERROR) { /* Suspend the CAMERA capture */ BSP_CAMERA_Suspend(); }if (CameraError != CAMERA_ERROR) { ... } }{ ... } /** * @brief Resume the camera capture. * @param None * @retval None *//* ... */ void CAMERA_Resume(void) { if( CameraError != CAMERA_ERROR) { /* Resume the CAMERA capture */ BSP_CAMERA_Resume(); }if (CameraError != CAMERA_ERROR) { ... } }{ ... } /** * @brief Stop the camera capture. * @param None * @retval None *//* ... */ void CAMERA_Stop(void) { if( CameraError != CAMERA_ERROR) { /* Disable Camera request and Disable DCMI capture */ BSP_CAMERA_Stop(); }if (CameraError != CAMERA_ERROR) { ... } /* After SD disconnection, a SD Init is required */ BSP_SD_Init(); /* Restore Audio configuration */ k_BspAudioInit(); }{ ... } /** * @brief Return Camera State * @param None * @retval Camera State *//* ... */ uint8_t CAMERA_GetState(void) { return CameraError; }{ ... } /** * @brief Camera error callback. * @param None * @retval None *//* ... */ void BSP_CAMERA_ErrorCallback(void) { if( CameraError != CAMERA_ERROR) { BSP_CAMERA_Resume(); }if (CameraError != CAMERA_ERROR) { ... } }{ ... } /** * @brief Save the data to specified file. * @param path: pointer to the saving path * @retval File saved *//* ... */ uint8_t CAMERA_SaveToFile(uint8_t *path) { RTC_TimeTypeDef Time; RTC_DateTypeDef Date; FIL file; uint32_t NumWrittenData; uint8_t ret = 1; char filename[FILEMGR_FILE_NAME_SIZE]; char fullpath[FILEMGR_FILE_NAME_SIZE]; /* Create filename */ k_GetTime(&Time); k_GetDate(&Date); sprintf((char *)filename, "/Camera_%02d%02d%04d_%02d%02d%02d.bmp", Date.Date, Date.Month, Date.Year + 2015, Time.Hours, Time.Minutes, Time.Seconds); strcpy((char *)fullpath, (char *)path); strcat ((char *)fullpath, (char *)filename); BSP_CAMERA_Suspend(); /* Can not create file */ if (f_open(&file, (char *)fullpath, FA_CREATE_NEW | FA_WRITE) == FR_OK) { /* Write the received data into the file */ if (f_write(&file, (char *)BMPHeader_QQVGA24Bit, RGB_HEADER_SIZE, (UINT *)&NumWrittenData) == FR_OK) { f_sync(&file); /* Convert RGB16 image to RGB24 */ RGB16toRGB24((uint8_t *)CAMERA_CVRT_BUFFER, (uint8_t *)CAMERA_FRAME_BUFFER); if (f_write(&file, (char *)CAMERA_CVRT_BUFFER, MAX_IMAGE_SIZE, (UINT*)&NumWrittenData)== FR_OK) { /*File Written correctly */ ret = 0; }if (f_write(&file, (char *)CAMERA_CVRT_BUFFER, MAX_IMAGE_SIZE, (UINT*)&NumWrittenData)== FR_OK) { ... } }if (f_write(&file, (char *)BMPHeader_QQVGA24Bit, RGB_HEADER_SIZE, (UINT *)&NumWrittenData) == FR_OK) { ... } f_close(&file); }if (f_open(&file, (char *)fullpath, FA_CREATE_NEW | FA_WRITE) == FR_OK) { ... } BSP_CAMERA_Resume(); return ret; }{ ... } /** * @brief Convert RGB16 image to RGB24. * @param pDestBuffer: buffer hold the data in RGB24 format * @param pSrcBuffer: buffer hold the data in RGB16 format * @retval None *//* ... */ static void RGB16toRGB24(uint8_t *pDestBuffer, uint8_t *pSrcBuffer) { uint16_t *pSrc; uint8_t *pDest; uint32_t i = 0, j = 0; uint16_t value; pSrc = (uint16_t*) & pSrcBuffer[IMAGE_BUFFER_SIZE] - IMAGE_LINE_SIZE; pDest = (uint8_t*) & pDestBuffer[0]; for (i = IMAGE_COLUMN_SIZE; i > 0; i-- ) { for ( j = 0; j < 2 * IMAGE_LINE_SIZE; j += 2 ) { value = (uint16_t) * pSrc; *pDest++ = BMP_PIXEL16_TO_R(value); *pDest++ = BMP_PIXEL16_TO_G(value); *pDest++ = BMP_PIXEL16_TO_B(value); pSrc++; }for (j = 0; j < 2 * IMAGE_LINE_SIZE; j += 2) { ... } pSrc -= (IMAGE_LINE_SIZE * 2); }for (i = IMAGE_COLUMN_SIZE; i > 0; i--) { ... } }{ ... } /** * @} *//* ... */ /** * @} *//* ... */