virtual_camera.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /******************************************************************************
  2. *
  3. * Freescale Semiconductor Inc.
  4. * (c) Copyright 2004-2010 Freescale Semiconductor, Inc.
  5. * ALL RIGHTS RESERVED.
  6. *
  7. **************************************************************************//*!
  8. *
  9. * @file virtual_camera.c
  10. *
  11. * @author
  12. *
  13. * @version
  14. *
  15. * @date Jul-20-2010
  16. *
  17. * @brief The file emulates a virtual camerea
  18. *****************************************************************************/
  19. /******************************************************************************
  20. * Includes
  21. *****************************************************************************/
  22. #include "hidef.h" /* for EnableInterrupts macro */
  23. #include "derivative.h" /* include peripheral declarations */
  24. #include "types.h" /* Contains User Defined Data Types */
  25. #include "usb_video.h" /* USB Video Class Header File */
  26. #include "virtual_camera.h" /* Virtual camera Application Header File */
  27. #include "sci.h" /* SCI Header File */
  28. /* skip the inclusion in dependency statge */
  29. #ifndef __NO_SETJMP
  30. #include <stdio.h>
  31. #endif
  32. #include <stdlib.h>
  33. #include <string.h>
  34. /*****************************************************************************
  35. * Constant and Macro's - None
  36. *****************************************************************************/
  37. /*****************************************************************************
  38. * Global Functions Prototypes
  39. *****************************************************************************/
  40. void TestApp_Init(void);
  41. void TestApp_Task(void);
  42. void USB_Prepare_Data(void);
  43. /****************************************************************************
  44. * Global Variables
  45. ****************************************************************************/
  46. #if (defined _MC9S08MM128_H)
  47. #pragma CONST_SEG WAV
  48. #endif
  49. extern const unsigned char video_data[];
  50. extern const uint_32 video_size;
  51. #if (defined _MC9S08MM128_H)
  52. #pragma CONST_SEG DEFAULT
  53. #endif
  54. /*****************************************************************************
  55. * Local Types - None
  56. *****************************************************************************/
  57. /*****************************************************************************
  58. * Local Functions Prototypes
  59. *****************************************************************************/
  60. static void USB_App_Callback(uint_8 controller_ID,
  61. uint_8 event_type, void* val);
  62. /*****************************************************************************
  63. * Local Variables
  64. *****************************************************************************/
  65. #ifdef _MC9S08JS16_H
  66. #pragma DATA_SEG APP_DATA
  67. #endif
  68. /* Virtual camera start Init Flag */
  69. static volatile boolean start_app = FALSE;
  70. static boolean start_send = TRUE;
  71. static uint_8 wav_buff[VIDEO_PACKET_SIZE];
  72. static uint_32 video_position = 0;
  73. static uint_8 odd_img=0;
  74. static uint_32 frame_size;
  75. /*****************************************************************************
  76. * Local Functions
  77. *****************************************************************************/
  78. /******************************************************************************
  79. *
  80. * @name USB_Prepare_Data
  81. *
  82. * @brief This function prepares data to send
  83. *
  84. * @param None
  85. *
  86. * @return None
  87. *****************************************************************************
  88. * This function prepare data before sending
  89. *****************************************************************************/
  90. void USB_Prepare_Data(void)
  91. {
  92. uint_16 k;
  93. uint_8 j;
  94. wav_buff[0]=0x0C;
  95. wav_buff[1]=odd_img;
  96. for(j=2;j<HEADER_PACKET_SIZE;j++)
  97. {
  98. wav_buff[j]=0x00;
  99. }
  100. /* copy data to buffer */
  101. for(k=HEADER_PACKET_SIZE;k<VIDEO_PACKET_SIZE;k++,video_position++)
  102. {
  103. if((0xff==video_data[video_position-1])&&(0xd9==video_data[video_position]))
  104. {
  105. wav_buff[1]|=0x02;
  106. odd_img=(uint_8)(1-odd_img);
  107. video_position++;
  108. break;
  109. }
  110. else
  111. {
  112. wav_buff[k] = video_data[video_position];
  113. }
  114. }
  115. }
  116. /******************************************************************************
  117. *
  118. * @name TestApp_Init
  119. *
  120. * @brief This function is the entry for the Virtual camera application
  121. *
  122. * @param None
  123. *
  124. * @return None
  125. *****************************************************************************
  126. * This function starts the virtual camera application
  127. *****************************************************************************/
  128. void TestApp_Init(void)
  129. {
  130. uint_8 error;
  131. DisableInterrupts;
  132. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  133. usb_int_dis();
  134. #endif
  135. //sci_init();
  136. /* Initialize the USB interface */
  137. error = USB_Class_Video_Init(CONTROLLER_ID,USB_App_Callback,
  138. NULL,NULL);
  139. #if (defined _MCF51MM256_H) || (defined _MCF51JE256_H)
  140. usb_int_en();
  141. #endif
  142. EnableInterrupts;
  143. }
  144. /******************************************************************************
  145. *
  146. * @name USB_App_Callback
  147. *
  148. * @brief This function handles Class callback
  149. *
  150. * @param controller_ID : Controller ID
  151. * @param event_type : Value of the event
  152. * @param val : gives the configuration value
  153. *
  154. * @return None
  155. *
  156. *****************************************************************************
  157. * This function is called from the class layer whenever reset occurs or enum
  158. * is completed. After the enum is completed this function sets a variable so
  159. * that the application can start.
  160. * This function also receives DATA Send and RECEIVED Events
  161. *****************************************************************************/
  162. static void USB_App_Callback (
  163. uint_8 controller_ID, /* [IN] Controller ID */
  164. uint_8 event_type, /* [IN] value of the event */
  165. void* val /* [IN] gives the configuration value */
  166. )
  167. {
  168. UNUSED (controller_ID)
  169. UNUSED (val)
  170. if ((event_type == USB_APP_BUS_RESET) || (event_type == USB_APP_CONFIG_CHANGED))
  171. {
  172. start_app=FALSE;
  173. }
  174. else if(event_type == USB_APP_ENUM_COMPLETE)
  175. {
  176. start_app=TRUE;
  177. if (start_send==FALSE){
  178. USB_Prepare_Data();
  179. (void)USB_Class_Video_Send_Data(controller_ID, VIDEO_ENDPOINT, wav_buff,VIDEO_PACKET_SIZE) ;
  180. };
  181. //(void)printf("Virtual camera is working ...\r\n");
  182. }
  183. else if((event_type == USB_APP_SEND_COMPLETE) && (TRUE == start_app))
  184. {
  185. if(video_position > video_size)
  186. {
  187. video_position = 0;
  188. }
  189. USB_Prepare_Data();
  190. (void)USB_Class_Video_Send_Data(controller_ID,VIDEO_ENDPOINT,wav_buff,VIDEO_PACKET_SIZE);
  191. }
  192. return;
  193. }
  194. /******************************************************************************
  195. *
  196. * @name TestApp_Task
  197. *
  198. * @brief This function use to send data
  199. *
  200. * @param None
  201. *
  202. * @return None
  203. *
  204. *****************************************************************************
  205. *
  206. *****************************************************************************/
  207. void TestApp_Task(void)
  208. {
  209. /*check whether enumeration is complete or not */
  210. if((start_app==TRUE) && (start_send==TRUE))
  211. {
  212. start_send = FALSE;
  213. USB_Prepare_Data();
  214. (void)USB_Class_Video_Send_Data(CONTROLLER_ID,VIDEO_ENDPOINT,wav_buff,VIDEO_PACKET_SIZE) ;
  215. }
  216. }
  217. /* EOF */