This is a sample program that uses two Si1143 sensors.

Dependencies:   GR-PEACH_video SI1143 mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DisplayBace.h"
00003 #include "rtos.h"
00004 #include "SI1143.h"
00005 #include "image_data.h"
00006 
00007 /**** LCD Parameter **********/
00008 #define LCD_DE_MODE            (0)
00009 #define LCD_SYNC_MODE          (1)
00010 
00011 #define LCD_DOT_CLOCK          (13.40f)            // 13.4MHz
00012 
00013 #define LCD_H_WIDTH            (480u)
00014 #define LCD_H_BACK_PORCH       (43u)
00015 #define LCD_H_FRONT_PORCH      (52u)
00016 #define LCD_H_SYNC_WIDTH       (41u)
00017 
00018 #define LCD_V_WIDTH            (272u)
00019 #define LCD_V_BACK_PORCH       (12u)
00020 #define LCD_V_FRONT_PORCH      (2u)
00021 #define LCD_V_SYNC_WIDTH       (10u)
00022 
00023 #define LCD_MODE               (LCD_SYNC_MODE)
00024 
00025 /*****************************/
00026 
00027 
00028 #define FRAME_BUFFER_BYTE_PER_PIXEL      (2u)
00029 #define VIDEO_BUFFER_STRIDE    (((LCD_H_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL) + 31u) & ~31u)
00030 #define VIDEO_BUFFER_HEIGHT    (LCD_V_WIDTH)
00031 
00032 #define IMG_RGB565             (0)
00033 #define IMG_ARGB1555           (2)
00034 #define IMG_ARGB4444           (3)
00035 #define IMG_CLUT8              (6)
00036 #define IMG_CLUT4              (7)
00037 #define IMG_CLUT1              (8)
00038 #define IMG_XRGB8888           (9)
00039 #define IMG_ARGB8888           (10)
00040 #define IMG_YUV422             (11)
00041 
00042 enum {
00043     IMG_SETTING,
00044     IMG_START,
00045     IMG_RED,
00046     IMG_GREEN,
00047     IMG_BLUE,
00048     IMG_YELLOW,
00049     IMG_LIGHTBLUE,
00050     IMG_PINK,
00051     IMG_WHITE
00052 };
00053 
00054 enum {
00055     SENSE1_NON,
00056     SENSE1_1,
00057     SENSE1_2,
00058     SENSE1_3
00059 };
00060 
00061 enum {
00062     SENSE2_NON,
00063     SENSE2_1,
00064     SENSE2_2,
00065     SENSE2_3
00066 };
00067 
00068 DigitalOut lcd_pwon(P7_15);
00069 DigitalOut lcd_blon(P8_1);
00070 PwmOut     lcd_cntrst(P8_15);
00071 SI1143     sensor1(I2C_SDA, I2C_SCL);
00072 SI1143     sensor2(P1_7, P1_6);
00073 
00074 typedef struct {
00075     uint32_t    dummy1;
00076     uint32_t    offset_to_image;
00077     uint32_t    dummy2;
00078     uint16_t    width;
00079     uint16_t    height;
00080     uint8_t     type;
00081     uint8_t     dummy3;
00082     uint32_t    dummy4;
00083 } graphics_image_t;
00084 
00085 typedef struct {
00086     const graphics_image_t* image;
00087     uint32_t pos_x;
00088     uint32_t pos_y;
00089 } draw_info_t;
00090 
00091 typedef struct {
00092     draw_info_t curr;
00093     draw_info_t last;
00094 } image_draw_t;
00095 
00096 static const graphics_image_t* image_file[9] = {
00097     g_setting, g_start, g_red, g_gren, g_blue, g_yellow, g_lightblue, g_pink, g_white
00098 };
00099 
00100 static uint8_t user_frame_buffer[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(32)));  //32 bytes aligned!;
00101 static uint8_t user_frame_buffer2[VIDEO_BUFFER_STRIDE * VIDEO_BUFFER_HEIGHT]__attribute((section("NC_BSS"),aligned(32)));  //32 bytes aligned!;
00102 static volatile int32_t vsync_count;
00103 static uint8_t* disp_buff_addr;
00104 static int sense1[3] = {0, 0, 0};
00105 static int sense2[3] = {0, 0, 0};
00106 static image_draw_t img1_info;
00107 static image_draw_t img2_info;
00108 
00109 static void IntCallbackFunc_Vsync(DisplayBase::int_type_t int_type)
00110 {
00111     //Interrupt callback function for Vsync interruption
00112     if (vsync_count > 0) {
00113         vsync_count--;
00114     }
00115 }
00116 
00117 static void Wait_Vsync(const int32_t wait_count)
00118 {
00119     //Wait for the specified number of times Vsync occurs
00120     vsync_count = wait_count;
00121     while (vsync_count > 0) {
00122         /* Do nothing */
00123     }
00124 }
00125 
00126 static void Clr_FrameBuffer(uint8_t frmbuf_num) {
00127     int vcnt, wcnt;
00128     uint16_t * fb_base;
00129     uint16_t * fb_pos;
00130 
00131     if ((frmbuf_num & 0x01) != 0) {
00132         fb_base = (uint16_t *)user_frame_buffer;
00133         for (vcnt = 0; vcnt < LCD_V_WIDTH; vcnt++) {
00134             fb_pos = fb_base + (vcnt * LCD_H_WIDTH);
00135             for (wcnt = 0; wcnt < LCD_H_WIDTH; wcnt++) {
00136                 fb_pos[wcnt] = 0xFFFF;
00137             }
00138         }
00139     }
00140     if ((frmbuf_num & 0x02) != 0) {
00141         fb_base = (uint16_t *)user_frame_buffer2;
00142         for (vcnt = 0; vcnt < LCD_V_WIDTH; vcnt++) {
00143             fb_pos = fb_base + (vcnt * LCD_H_WIDTH);
00144             for (wcnt = 0; wcnt < LCD_H_WIDTH; wcnt++) {
00145                 fb_pos[wcnt] = 0xFFFF;
00146             }
00147         }
00148     }
00149 }
00150 
00151 static void Draw_Image(void * dst_buff, uint32_t dst_stride, const graphics_image_t * src_image, uint32_t pos_x, uint32_t pos_y) {
00152     uint32_t    offset_to_image;
00153     uint16_t    width;
00154     uint16_t    height;
00155     uint8_t     type;
00156     uint16_t  * pSrc16;
00157     uint32_t  * pSrc32;
00158     uint16_t  * pDst16;
00159     uint32_t  * pDst32;
00160     uint32_t    x,y;
00161 
00162     if (src_image != NULL) {
00163         offset_to_image   = src_image->offset_to_image;
00164         width             = src_image->width;
00165         height            = src_image->height;
00166         type              = src_image->type;
00167 
00168         if (type == IMG_ARGB4444) {
00169             pDst16 = (uint16_t *)dst_buff;
00170             pSrc16 = (uint16_t *)((uint8_t *)src_image +  offset_to_image );
00171 
00172             for (y = 0; y < height; y++) {
00173                 for (x = 0; x < width; x++) {
00174                     if ((pSrc16[(y * width) + x] & 0xf000) != 0) {
00175                         /* Copy the pixel value if its alpha value is not 0 */
00176                         pDst16[((y + pos_y) * dst_stride / 2) + (x + pos_x)] = pSrc16[(y * width) + x];
00177                     }
00178                 }
00179             }
00180         } else if (type == IMG_ARGB8888) {
00181             pDst32 = (uint32_t *)dst_buff;
00182             pSrc32 = (uint32_t *)((uint8_t *)src_image + offset_to_image);
00183 
00184             for (y = 0; y < height; y++) {
00185                 for (x = 0; x < width; x++) {
00186                     if ((pSrc32[(y * width) + x] & 0xff000000) != 0) {
00187                         /* Copy the pixel value if its alpha value is not 0 */
00188                         pDst32[((y + pos_y) * dst_stride / 4) + (x + pos_x)] = pSrc32[(y * width) + x];
00189                     }
00190                 }
00191             }
00192         } else if (type == IMG_RGB565) {
00193             pDst16 = (uint16_t *)dst_buff;
00194             pSrc16 = (uint16_t *)((uint8_t *)src_image + offset_to_image);
00195 
00196             for (y = 0; y < height; y++) {
00197                 for (x = 0; x < width; x++) {
00198                     pDst16[((y + pos_y) * dst_stride / 2) + (x + pos_x)] = pSrc16[(y * width) + x];
00199                 }
00200             }
00201         }
00202     }
00203 }
00204 
00205 static int Get_Sensor1(void) {
00206     int sense_num = SENSE1_NON;
00207 
00208     // Read each led sensor
00209     sense1[0] = sensor1.get_ps1(5);
00210     sense1[1] = sensor1.get_ps2(5);
00211     sense1[2] = sensor1.get_ps3(5);
00212 
00213     if (sense1[0] > 500 || sense1[1] > 500 || sense1[2] > 500) {
00214         if (sense1[0] > sense1[1] && sense1[0] > sense1[2]) {
00215             sense_num = SENSE1_1;
00216         } else if (sense1[1] > sense1[0] && sense1[1] > sense1[2]) {
00217             sense_num = SENSE1_2;
00218         } else if (sense1[2] > sense1[0] && sense1[2] > sense1[1]) {
00219             sense_num = SENSE1_3;
00220         }
00221     }
00222 
00223     return sense_num;
00224 }
00225 
00226 static int Get_Sensor2(void) {
00227     int sense_num = SENSE2_NON;
00228 
00229     // Read each led sensor
00230     sense2[0] = sensor2.get_ps1(5);
00231     sense2[1] = sensor2.get_ps2(5);
00232     sense2[2] = sensor2.get_ps3(5);
00233 
00234     if (sense2[0] > 500 || sense2[1] > 500 || sense2[2] > 500) {
00235         if (sense2[0] > sense2[1] && sense2[0] > sense2[2]) {
00236             sense_num = SENSE2_1;
00237         } else if (sense2[1] > sense2[0] && sense2[1] > sense2[2]) {
00238             sense_num = SENSE2_2;
00239         } else if (sense2[2] > sense2[0] && sense2[2] > sense2[1]) {
00240             sense_num = SENSE2_3;
00241         }
00242     }
00243 
00244     return sense_num;
00245 }
00246 
00247 static void Swap_FrameBuffer(void) {
00248     if (disp_buff_addr == user_frame_buffer) {
00249         disp_buff_addr = user_frame_buffer2;
00250         Clr_FrameBuffer(0x02);
00251     } else {
00252         disp_buff_addr = user_frame_buffer;
00253         Clr_FrameBuffer(0x01);
00254     }
00255 }
00256 
00257 int main(void)
00258 {
00259     /* Create DisplayBase object */
00260     DisplayBase Display;
00261     DisplayBase::graphics_error_t error;
00262     DisplayBase::lcd_config_t lcd_config;
00263     disp_buff_addr = user_frame_buffer;
00264     int sense1 = SENSE1_NON;
00265     int sense2 = SENSE2_NON;
00266     img1_info.curr.image = image_file[IMG_START];
00267     img1_info.curr.pos_x = 0;
00268     img1_info.curr.pos_y = 0;
00269     img1_info.last.image = image_file[IMG_WHITE];
00270     img1_info.last.pos_x = 0;
00271     img1_info.last.pos_y = 0;
00272     img2_info.curr.image = image_file[IMG_START];
00273     img2_info.curr.pos_x = 0;
00274     img2_info.curr.pos_y = 0;
00275     img2_info.last.image = image_file[IMG_WHITE];
00276     img2_info.last.pos_x = 240;
00277     img2_info.last.pos_y = 0;
00278 
00279     lcd_pwon = 0;
00280     lcd_blon = 0;
00281     Thread::wait(100);
00282     lcd_pwon = 1;
00283     lcd_blon = 1;
00284     Thread::wait(100);
00285 
00286     // Setup display
00287     vsync_count = 0;
00288     PinName lvds_pin[8] = {
00289         /* data pin */
00290         P5_7, P5_6, P5_5, P5_4, P5_3, P5_2, P5_1, P5_0
00291     };
00292     DisplayBase::rect_t rect;
00293 
00294     lcd_config.lcd_type             = DisplayBase::LCD_TYPE_LVDS;
00295     lcd_config.intputClock          = 66.67f;
00296     lcd_config.outputClock          = LCD_DOT_CLOCK;
00297     lcd_config.lcd_outformat        = DisplayBase::LCD_OUTFORMAT_RGB888;
00298     lcd_config.lcd_edge             = DisplayBase::EDGE_RISING;
00299 #if(LCD_MODE) //SYNC Mode
00300     lcd_config.h_toatal_period      = (LCD_H_BACK_PORCH + LCD_H_WIDTH + LCD_H_FRONT_PORCH);
00301     lcd_config.v_toatal_period      = (LCD_V_BACK_PORCH + LCD_V_WIDTH + LCD_V_FRONT_PORCH);
00302  
00303     lcd_config.h_disp_widht         = (LCD_H_WIDTH);
00304     lcd_config.v_disp_widht         = (LCD_V_WIDTH);
00305     lcd_config.h_back_porch         = (LCD_H_BACK_PORCH);
00306     lcd_config.v_back_porch         = (LCD_V_BACK_PORCH);
00307  
00308     lcd_config.h_sync_port          = DisplayBase::LCD_TCON_PIN_2;
00309     lcd_config.h_sync_port_polarity = DisplayBase::SIG_POL_INVERTED;
00310     lcd_config.h_sync_width         = LCD_H_SYNC_WIDTH;
00311  
00312     lcd_config.v_sync_port          = DisplayBase::LCD_TCON_PIN_0;
00313     lcd_config.v_sync_port_polarity = DisplayBase::SIG_POL_INVERTED;
00314     lcd_config.v_sync_width         = LCD_V_SYNC_WIDTH;
00315  
00316     lcd_config.de_port              = DisplayBase::LCD_TCON_PIN_3;
00317     lcd_config.de_port_polarity     = DisplayBase::SIG_POL_NOT_INVERTED;
00318 #else  //DE Mode
00319     lcd_config.h_toatal_period      = (LCD_H_WIDTH + 80u);
00320     lcd_config.v_toatal_period      = (LCD_V_WIDTH);
00321 
00322     lcd_config.h_disp_widht         = (LCD_H_WIDTH);
00323     lcd_config.v_disp_widht         = (LCD_V_WIDTH);
00324     lcd_config.h_back_porch         = (68u);
00325     lcd_config.v_back_porch         = (18u);
00326 
00327     lcd_config.h_sync_port          = DisplayBase::LCD_TCON_PIN_NON;
00328     lcd_config.h_sync_port_polarity = DisplayBase::SIG_POL_NOT_INVERTED;
00329     lcd_config.h_sync_width         = 0;
00330 
00331     lcd_config.v_sync_port          = DisplayBase::LCD_TCON_PIN_NON;
00332     lcd_config.v_sync_port_polarity = DisplayBase::SIG_POL_NOT_INVERTED;
00333     lcd_config.v_sync_width         = 0;
00334 
00335     lcd_config.de_port              = DisplayBase::LCD_TCON_PIN_3;
00336     lcd_config.de_port_polarity     = DisplayBase::SIG_POL_INVERTED;
00337 #endif
00338 
00339     /* Graphics initialization process */
00340     error = Display.Graphics_init(&lcd_config);
00341     if (error != DisplayBase::GRAPHICS_OK) {
00342         printf("Line %d, error %d\n", __LINE__, error);
00343         while (1);
00344     }
00345 
00346     /* Interrupt callback function setting (Vsync signal output from scaler 0) */
00347     error = Display.Graphics_Irq_Handler_Set(DisplayBase::INT_TYPE_S0_LO_VSYNC, 0, IntCallbackFunc_Vsync);
00348     if (error != DisplayBase::GRAPHICS_OK) {
00349         printf("Line %d, error %d\n", __LINE__, error);
00350         while (1);
00351     }
00352 
00353     Display.Graphics_Lvds_Port_Init(lvds_pin, 8);
00354     rect.vs = 0;
00355     rect.vw = LCD_V_WIDTH;
00356     rect.hs = 0;
00357     rect.hw = LCD_H_WIDTH;
00358 
00359     Display.Graphics_Read_Setting(
00360         DisplayBase::GRAPHICS_LAYER_0,
00361         (void *)disp_buff_addr,
00362         VIDEO_BUFFER_STRIDE,
00363         DisplayBase::GRAPHICS_FORMAT_RGB565,
00364         DisplayBase::WR_RD_WRSWA_32_16BIT,
00365         &rect
00366     );
00367     Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_0);
00368     lcd_cntrst.write(1.0);
00369 
00370     // Setup the baseline
00371     Draw_Image(disp_buff_addr, VIDEO_BUFFER_STRIDE, image_file[IMG_SETTING], 0, 0);
00372     printf("SI1143 Gesture Sensor setting...\n");
00373     sensor1.bias(1,5);
00374     sensor2.bias(1,5);
00375     Thread::wait(1000);
00376     Draw_Image(disp_buff_addr, VIDEO_BUFFER_STRIDE, image_file[IMG_START], 0, 0);
00377     printf("SI1143 Gesture Sensor setting finished!\n");
00378     while (1) {
00379         sense1 = Get_Sensor1();
00380         switch (sense1) {
00381             case SENSE1_1 :
00382                 img1_info.curr.image = image_file[IMG_RED];
00383                 img1_info.curr.pos_x = 0;
00384                 img1_info.curr.pos_y = 0;
00385                 break;
00386             case SENSE1_2 :
00387                 img1_info.curr.image = image_file[IMG_GREEN];
00388                 img1_info.curr.pos_x = 80;
00389                 img1_info.curr.pos_y = 0;
00390                 break;
00391             case SENSE1_3 :
00392                 img1_info.curr.image = image_file[IMG_BLUE];
00393                 img1_info.curr.pos_x = 160;
00394                 img1_info.curr.pos_y = 0;
00395                 break;
00396             default :
00397                 img1_info.curr.image = NULL;
00398                 img1_info.curr.pos_x = 0;
00399                 img1_info.curr.pos_y = 0;
00400                 break;
00401         }
00402         sense2 = Get_Sensor2();
00403         switch (sense2) {
00404             case SENSE2_1 :
00405                 img2_info.curr.image = image_file[IMG_YELLOW];
00406                 img2_info.curr.pos_x = 240;
00407                 img2_info.curr.pos_y = 0;
00408                 break;
00409             case SENSE2_2 :
00410                 img2_info.curr.image = image_file[IMG_LIGHTBLUE];
00411                 img2_info.curr.pos_x = 320;
00412                 img2_info.curr.pos_y = 0;
00413                 break;
00414             case SENSE2_3 :
00415                 img2_info.curr.image = image_file[IMG_PINK];
00416                 img2_info.curr.pos_x = 400;
00417                 img2_info.curr.pos_y = 0;
00418                 break;
00419             default :
00420                 img2_info.curr.image = NULL;
00421                 img2_info.curr.pos_x = 0;
00422                 img2_info.curr.pos_y = 0;
00423                 break;
00424         }
00425 
00426         if ((sense1 != 0) || (sense2 != 0)) {
00427             /* Draw Image */
00428             Swap_FrameBuffer();
00429             if (sense1 != 0) {
00430                 Draw_Image(disp_buff_addr, VIDEO_BUFFER_STRIDE, img1_info.curr.image, img1_info.curr.pos_x, img1_info.curr.pos_y);
00431                 Draw_Image(disp_buff_addr, VIDEO_BUFFER_STRIDE, img2_info.last.image, img2_info.last.pos_x, img2_info.last.pos_y);
00432                 img1_info.last.image = img1_info.curr.image;
00433                 img1_info.last.pos_x = img1_info.curr.pos_x;
00434                 img1_info.last.pos_y = img1_info.curr.pos_y;
00435             }
00436             if (sense2 != 0) {
00437                 Draw_Image(disp_buff_addr, VIDEO_BUFFER_STRIDE, img1_info.last.image, img1_info.last.pos_x, img1_info.last.pos_y);
00438                 Draw_Image(disp_buff_addr, VIDEO_BUFFER_STRIDE, img2_info.curr.image, img2_info.curr.pos_x, img2_info.curr.pos_y);
00439                 img2_info.last.image = img2_info.curr.image;
00440                 img2_info.last.pos_x = img2_info.curr.pos_x;
00441                 img2_info.last.pos_y = img2_info.curr.pos_y;
00442             }
00443             Display.Graphics_Read_Change(DisplayBase::GRAPHICS_LAYER_0, (void *)disp_buff_addr);
00444             Wait_Vsync(1);
00445         }
00446     }
00447 }