Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: AsciiFont GR-PEACH_video GraphicsFramework LCD_shield_config R_BSP TLV320_RBSP mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "USBHostMSD.h" 00003 #include "usb_host_setting.h" 00004 #include "LCD.hpp" 00005 #include "MovFile.hpp" 00006 #include "TLV320_RBSP.h" 00007 #include "MovPlayer.hpp" 00008 #include "AsciiFont.h" 00009 #include "BinaryImage_RZ_A1H.h" 00010 #include "CppStandardHelper.hpp" 00011 00012 #define USE_TLV320 00013 00014 static constexpr int FRAME_BUFFER_BYTE_PER_PIXEL = 2; 00015 static constexpr int FRAME_BUFFER_STRIDE = ((LCD_PIXEL_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL) + 31u) & ~31u; 00016 static uint8_t MBED_ALIGN(32) user_frame_buffer[FRAME_BUFFER_STRIDE * LCD_PIXEL_HEIGHT]; 00017 00018 static constexpr int FileNameBufferNum = 16; 00019 static constexpr int FileNameBufferLength = 32; 00020 static char filenameBuffer[FileNameBufferNum][FileNameBufferLength]; 00021 00022 static rtos::Semaphore touchSemaphore(0); 00023 00024 #ifdef ONLINE_COMPILER 00025 namespace TouchAction { 00026 #endif 00027 ENUM TouchAction { 00028 None, 00029 Play0, 00030 Play1, 00031 Play2, 00032 ScrollUp, 00033 ScrollDown 00034 }; 00035 #ifdef ONLINE_COMPILER 00036 } 00037 #endif 00038 00039 namespace Menu { 00040 const graphics_image_t *top = menu; 00041 const graphics_image_t *one = menu1; 00042 const graphics_image_t *two = menu2; 00043 const graphics_image_t *three = menu3; 00044 const graphics_image_t *down = menud; 00045 const graphics_image_t *up = menuu; 00046 const graphics_image_t *upx = menuux; 00047 const graphics_image_t *downx = menudx; 00048 } 00049 00050 static void dcache_clean(void * p_buf, uint32_t size) { 00051 uint32_t start_addr = (uint32_t)p_buf & 0xFFFFFFE0; 00052 uint32_t end_addr = (uint32_t)p_buf + size; 00053 uint32_t addr; 00054 00055 /* Data cache clean */ 00056 for (addr = start_addr; addr < end_addr; addr += 0x20) { 00057 __v7_clean_dcache_mva((void *)addr); 00058 } 00059 } 00060 00061 static void touchCallback() 00062 { 00063 touchSemaphore.release(); 00064 } 00065 00066 #ifdef ONLINE_COMPILER 00067 static TouchAction::TouchAction readTouch(LCD *lcd, TouckKey_LCD_shield *touch, const graphics_image_t *currentImage) 00068 #else 00069 static TouchAction readTouch(LCD *lcd, TouckKey_LCD_shield *touch, const graphics_image_t *currentImage) 00070 #endif 00071 { 00072 TouchKey::touch_pos_t touchPos; 00073 printf("waiting for touch\n"); 00074 touchSemaphore.wait(); 00075 printf("touch detected\n"); 00076 touch->GetCoordinates(1, &touchPos); 00077 #ifdef ONLINE_COMPILER 00078 TouchAction::TouchAction ret = TouchAction::None; 00079 #else 00080 TouchAction ret = TouchAction::None; 00081 #endif 00082 const graphics_image_t *image = nullptr; 00083 int touchNum = 1; 00084 if (40 <= touchPos.x && touchPos.x <= 360) { 00085 if (35 <= touchPos.y && touchPos.y <= 70) { 00086 ret = TouchAction::Play0; 00087 image = Menu::one; 00088 } else if (115 <= touchPos.y && touchPos.y <= 155) { 00089 ret = TouchAction::Play1; 00090 image = Menu::two; 00091 } else if (195 <= touchPos.y && touchPos.y <= 235) { 00092 ret = TouchAction::Play2; 00093 image = Menu::three; 00094 } 00095 } else if (409 <= touchPos.x && touchPos.x <= 460) { 00096 if (22 <= touchPos.y && touchPos.y <= 68) { 00097 ret = TouchAction::ScrollUp; 00098 image = Menu::up; 00099 } else if (202 <= touchPos.y && touchPos.y <= 248) { 00100 ret = TouchAction::ScrollDown; 00101 image = Menu::down; 00102 } 00103 } 00104 if (lcd && image) { 00105 lcd->drawImage(image); 00106 } 00107 while (touchNum) { 00108 touchSemaphore.wait(); 00109 touchNum = touch->GetCoordinates(1, &touchPos); 00110 } 00111 if (lcd) { 00112 lcd->drawImage(currentImage); 00113 } 00114 return ret; 00115 } 00116 00117 int main(int MBED_UNUSED argc, const char MBED_UNUSED * argv[]) 00118 { 00119 DigitalOut usb1en(P3_8); 00120 USBHostMSD msd("usb"); 00121 usb1en = 1; 00122 rtos::Thread::wait(5); 00123 usb1en = 0; 00124 00125 DigitalOut led(LED1); 00126 while (! msd.connect()) { 00127 printf("not connected\n"); 00128 rtos::Thread::wait(500); 00129 led = ! led; 00130 } 00131 led = 0; 00132 00133 LCD *lcd = LCD::singleton(); 00134 lcd->start(); 00135 00136 #ifdef USE_TLV320 00137 TLV320_RBSP audio(P10_13, I2C_SDA, I2C_SCL, P4_4, P4_5, P4_7, P4_6, 00138 0x80, MovPlayer::BufferLength - 1, 0); 00139 audio.power(0x02); 00140 audio.format(16); 00141 audio.frequency(44100); 00142 MovPlayer::AudioCallback callback(&audio, &TLV320_RBSP::write); 00143 #else 00144 R_BSP_Ssif audio(P2_4, P2_5, P2_7, P2_6, 0x80, MovPlayer::BufferLength - 1, 0); 00145 ssif_channel_cfg_t ssif_cfg; 00146 ssif_cfg.enabled = true; 00147 ssif_cfg.int_level = 0x78; 00148 ssif_cfg.slave_mode = false; 00149 ssif_cfg.sample_freq = 44100u; 00150 ssif_cfg.clk_select = SSIF_CFG_CKS_AUDIO_X1; 00151 ssif_cfg.multi_ch = SSIF_CFG_MULTI_CH_1; 00152 ssif_cfg.data_word = SSIF_CFG_DATA_WORD_16; 00153 ssif_cfg.system_word = SSIF_CFG_SYSTEM_WORD_16; 00154 ssif_cfg.bclk_pol = SSIF_CFG_FALLING; 00155 ssif_cfg.ws_pol = SSIF_CFG_WS_HIGH; 00156 ssif_cfg.padding_pol = SSIF_CFG_PADDING_LOW; 00157 ssif_cfg.serial_alignment = SSIF_CFG_DATA_FIRST; 00158 ssif_cfg.parallel_alignment = SSIF_CFG_LEFT; 00159 ssif_cfg.ws_delay = SSIF_CFG_NO_DELAY; 00160 ssif_cfg.noise_cancel = SSIF_CFG_ENABLE_NOISE_CANCEL; 00161 ssif_cfg.tdm_mode = SSIF_CFG_DISABLE_TDM; 00162 ssif_cfg.romdec_direct.mode = SSIF_CFG_DISABLE_ROMDEC_DIRECT; 00163 ssif_cfg.romdec_direct.p_cbfunc = NULL; 00164 audio.ConfigChannel(&ssif_cfg); 00165 MovPlayer::AudioCallback callback(&audio, &R_BSP_Ssif::write); 00166 #endif 00167 00168 DisplayBase Display; 00169 memset(user_frame_buffer, 0, sizeof(user_frame_buffer)); 00170 dcache_clean(user_frame_buffer, sizeof(user_frame_buffer)); 00171 DisplayBase::rect_t rect; 00172 rect.vs = 0; 00173 rect.vw = LCD_PIXEL_HEIGHT; 00174 rect.hs = 0; 00175 rect.hw = LCD_PIXEL_WIDTH; 00176 Display.Graphics_Read_Setting(DisplayBase::GRAPHICS_LAYER_1, (void *)user_frame_buffer, 00177 FRAME_BUFFER_STRIDE, DisplayBase::GRAPHICS_FORMAT_ARGB4444, 00178 DisplayBase::WR_RD_WRSWA_32_16BIT, &rect); 00179 Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_1); 00180 00181 AsciiFont ascii(user_frame_buffer, LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT, 00182 FRAME_BUFFER_STRIDE, FRAME_BUFFER_BYTE_PER_PIXEL); 00183 00184 TouckKey_LCD_shield touch(P4_0, P2_13, I2C_SDA, I2C_SCL); 00185 touch.SetCallback(touchCallback); 00186 touch.Reset(); 00187 00188 MovFile *mov = MovFile::sharedFile(); 00189 MovPlayer *player = MovPlayer::defaultPlayer(); 00190 00191 memset(filenameBuffer, 0, FileNameBufferNum * FileNameBufferLength); 00192 DIR *directory = opendir("/usb/"); 00193 struct dirent *file = NULL; 00194 int numOfMovies = 0; 00195 while (numOfMovies < FileNameBufferNum && (file = readdir(directory))) { 00196 size_t length = strlen(file->d_name); 00197 if (file->d_name[0] == '.') { 00198 continue; 00199 } 00200 if (memcmp(&file->d_name[length - 4], ".mov", 4)) { 00201 continue; 00202 } 00203 strcpy(filenameBuffer[numOfMovies], "/usb/"); 00204 strcat(filenameBuffer[numOfMovies], file->d_name); 00205 ++numOfMovies; 00206 } 00207 closedir(directory); 00208 00209 int fileIndex = 0; 00210 const graphics_image_t *currentImage = Menu::upx; 00211 lcd->drawImage(Menu::upx); 00212 constexpr uint32_t black = 0x000000f0; 00213 00214 while (1) { 00215 ascii.DrawStr(&filenameBuffer[fileIndex][5], 0, 30, black, 2); 00216 ascii.DrawStr(&filenameBuffer[fileIndex + 1][5], 0, 110, black, 2); 00217 ascii.DrawStr(&filenameBuffer[fileIndex + 2][5], 0, 190, black, 2); 00218 dcache_clean(user_frame_buffer, sizeof(user_frame_buffer)); 00219 #ifdef ONLINE_COMPILER 00220 TouchAction::TouchAction action = readTouch(lcd, &touch, currentImage); 00221 #else 00222 TouchAction action = readTouch(lcd, &touch, currentImage); 00223 #endif 00224 char *path = nullptr; 00225 switch (action) { 00226 case TouchAction::Play0: 00227 path = filenameBuffer[fileIndex]; 00228 break; 00229 00230 case TouchAction::Play1: 00231 path = filenameBuffer[fileIndex + 1]; 00232 break; 00233 00234 case TouchAction::Play2: 00235 path = filenameBuffer[fileIndex + 2]; 00236 break; 00237 00238 case TouchAction::ScrollUp: 00239 fileIndex -= 3; 00240 if (fileIndex <= 0) { 00241 fileIndex = 0; 00242 currentImage = Menu::upx; 00243 } else { 00244 currentImage = Menu::top; 00245 } 00246 lcd->drawImage(currentImage); 00247 ascii.Erase(); 00248 dcache_clean(user_frame_buffer, sizeof(user_frame_buffer)); 00249 break; 00250 00251 case TouchAction::ScrollDown: 00252 fileIndex += 3; 00253 if (fileIndex + 3 >= numOfMovies) { 00254 fileIndex = numOfMovies - 3; 00255 if (fileIndex < 0) { 00256 fileIndex = 0; 00257 } 00258 currentImage = Menu::downx; 00259 } else { 00260 currentImage = Menu::top; 00261 } 00262 lcd->drawImage(currentImage); 00263 ascii.Erase(); 00264 dcache_clean(user_frame_buffer, sizeof(user_frame_buffer)); 00265 break; 00266 00267 default: 00268 break; 00269 } 00270 if (path && *path) { 00271 ascii.Erase(); 00272 dcache_clean(user_frame_buffer, sizeof(user_frame_buffer)); 00273 FILE *file = fopen(path, "r"); 00274 printf("start playing %s\n", path); 00275 mov->start(file); 00276 player->play(mov, lcd, callback); 00277 fclose(file); 00278 printf("finished playing\n"); 00279 } else { 00280 continue; 00281 } 00282 touch.Reset(); 00283 while (touchSemaphore.wait(0) > 0) ; 00284 readTouch(nullptr, &touch, nullptr); 00285 lcd->drawImage(currentImage); 00286 } 00287 00288 return 0; 00289 } 00290
Generated on Fri Jul 15 2022 12:17:04 by
1.7.2