testing n-Bed with a Logitech C270 camera

Dependencies:   USBHost mbed

Fork of USBHostC270_example by Norimasa Okamoto

Committer:
chalikias
Date:
Thu May 21 11:59:47 2015 +0000
Revision:
14:449f809d07eb
Parent:
11:6a8eef89eb22
camera resolution reduced to 160x120, else memory overflow issues...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 9:fecabade834a 1 #include "USBHostC270.h"
va009039 9:fecabade834a 2 #include "dbg.h"
va009039 9:fecabade834a 3
va009039 10:387c49b2fc7e 4 //#define C270_DEBUG 1
va009039 10:387c49b2fc7e 5 #ifdef C270_DEBUG
va009039 10:387c49b2fc7e 6 #define C270_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
va009039 10:387c49b2fc7e 7 #else
va009039 10:387c49b2fc7e 8 #define C270_DBG(...) while(0);
va009039 10:387c49b2fc7e 9 #endif
va009039 10:387c49b2fc7e 10
va009039 9:fecabade834a 11 // ------------------ HcControl Register ---------------------
va009039 9:fecabade834a 12 #define OR_CONTROL_IE 0x00000008
va009039 9:fecabade834a 13
va009039 9:fecabade834a 14 USBHostC270::USBHostC270(int formatIndex, int frameIndex, uint32_t interval)
va009039 9:fecabade834a 15 {
va009039 10:387c49b2fc7e 16 C270_DBG("formatIndex: %d, frameIndex: %d, interval: %d", formatIndex, frameIndex, interval);
va009039 9:fecabade834a 17 _formatIndex = formatIndex;
va009039 9:fecabade834a 18 _frameIndex = frameIndex;
va009039 9:fecabade834a 19 _interval = interval;
va009039 10:387c49b2fc7e 20 clearOnResult();
va009039 9:fecabade834a 21 host = USBHost::getHostInst();
va009039 11:6a8eef89eb22 22 m_isoEp = new IsochronousEp;
va009039 9:fecabade834a 23 init();
va009039 9:fecabade834a 24 }
va009039 9:fecabade834a 25
va009039 9:fecabade834a 26 void USBHostC270::init()
va009039 9:fecabade834a 27 {
va009039 9:fecabade834a 28 C270_DBG("");
va009039 9:fecabade834a 29 dev_connected = false;
va009039 9:fecabade834a 30 dev = NULL;
va009039 9:fecabade834a 31 c270_intf = -1;
va009039 9:fecabade834a 32 c270_device_found = false;
va009039 9:fecabade834a 33 c270_vid_pid_found = false;
va009039 9:fecabade834a 34 }
va009039 9:fecabade834a 35
va009039 9:fecabade834a 36 bool USBHostC270::connected()
va009039 9:fecabade834a 37 {
va009039 9:fecabade834a 38 return dev_connected;
va009039 9:fecabade834a 39 }
va009039 9:fecabade834a 40
va009039 9:fecabade834a 41 bool USBHostC270::connect()
va009039 9:fecabade834a 42 {
va009039 9:fecabade834a 43 if (dev_connected) {
va009039 9:fecabade834a 44 return true;
va009039 9:fecabade834a 45 }
va009039 9:fecabade834a 46
va009039 9:fecabade834a 47 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
va009039 9:fecabade834a 48 if ((dev = host->getDevice(i)) != NULL) {
va009039 9:fecabade834a 49
va009039 9:fecabade834a 50 C270_DBG("Trying to connect C270 device\r\n");
va009039 9:fecabade834a 51
va009039 11:6a8eef89eb22 52 if(host->enumerate(dev, this)) {
va009039 9:fecabade834a 53 break;
va009039 11:6a8eef89eb22 54 }
va009039 9:fecabade834a 55 if (c270_device_found) {
va009039 9:fecabade834a 56 USB_INFO("New C270 device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, c270_intf);
va009039 9:fecabade834a 57 dev->setName("C270", c270_intf);
va009039 10:387c49b2fc7e 58 host->registerDriver(dev, c270_intf, this, &USBHostC270::onDisconnect);
va009039 9:fecabade834a 59 int addr = dev->getAddress();
va009039 11:6a8eef89eb22 60 m_isoEp->init(addr, C270_EN, C270_MPS);
va009039 9:fecabade834a 61 uint8_t buf[26];
va009039 9:fecabade834a 62 memset(buf, 0, sizeof(buf));
va009039 9:fecabade834a 63 buf[2] = _formatIndex;
va009039 9:fecabade834a 64 buf[3] = _frameIndex;
va009039 9:fecabade834a 65 *reinterpret_cast<uint32_t*>(buf+4) = _interval;
va009039 9:fecabade834a 66 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf));
va009039 9:fecabade834a 67 if (res != USB_TYPE_OK) {
va009039 9:fecabade834a 68 C270_DBG("SET_CUR VS_COMMIT_CONTROL FAILED");
va009039 9:fecabade834a 69 }
va009039 9:fecabade834a 70 res = setInterfaceAlternate(1, C270_IF_ALT); // alt=1 packet size = 192
va009039 9:fecabade834a 71 if (res != USB_TYPE_OK) {
va009039 9:fecabade834a 72 C270_DBG("SET_INTERFACE FAILED");
va009039 9:fecabade834a 73 }
va009039 9:fecabade834a 74 for(int i = 0; i < 16; i++) {
va009039 9:fecabade834a 75 report_cc_count[i] = 0;
va009039 9:fecabade834a 76 report_ps_cc_count[i] = 0;
va009039 9:fecabade834a 77 }
va009039 9:fecabade834a 78 LPC_USB->HcControl |= OR_CONTROL_PLE; // PeriodicListEnable
va009039 9:fecabade834a 79 LPC_USB->HcControl |= OR_CONTROL_IE; // IsochronousEnable
va009039 9:fecabade834a 80
va009039 9:fecabade834a 81 dev_connected = true;
va009039 9:fecabade834a 82 return true;
va009039 9:fecabade834a 83 }
va009039 9:fecabade834a 84 }
va009039 9:fecabade834a 85 }
va009039 9:fecabade834a 86 init();
va009039 9:fecabade834a 87 return false;
va009039 9:fecabade834a 88 }
va009039 9:fecabade834a 89
va009039 10:387c49b2fc7e 90 void USBHostC270::onDisconnect()
va009039 10:387c49b2fc7e 91 {
va009039 10:387c49b2fc7e 92 C270_DBG("dev_connected: %d", dev_connected);
va009039 11:6a8eef89eb22 93 if (dev_connected) {
va009039 11:6a8eef89eb22 94 m_isoEp->disconnect();
va009039 11:6a8eef89eb22 95 init();
va009039 10:387c49b2fc7e 96 }
va009039 10:387c49b2fc7e 97 }
va009039 10:387c49b2fc7e 98
va009039 9:fecabade834a 99 /*virtual*/ void USBHostC270::setVidPid(uint16_t vid, uint16_t pid)
va009039 9:fecabade834a 100 {
va009039 9:fecabade834a 101 C270_DBG("vid:%04x,pid:%04x", vid, pid);
va009039 9:fecabade834a 102 if (vid == C270_VID && pid == C270_PID) {
va009039 9:fecabade834a 103 c270_vid_pid_found = true;
va009039 9:fecabade834a 104 } else {
va009039 9:fecabade834a 105 c270_vid_pid_found = false;
va009039 9:fecabade834a 106 }
va009039 9:fecabade834a 107 }
va009039 9:fecabade834a 108
va009039 9:fecabade834a 109 /*virtual*/ bool USBHostC270::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
va009039 9:fecabade834a 110 {
va009039 9:fecabade834a 111 C270_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
va009039 9:fecabade834a 112 if ((c270_intf == -1) && c270_vid_pid_found) {
va009039 9:fecabade834a 113 c270_intf = intf_nb;
va009039 10:387c49b2fc7e 114 c270_vid_pid_found = false;
va009039 9:fecabade834a 115 c270_device_found = true;
va009039 9:fecabade834a 116 return true;
va009039 9:fecabade834a 117 }
va009039 9:fecabade834a 118 return false;
va009039 9:fecabade834a 119 }
va009039 9:fecabade834a 120
va009039 9:fecabade834a 121 /*virtual*/ bool USBHostC270::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
va009039 9:fecabade834a 122 {
va009039 9:fecabade834a 123 C270_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir);
va009039 9:fecabade834a 124 return false;
va009039 9:fecabade834a 125 }
va009039 9:fecabade834a 126
va009039 10:387c49b2fc7e 127 #define SEQ_READ_IDOL 0
va009039 10:387c49b2fc7e 128 #define SEQ_READ_EXEC 1
va009039 10:387c49b2fc7e 129 #define SEQ_READ_DONE 2
va009039 10:387c49b2fc7e 130
va009039 10:387c49b2fc7e 131 int USBHostC270::readJPEG(uint8_t* buf, int size, int timeout_ms) {
va009039 10:387c49b2fc7e 132 _buf = buf;
va009039 10:387c49b2fc7e 133 _pos = 0;
va009039 10:387c49b2fc7e 134 _size = size;
va009039 10:387c49b2fc7e 135 _seq = SEQ_READ_IDOL;
va009039 10:387c49b2fc7e 136 setOnResult(this, &USBHostC270::callback_motion_jpeg);
va009039 10:387c49b2fc7e 137 Timer timeout_t;
va009039 10:387c49b2fc7e 138 timeout_t.reset();
va009039 10:387c49b2fc7e 139 timeout_t.start();
va009039 10:387c49b2fc7e 140 while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE) {
va009039 10:387c49b2fc7e 141 poll(timeout_ms);
va009039 10:387c49b2fc7e 142 }
va009039 10:387c49b2fc7e 143 return _pos;
va009039 9:fecabade834a 144 }
va009039 9:fecabade834a 145
va009039 10:387c49b2fc7e 146 /* virtual */ void USBHostC270::outputJPEG(uint8_t c, int status) { // from decodeMJPEG
va009039 10:387c49b2fc7e 147 if (_seq == SEQ_READ_IDOL) {
va009039 10:387c49b2fc7e 148 if (status == JPEG_START) {
va009039 10:387c49b2fc7e 149 _pos = 0;
va009039 10:387c49b2fc7e 150 _seq = SEQ_READ_EXEC;
va009039 10:387c49b2fc7e 151 }
va009039 9:fecabade834a 152 }
va009039 10:387c49b2fc7e 153 if (_seq == SEQ_READ_EXEC) {
va009039 10:387c49b2fc7e 154 if (_pos < _size) {
va009039 10:387c49b2fc7e 155 _buf[_pos++] = c;
va009039 10:387c49b2fc7e 156 }
va009039 10:387c49b2fc7e 157 if (status == JPEG_END) {
va009039 10:387c49b2fc7e 158 _seq = SEQ_READ_DONE;
va009039 10:387c49b2fc7e 159 }
va009039 10:387c49b2fc7e 160 }
va009039 9:fecabade834a 161 }
va009039 9:fecabade834a 162
va009039 10:387c49b2fc7e 163 void USBHostC270::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
va009039 10:387c49b2fc7e 164 inputPacket(buf, len);
va009039 10:387c49b2fc7e 165 }