Mbed Client sample for GR-LYCHEE where ZXing is incorporated.

Dependencies:   DisplayApp AsciiFont

Fork of GR-PEACH_mbed-os-client-ZXingSample by Renesas

Overview

This sample program shows how to use mbed Client together with ZXing which is an open-source, multi-format 1D/2D barcode image processing library. For more info on ZXing, please refer to https://github.com/zxing/zxing.

Required hardware

Application setup

  1. Select the connection type. For details, please refer to the following wiki:
    https://os.mbed.com/users/1050186/code/GR-LYCHEE_mbed-os-client-ZXingSample/wiki/Connection-type.
  2. Set the client credentials. For details, please refer to the following wiki:
    https://os.mbed.com/users/1050186/code/GR-LYCHEE_mbed-os-client-ZXingSample/wiki/Client-credentials.
  3. Change Wifi settings. For details, please refer to the following wiki:
    https://os.mbed.com/users/1050186/code/GR-LYCHEE_mbed-os-client-ZXingSample/wiki/Wifi-settings.

Building the example

To build this example:

  1. Import this example onto mbed Compiler.
  2. Configure the example in accordance with Application setup.
  3. Compile the example on mbed Compiler and download the resultant binary file.
  4. Plug the micro-USB cable into the OpenSDA port which lies on the next to the RESET button.
  5. Copy the binary previously downloaded to your PC to GR-LYCHEE to flash this example. When the copy is successfully completed, the board is ready to work.
  6. Press the RESET button on the board to run the example.
  7. For verification, please refer to the following wiki:
    https://os.mbed.com/users/1050186/code/GR-LYCHEE_mbed-os-client-ZXingSample/wiki/Monitoring-the-application.

Application resources

This example exposes four resources listed below:

  1. 3202/0/5700. Decode result of barcode data input from camera (GET).
  2. 3201/0/5850. Blink function, blinks LED when executed (POST).
  3. 3201/0/5853. Blink pattern, used by the blink function to determine how to blink. In the format of 1000:500:1000:500:1000:500 (PUT).
  4. 3201/0/5855. Blink color, used by the blink function. Any of green, yellow, orange and red is acceptable (PUT).
