BLE demo for mbed Ported RunningElectronics's SBDBT firmware for BLE. It can communicate with iOS

Dependencies:   FatFileSystem mbed

Fork of BTstack by Norimasa Okamoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers uvcini.cpp Source File

uvcini.cpp

00001 #include "mbed.h"
00002 #include "uvc.h"
00003 #define __DEBUG
00004 #include "mydbg.h"
00005 #include "stcamcfg.h"
00006 
00007 const struct stcamcfg stcamcfg_table[] = {
00008 /*
00009 {0x045e, 0x074a,
00010 160,120,
00011 PAYLOAD_MJPEG,
00012 0x81, 128,
00013 1, 5, 2000000, // 160x120 5.0fps
00014 1, 1, "Microsoft LifeCam VX-700",
00015 8, 3,
00016 },*/
00017 /*
00018 {0x0c45, 0x62c0,
00019 160,120,
00020 PAYLOAD_MJPEG,
00021 0x81, 128,
00022 1, 5, 2000000, // 160x120 5.0fps
00023 1, 1, "UVCA130AF",
00024 8, 3,
00025 },*/
00026 
00027 {0x046d, 0x0994,
00028 160,120,
00029 PAYLOAD_MJPEG,
00030 0, 0,
00031 0, 0, 2000000, // 160x120 10.0fps
00032 0, 0, "Logitech QuickCam Orbit AF",
00033 0, 3,
00034 },
00035 {0x0000, 0x0000,
00036 160,120,
00037 PAYLOAD_MJPEG,
00038 0x00, 0,
00039 0, 0, 2000000,
00040 0, 0, "default",
00041 0, 3,
00042 },
00043 };
00044 
00045 inline void LE32(uint32_t n, uint8_t* d)
00046 {
00047     d[0] = (uint8_t)n;
00048     d[1] = (uint8_t)(n >> 8);
00049     d[2] = (uint8_t)(n >> 16);
00050     d[3] = (uint8_t)(n >> 24);
00051 }
00052 
00053 void uvc::SetFormatIndex(int index)
00054 {
00055     DBG_ASSERT(index >= 1);
00056     DBG_ASSERT(index <= 2);
00057     m_FormatIndex = index;
00058 }
00059 
00060 void uvc::SetFrameIndex(int index)
00061 {
00062     DBG_ASSERT(index >= 1);
00063     DBG_ASSERT(index <= 8);
00064     m_FrameIndex = index;
00065 }
00066 
00067 void uvc::SetFrameInterval(int val)
00068 {
00069     DBG_ASSERT(val >= 333333);
00070     DBG_ASSERT(val <= 10000000);
00071     m_FrameInterval = val;
00072 }
00073 
00074 void uvc::SetPacketSize(int size)
00075 {
00076     DBG_ASSERT(size >= 128);
00077     DBG_ASSERT(size <= 956);
00078     m_PacketSize = size;
00079 }
00080 
00081 void uvc::SetImageSize(int width, int height)
00082 {
00083     DBG_ASSERT(width >= 160);
00084     DBG_ASSERT(width <= 800);
00085     DBG_ASSERT(height >= 120);
00086     DBG_ASSERT(height <= 600);
00087     m_width = width;
00088     m_height = height;
00089 }
00090 
00091 void uvc::SetPayload(int payload)
00092 {
00093     DBG_ASSERT(payload == PAYLOAD_MJPEG || payload == PAYLOAD_YUY2);
00094     m_payload = payload;
00095 }
00096 
00097 void uvc::poll()
00098 {
00099     isochronous();
00100 }
00101 
00102 int uvc::_init()
00103 {
00104     m_init = true;
00105     UsbErr rc;
00106     for(int i = 0; i < 2; i++) {
00107         m_pDev = m_pHost->getDeviceByClass(CLASS_VIDEO, m_cam); // UVC
00108         if (m_pDev || i > 0) {
00109             break;
00110         }
00111         rc = Usb_poll();
00112         if (rc == USBERR_PROCESSING) {
00113             VERBOSE("%p USBERR_PROCESSING\n", this);
00114             return -1;
00115         }
00116     }
00117     DBG("m_pDev=%p\n", m_pDev);
00118     if (!m_pDev) {
00119         VERBOSE("%p UVC CAMERA(%d) NOT FOUND\n", this, m_cam);
00120         return -1;
00121     }
00122     DBG_ASSERT(m_pDev);
00123     
00124     struct stcamcfg cfg;
00125     for(int i = 0; ; i++) {
00126         cfg = stcamcfg_table[i];
00127         if (cfg.idVender == 0x0000) {
00128             DBG("not cam config\n");
00129             DBG("vid: %04X\n", m_pDev->getVid());
00130             DBG("pid: %04X\n", m_pDev->getPid());
00131             break;
00132         }
00133         if (cfg.idVender == m_pDev->getVid() && cfg.idProduct ==  m_pDev->getPid()) {
00134             DBG_ASSERT(cfg.name);
00135             DBG("found %s\n", cfg.name);
00136             break;
00137         }
00138     }
00139 
00140     if (m_width) {
00141         cfg.width = m_width;
00142     }
00143     if (m_height) {
00144         cfg.height = m_height;
00145     }
00146     if (m_payload != PAYLOAD_UNDEF) {
00147         cfg.payload = m_payload;
00148     }
00149     if (m_FormatIndex) {
00150         cfg.FormatIndex = m_FormatIndex;
00151     }
00152     if (m_FrameIndex) {
00153         cfg.FrameIndex = m_FrameIndex;
00154     }
00155     if (m_FrameInterval) {
00156         cfg.dwFrameInterval = m_FrameInterval;
00157     }
00158     if (m_PacketSize) {
00159         cfg.wMaxPacketSize = m_PacketSize;
00160     }
00161 
00162     _config(&cfg);
00163 
00164     if (cfg.payload == PAYLOAD_YUY2) {
00165         if (cfg.FormatIndex == 0) {
00166             VERBOSE("YUY2 FORMAT NOT FOUND\n");
00167             return -1;
00168         }
00169     }
00170     
00171     if (cfg.iso_FrameCount == 0) {
00172         int c = usb_bp_size() / cfg.wMaxPacketSize;
00173         if (c > 8) {
00174             c = 8;
00175         }
00176         cfg.iso_FrameCount = c; 
00177     }
00178     DBG_ASSERT(cfg.iso_FrameCount >= 1);
00179     DBG_ASSERT(cfg.iso_FrameCount <= 8);
00180     DBG_ASSERT((cfg.iso_FrameCount * cfg.wMaxPacketSize) <= usb_bp_size());
00181     if (cfg.iso_itdCount == 0) {
00182         cfg.iso_itdCount = 3;
00183     }
00184     DBG_ASSERT(cfg.iso_itdCount >= 1);
00185     DBG("cfg.wMaxPacketSize=%d\n", cfg.wMaxPacketSize);
00186     DBG("cfg.iso_FrameCount=%d\n", cfg.iso_FrameCount);
00187     DBG("cfg.iso_itdCount=%d\n", cfg.iso_itdCount);
00188     DBG_ASSERT(cfg.iso_FrameCount >= 1 && cfg.iso_FrameCount <= 8);
00189     //m_pEpIntIn = new UsbEndpoint(m_pDev, 0x83, true, USB_INT, 16);
00190     //DBG_ASSERT(m_pEpIntIn);
00191     
00192     DBG_ASSERT(cfg.bEndpointAddress == 0x81);
00193     DBG_ASSERT(cfg.wMaxPacketSize >= 128);
00194     DBG_ASSERT(cfg.wMaxPacketSize <= 956);
00195     m_PacketSize = cfg.wMaxPacketSize;
00196     DBG_ASSERT(m_PacketSize); 
00197     m_pEpIsoIn = new UsbEndpoint(m_pDev, cfg.bEndpointAddress, true, USB_ISO, m_PacketSize);
00198     DBG_ASSERT(m_pEpIsoIn);
00199 
00200     DBG_ASSERT(cfg.FormatIndex >= 1);
00201     DBG_ASSERT(cfg.FormatIndex <= 2);
00202     DBG_ASSERT(cfg.FrameIndex >= 1);
00203     DBG_ASSERT(cfg.FrameIndex <= 8);
00204     DBG_ASSERT(cfg.dwFrameInterval <= 10000000);
00205     DBG_ASSERT(cfg.dwFrameInterval >= 333333);
00206 
00207     uint8_t temp1[1];
00208     temp1[0] = 0x00;
00209     rc = Control(GET_INFO, VS_PROBE_CONTROL, 1, temp1, sizeof(temp1));
00210     DBG_ASSERT(rc == USBERR_OK);
00211     DBG_BYTES("GET_INFO Probe ", temp1, sizeof(temp1));
00212 
00213     uint8_t temp[34];
00214     rc = Control(GET_CUR, VS_PROBE_CONTROL, 1, temp, sizeof(temp));
00215     DBG_ASSERT(rc == USBERR_OK);
00216     DBG_BYTES("GET_CUR Probe ", temp, sizeof(temp));
00217 
00218     uint8_t param[34];
00219     memset(param, 0x00, sizeof(param));
00220     param[0] = 0x00;
00221     param[2] = cfg.FormatIndex;
00222     param[3] = cfg.FrameIndex; // 160x120
00223     LE32(cfg.dwFrameInterval, param+4); // Frame Interval
00224 
00225     DBG_BYTES("SET_CUR Probe ", param, sizeof(param));
00226     rc = Control(SET_CUR, VS_PROBE_CONTROL, 1, param, sizeof(param));
00227     DBG_ASSERT(rc == USBERR_OK);
00228 
00229     rc = Control(GET_CUR, VS_PROBE_CONTROL, 1, temp, sizeof(temp));
00230     DBG_ASSERT(rc == USBERR_OK);
00231     DBG_BYTES("GET_CUR Probe ", temp, sizeof(temp));
00232 
00233     rc = Control(GET_CUR, VS_COMMIT_CONTROL, 1, temp, sizeof(temp));
00234     DBG_ASSERT(rc == USBERR_OK);
00235     DBG_BYTES("GET_CUR Commit", temp, sizeof(temp));
00236 
00237     DBG_BYTES("SET_CUR Commit", param, sizeof(param));
00238     rc = Control(SET_CUR, VS_COMMIT_CONTROL, 1, param, sizeof(param));
00239     DBG_ASSERT(rc == USBERR_OK);
00240 
00241     //USBH_SET_INTERFACE(1, 1); // alt=1 size=128
00242     DBG_ASSERT(cfg.bInterface >= 1); 
00243     DBG_ASSERT(cfg.bAlternate >= 1);
00244     DBG_ASSERT(cfg.bAlternate <= 6);
00245     //rc = m_pDev->controlSend(
00246     //              USB_HOST_TO_DEVICE | USB_RECIPIENT_INTERFACE, 
00247     //              SET_INTERFACE, cfg.bAlternate, cfg.bInterface, NULL, 0);
00248     rc = m_pDev->SetInterfaceAlternate(cfg.bInterface, cfg.bAlternate);
00249     DBG_ASSERT(rc == USBERR_OK);
00250 
00251     DBG_ASSERT(cfg.iso_FrameCount >= 1);
00252     DBG_ASSERT(cfg.iso_FrameCount <= 8);
00253     m_FrameCount = cfg.iso_FrameCount;
00254     DBG_ASSERT(cfg.iso_itdCount >= 1);
00255     DBG_ASSERT(cfg.iso_itdCount <= 8);
00256     m_itdCount = cfg.iso_itdCount;
00257     
00258     LPC_USB->HcControl |= OR_CONTROL_PLE; // PeriodicListEnable
00259     LPC_USB->HcControl |= OR_CONTROL_IE;  // IsochronousEnable
00260 
00261     m_connect = true;
00262     return 0;
00263 }