Committer:
1050186
Date:
Thu Feb 15 04:29:52 2018 +0000
Revision:
6:4e43b6f8f772
Parent:
4:076b3dd7c0e6
Child:
10:c36cf363bf55
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 0:eb73febb2bba 1
<> 0:eb73febb2bba 2 #include "mbed.h"
1050186 6:4e43b6f8f772 3 #include "EasyAttach_CameraAndLCD.h"
<> 0:eb73febb2bba 4 #include "ImageReaderSource.h"
<> 0:eb73febb2bba 5
<> 0:eb73febb2bba 6 /**** User Selection *********/
dkato 2:6ec5c1c1d41c 7 /** JPEG out setting **/
1050186 6:4e43b6f8f772 8 #define JPEG_SEND (1) /* Select 0(JPEG images are not output to PC) or 1(JPEG images are output to PC on USB(CDC) for focusing the camera) */
1050186 6:4e43b6f8f772 9 #define JPEG_ENCODE_QUALITY (75) /* JPEG encode quality (min:1, max:75 (Considering the size of JpegBuffer, about 75 is the upper limit.)) */
1050186 6:4e43b6f8f772 10 #define VFIELD_INT_SKIP_CNT (0) /* A guide for GR-LYCHEE. 0:60fps, 1:30fps, 2:20fps, 3:15fps, 4:12fps, 5:10fps */
dkato 2:6ec5c1c1d41c 11 /** Decode hints **/
dkato 2:6ec5c1c1d41c 12 #define DECODE_HINTS (DecodeHints::ONED_HINT | DecodeHints::QR_CODE_HINT | DecodeHints::DATA_MATRIX_HINT | DecodeHints::AZTEC_HINT)
1050186 6:4e43b6f8f772 13 /** Camera setting **/
1050186 6:4e43b6f8f772 14 #define OV7725_SETTING_TEST (0) /* Exposure and Gain Setting Test 0:disable 1:enable */
<> 0:eb73febb2bba 15 /*****************************/
<> 0:eb73febb2bba 16
1050186 6:4e43b6f8f772 17 /* Video input and LCD layer 0 output */
1050186 6:4e43b6f8f772 18 #define VIDEO_FORMAT (DisplayBase::VIDEO_FORMAT_YCBCR422)
1050186 6:4e43b6f8f772 19 #define GRAPHICS_FORMAT (DisplayBase::GRAPHICS_FORMAT_YCBCR422)
1050186 6:4e43b6f8f772 20 //#define WR_RD_WRSWA (DisplayBase::WR_RD_WRSWA_32_16BIT)
1050186 6:4e43b6f8f772 21 #define WR_RD_WRSWA (DisplayBase::WR_RD_WRSWA_16BIT)
1050186 6:4e43b6f8f772 22 #define DATA_SIZE_PER_PIC (2u)
1050186 6:4e43b6f8f772 23
1050186 6:4e43b6f8f772 24 /*! Frame buffer stride: Frame buffer stride should be set to a multiple of 32 or 128
1050186 6:4e43b6f8f772 25 in accordance with the frame buffer burst transfer mode. */
1050186 6:4e43b6f8f772 26 #if MBED_CONF_APP_LCD
1050186 6:4e43b6f8f772 27 #define VIDEO_PIXEL_HW LCD_PIXEL_WIDTH /* QVGA */
1050186 6:4e43b6f8f772 28 #define VIDEO_PIXEL_VW LCD_PIXEL_HEIGHT /* QVGA */
<> 0:eb73febb2bba 29 #else
1050186 6:4e43b6f8f772 30 #define VIDEO_PIXEL_HW (640u) /* VGA */
1050186 6:4e43b6f8f772 31 #define VIDEO_PIXEL_VW (480u) /* VGA */
<> 0:eb73febb2bba 32 #endif
1050186 6:4e43b6f8f772 33
1050186 6:4e43b6f8f772 34 #define FRAME_BUFFER_STRIDE (((VIDEO_PIXEL_HW * DATA_SIZE_PER_PIC) + 31u) & ~31u)
1050186 6:4e43b6f8f772 35 #define FRAME_BUFFER_HEIGHT (VIDEO_PIXEL_VW)
1050186 6:4e43b6f8f772 36
1050186 6:4e43b6f8f772 37 DisplayBase Display;
1050186 6:4e43b6f8f772 38 // Timer
1050186 6:4e43b6f8f772 39 static Timer decode_timer;
1050186 6:4e43b6f8f772 40
1050186 6:4e43b6f8f772 41 #if defined(__ICCARM__)
1050186 6:4e43b6f8f772 42 #pragma data_alignment=32
1050186 6:4e43b6f8f772 43 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]@ ".mirrorram";
1050186 6:4e43b6f8f772 44 #else
1050186 6:4e43b6f8f772 45 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * FRAME_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(32)));
dkato 2:6ec5c1c1d41c 46 #endif
dkato 2:6ec5c1c1d41c 47
dkato 4:076b3dd7c0e6 48 #if JPEG_SEND
1050186 6:4e43b6f8f772 49 #include "JPEG_Converter.h"
1050186 6:4e43b6f8f772 50 #include "DisplayApp.h"
1050186 6:4e43b6f8f772 51 #include "dcache-control.h"
dkato 2:6ec5c1c1d41c 52
1050186 6:4e43b6f8f772 53 #if defined(__ICCARM__)
1050186 6:4e43b6f8f772 54 #pragma data_alignment=32
1050186 6:4e43b6f8f772 55 static uint8_t JpegBuffer[2][1024 * 64];
dkato 2:6ec5c1c1d41c 56 #else
1050186 6:4e43b6f8f772 57 static uint8_t JpegBuffer[2][1024 * 64]__attribute((aligned(32)));
<> 0:eb73febb2bba 58 #endif
1050186 6:4e43b6f8f772 59 static size_t jcu_encode_size[2];
1050186 6:4e43b6f8f772 60 static JPEG_Converter Jcu;
1050186 6:4e43b6f8f772 61 static int jcu_buf_index_write = 0;
1050186 6:4e43b6f8f772 62 static int jcu_buf_index_write_done = 0;
1050186 6:4e43b6f8f772 63 static int jcu_buf_index_read = 0;
1050186 6:4e43b6f8f772 64 static volatile int jcu_encoding = 0;
1050186 6:4e43b6f8f772 65 static volatile int image_change = 0;
1050186 6:4e43b6f8f772 66 static DisplayApp display_app;
1050186 6:4e43b6f8f772 67 static int Vfield_Int_Cnt = 0;
<> 0:eb73febb2bba 68
<> 0:eb73febb2bba 69 static int decode_wait_time = 0;
<> 0:eb73febb2bba 70 static void (*p_callback_func)(char * addr, int size);
<> 0:eb73febb2bba 71
1050186 6:4e43b6f8f772 72 static void JcuEncodeCallBackFunc(JPEG_Converter::jpeg_conv_error_t err_code) {
1050186 6:4e43b6f8f772 73 if (err_code == JPEG_Converter::JPEG_CONV_OK) {
1050186 6:4e43b6f8f772 74 jcu_buf_index_write_done = jcu_buf_index_write;
1050186 6:4e43b6f8f772 75 image_change = 1;
1050186 6:4e43b6f8f772 76 }
1050186 6:4e43b6f8f772 77 jcu_encoding = 0;
1050186 6:4e43b6f8f772 78 }
<> 0:eb73febb2bba 79
1050186 6:4e43b6f8f772 80 static void snapshot(void) {
1050186 6:4e43b6f8f772 81 while ((jcu_encoding == 1) || (image_change == 0)) {
1050186 6:4e43b6f8f772 82 Thread::wait(1);
<> 0:eb73febb2bba 83 }
1050186 6:4e43b6f8f772 84 jcu_buf_index_read = jcu_buf_index_write_done;
1050186 6:4e43b6f8f772 85 image_change = 0;
1050186 6:4e43b6f8f772 86 display_app.SendJpeg(JpegBuffer[jcu_buf_index_read], (int)jcu_encode_size[jcu_buf_index_read]);
<> 0:eb73febb2bba 87 }
<> 0:eb73febb2bba 88
1050186 6:4e43b6f8f772 89 static void IntCallbackFunc_Vfield(DisplayBase::int_type_t int_type) {
1050186 6:4e43b6f8f772 90 if (Vfield_Int_Cnt < VFIELD_INT_SKIP_CNT) {
1050186 6:4e43b6f8f772 91 Vfield_Int_Cnt++;
1050186 6:4e43b6f8f772 92 return;
1050186 6:4e43b6f8f772 93 }
1050186 6:4e43b6f8f772 94 Vfield_Int_Cnt = 0;
1050186 6:4e43b6f8f772 95
1050186 6:4e43b6f8f772 96 //Interrupt callback function
1050186 6:4e43b6f8f772 97 if (jcu_encoding == 0) {
1050186 6:4e43b6f8f772 98 JPEG_Converter::bitmap_buff_info_t bitmap_buff_info;
1050186 6:4e43b6f8f772 99 JPEG_Converter::encode_options_t encode_options;
1050186 6:4e43b6f8f772 100
1050186 6:4e43b6f8f772 101 bitmap_buff_info.width = VIDEO_PIXEL_HW;
1050186 6:4e43b6f8f772 102 bitmap_buff_info.height = VIDEO_PIXEL_VW;
1050186 6:4e43b6f8f772 103 bitmap_buff_info.format = JPEG_Converter::WR_RD_YCbCr422;
1050186 6:4e43b6f8f772 104 bitmap_buff_info.buffer_address = (void *)user_frame_buffer0;
1050186 6:4e43b6f8f772 105
1050186 6:4e43b6f8f772 106 encode_options.encode_buff_size = sizeof(JpegBuffer[0]);
1050186 6:4e43b6f8f772 107 encode_options.p_EncodeCallBackFunc = &JcuEncodeCallBackFunc;
1050186 6:4e43b6f8f772 108 // encode_options.input_swapsetting = JPEG_Converter::WR_RD_WRSWA_32_16_8BIT;
1050186 6:4e43b6f8f772 109 encode_options.input_swapsetting = JPEG_Converter::WR_RD_WRSWA_16_8BIT;
1050186 6:4e43b6f8f772 110
1050186 6:4e43b6f8f772 111 jcu_encoding = 1;
1050186 6:4e43b6f8f772 112 if (jcu_buf_index_read == jcu_buf_index_write) {
1050186 6:4e43b6f8f772 113 jcu_buf_index_write ^= 1; // toggle
1050186 6:4e43b6f8f772 114 }
1050186 6:4e43b6f8f772 115 jcu_encode_size[jcu_buf_index_write] = 0;
1050186 6:4e43b6f8f772 116 dcache_invalid(JpegBuffer[jcu_buf_index_write], sizeof(JpegBuffer[0]));
1050186 6:4e43b6f8f772 117 if (Jcu.encode(&bitmap_buff_info, JpegBuffer[jcu_buf_index_write],
1050186 6:4e43b6f8f772 118 &jcu_encode_size[jcu_buf_index_write], &encode_options) != JPEG_Converter::JPEG_CONV_OK) {
1050186 6:4e43b6f8f772 119 jcu_encode_size[jcu_buf_index_write] = 0;
1050186 6:4e43b6f8f772 120 jcu_encoding = 0;
1050186 6:4e43b6f8f772 121 }
1050186 6:4e43b6f8f772 122 }
1050186 6:4e43b6f8f772 123 }
1050186 6:4e43b6f8f772 124
1050186 6:4e43b6f8f772 125 #if OV7725_SETTING_TEST
1050186 6:4e43b6f8f772 126 static DisplayApp::touch_pos_t s_touch;
1050186 6:4e43b6f8f772 127
1050186 6:4e43b6f8f772 128 static bool touch_notification = false;
1050186 6:4e43b6f8f772 129
1050186 6:4e43b6f8f772 130 static void touch_callback(void) {
1050186 6:4e43b6f8f772 131 touch_notification = true;
1050186 6:4e43b6f8f772 132 }
1050186 6:4e43b6f8f772 133 #endif
1050186 6:4e43b6f8f772 134 #endif
1050186 6:4e43b6f8f772 135
1050186 6:4e43b6f8f772 136 static void Start_Video_Camera(void) {
1050186 6:4e43b6f8f772 137 // Video capture setting (progressive form fixed)
1050186 6:4e43b6f8f772 138 Display.Video_Write_Setting(
1050186 6:4e43b6f8f772 139 DisplayBase::VIDEO_INPUT_CHANNEL_0,
1050186 6:4e43b6f8f772 140 DisplayBase::COL_SYS_NTSC_358,
1050186 6:4e43b6f8f772 141 (void *)user_frame_buffer0,
1050186 6:4e43b6f8f772 142 FRAME_BUFFER_STRIDE,
1050186 6:4e43b6f8f772 143 VIDEO_FORMAT,
1050186 6:4e43b6f8f772 144 WR_RD_WRSWA,
1050186 6:4e43b6f8f772 145 VIDEO_PIXEL_VW,
1050186 6:4e43b6f8f772 146 VIDEO_PIXEL_HW
1050186 6:4e43b6f8f772 147 );
1050186 6:4e43b6f8f772 148 EasyAttach_CameraStart(Display, DisplayBase::VIDEO_INPUT_CHANNEL_0);
1050186 6:4e43b6f8f772 149 }
1050186 6:4e43b6f8f772 150
1050186 6:4e43b6f8f772 151 #if MBED_CONF_APP_LCD
1050186 6:4e43b6f8f772 152 static void Start_LCD_Display(void) {
<> 0:eb73febb2bba 153 DisplayBase::rect_t rect;
<> 0:eb73febb2bba 154
<> 0:eb73febb2bba 155 rect.vs = 0;
1050186 6:4e43b6f8f772 156 rect.vw = LCD_PIXEL_HEIGHT;
<> 0:eb73febb2bba 157 rect.hs = 0;
1050186 6:4e43b6f8f772 158 rect.hw = LCD_PIXEL_WIDTH;
<> 0:eb73febb2bba 159 Display.Graphics_Read_Setting(
<> 0:eb73febb2bba 160 DisplayBase::GRAPHICS_LAYER_0,
1050186 6:4e43b6f8f772 161 (void *)user_frame_buffer0,
<> 0:eb73febb2bba 162 FRAME_BUFFER_STRIDE,
<> 0:eb73febb2bba 163 GRAPHICS_FORMAT,
<> 0:eb73febb2bba 164 WR_RD_WRSWA,
<> 0:eb73febb2bba 165 &rect
<> 0:eb73febb2bba 166 );
<> 0:eb73febb2bba 167 Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_0);
dkato 2:6ec5c1c1d41c 168
1050186 6:4e43b6f8f772 169 Thread::wait(50);
1050186 6:4e43b6f8f772 170 EasyAttach_LcdBacklight(true);
dkato 2:6ec5c1c1d41c 171 }
<> 0:eb73febb2bba 172 #endif
<> 0:eb73febb2bba 173
<> 0:eb73febb2bba 174
<> 0:eb73febb2bba 175 /****** zxing_init ******/
<> 0:eb73febb2bba 176 void zxing_init(void (*pfunc)(char * addr, int size)) {
1050186 6:4e43b6f8f772 177 // Initialize the background to black
1050186 6:4e43b6f8f772 178 for (int i = 0; i < (int)sizeof(user_frame_buffer0); i += 2) {
1050186 6:4e43b6f8f772 179 user_frame_buffer0[i + 0] = 0x10;
1050186 6:4e43b6f8f772 180 user_frame_buffer0[i + 1] = 0x80;
1050186 6:4e43b6f8f772 181 }
<> 0:eb73febb2bba 182
1050186 6:4e43b6f8f772 183 EasyAttach_Init(Display);
1050186 6:4e43b6f8f772 184 #if JPEG_SEND
1050186 6:4e43b6f8f772 185 Jcu.SetQuality(JPEG_ENCODE_QUALITY);
1050186 6:4e43b6f8f772 186 // Interrupt callback function setting (Field end signal for recording function in scaler 0)
1050186 6:4e43b6f8f772 187 Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_VFIELD, 0, IntCallbackFunc_Vfield);
1050186 6:4e43b6f8f772 188 #if OV7725_SETTING_TEST
1050186 6:4e43b6f8f772 189 display_app.SetCallback(&touch_callback);
1050186 6:4e43b6f8f772 190 #endif
1050186 6:4e43b6f8f772 191 #endif
1050186 6:4e43b6f8f772 192 Start_Video_Camera();
1050186 6:4e43b6f8f772 193 #if MBED_CONF_APP_LCD
1050186 6:4e43b6f8f772 194 Start_LCD_Display();
1050186 6:4e43b6f8f772 195 #endif
<> 0:eb73febb2bba 196
<> 0:eb73febb2bba 197 p_callback_func = pfunc;
<> 0:eb73febb2bba 198 decode_timer.reset();
<> 0:eb73febb2bba 199 decode_timer.start();
<> 0:eb73febb2bba 200 }
<> 0:eb73febb2bba 201
<> 0:eb73febb2bba 202 /****** zxing_main ******/
<> 0:eb73febb2bba 203 int zxing_loop() {
<> 0:eb73febb2bba 204 int decode_result = -1;
<> 0:eb73febb2bba 205 vector<Ref<Result> > results;
dkato 2:6ec5c1c1d41c 206 char ** decode_str = NULL;
<> 0:eb73febb2bba 207
1050186 6:4e43b6f8f772 208 #if JPEG_SEND
1050186 6:4e43b6f8f772 209 snapshot();
1050186 6:4e43b6f8f772 210 #if OV7725_SETTING_TEST
1050186 6:4e43b6f8f772 211 // OV7725 Setting test
1050186 6:4e43b6f8f772 212 if (touch_notification) {
1050186 6:4e43b6f8f772 213 display_app.GetCoordinates(1, &s_touch);
1050186 6:4e43b6f8f772 214 if (s_touch.valid) {
1050186 6:4e43b6f8f772 215 uint16_t usManualExposure = (uint16_t)(0xFFFF * ((float)s_touch.x / 800.f));
1050186 6:4e43b6f8f772 216 uint8_t usManualGain = (uint8_t)(0xFF * ((float)s_touch.y / 480.0f));
1050186 6:4e43b6f8f772 217 OV7725_config::SetExposure(false, usManualExposure, usManualGain);
1050186 6:4e43b6f8f772 218 printf("usManualExposure 0x%04x, usManualGain 0x%02x\r\n", usManualExposure, usManualGain);
1050186 6:4e43b6f8f772 219 } else {
1050186 6:4e43b6f8f772 220 OV7725_config::SetExposure(true, 0x0000, 0x00);
1050186 6:4e43b6f8f772 221 printf("SetExposure Auto\r\n");
1050186 6:4e43b6f8f772 222 }
1050186 6:4e43b6f8f772 223 touch_notification = false;
1050186 6:4e43b6f8f772 224 }
1050186 6:4e43b6f8f772 225 #endif
1050186 6:4e43b6f8f772 226 #else
1050186 6:4e43b6f8f772 227 Thread::wait(1000);
1050186 6:4e43b6f8f772 228 #endif
1050186 6:4e43b6f8f772 229
<> 0:eb73febb2bba 230 /* Decode barcode image */
<> 0:eb73febb2bba 231 if (decode_timer.read_ms() >= decode_wait_time) {
<> 0:eb73febb2bba 232 decode_timer.reset();
dkato 2:6ec5c1c1d41c 233 DecodeHints hints(DECODE_HINTS);
dkato 2:6ec5c1c1d41c 234 hints.setTryHarder(false);
dkato 2:6ec5c1c1d41c 235 decode_result = ex_decode(user_frame_buffer0, (FRAME_BUFFER_STRIDE * VIDEO_PIXEL_VW), VIDEO_PIXEL_HW, VIDEO_PIXEL_VW, &results, hints);
<> 0:eb73febb2bba 236 if (decode_result == 0) {
<> 0:eb73febb2bba 237 decode_str = (char **)&(results[0]->getText()->getText());
dkato 2:6ec5c1c1d41c 238 int size = strlen(*decode_str);
<> 0:eb73febb2bba 239 if (p_callback_func != NULL) {
<> 0:eb73febb2bba 240 p_callback_func(*decode_str, size);
<> 0:eb73febb2bba 241 }
<> 0:eb73febb2bba 242 decode_wait_time = 500;
<> 0:eb73febb2bba 243 } else {
<> 0:eb73febb2bba 244 decode_wait_time = 10;
<> 0:eb73febb2bba 245 }
<> 0:eb73febb2bba 246 }
<> 0:eb73febb2bba 247
<> 0:eb73febb2bba 248 return decode_result;
<> 0:eb73febb2bba 249 }
dkato 2:6ec5c1c1d41c 250