Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z by Axeda Corp

Committer:
AxedaCorp
Date:
Wed Jul 02 19:57:37 2014 +0000
Revision:
2:2f9019c5a9fc
Parent:
0:65004368569c
ip switch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:65004368569c 1 // USBHostCam.cpp
AxedaCorp 0:65004368569c 2 #include "USBHostCam.h"
AxedaCorp 0:65004368569c 3
AxedaCorp 0:65004368569c 4 #if 0
AxedaCorp 0:65004368569c 5 #define CAM_DBG(x, ...) std::printf("[%s:%d]"x"\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
AxedaCorp 0:65004368569c 6 #else
AxedaCorp 0:65004368569c 7 #define CAM_DBG(...) while(0);
AxedaCorp 0:65004368569c 8 #endif
AxedaCorp 0:65004368569c 9 #define CAM_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0);
AxedaCorp 0:65004368569c 10 #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);}while(0);
AxedaCorp 0:65004368569c 11
AxedaCorp 0:65004368569c 12 CamInfo* getCamInfoList(); // CamInfo.cpp
AxedaCorp 0:65004368569c 13
AxedaCorp 0:65004368569c 14 USBHostCam::USBHostCam(uint8_t size, uint8_t option, CamInfo* user_caminfo)
AxedaCorp 0:65004368569c 15 {
AxedaCorp 0:65004368569c 16 CAM_DBG("size: %d, option: %d", size, option);
AxedaCorp 0:65004368569c 17 _caminfo_size = size;
AxedaCorp 0:65004368569c 18 _caminfo_option = option;
AxedaCorp 0:65004368569c 19 if (user_caminfo) {
AxedaCorp 0:65004368569c 20 CamInfoList = user_caminfo;
AxedaCorp 0:65004368569c 21 } else {
AxedaCorp 0:65004368569c 22 CamInfoList = getCamInfoList();
AxedaCorp 0:65004368569c 23 }
AxedaCorp 0:65004368569c 24 clearOnResult();
AxedaCorp 0:65004368569c 25 host = USBHost::getHostInst();
AxedaCorp 0:65004368569c 26 dev = host->getDevice(0);
AxedaCorp 0:65004368569c 27 ep_iso_in = new USBEndpoint;
AxedaCorp 0:65004368569c 28 init();
AxedaCorp 0:65004368569c 29 }
AxedaCorp 0:65004368569c 30
AxedaCorp 0:65004368569c 31 void USBHostCam::init()
AxedaCorp 0:65004368569c 32 {
AxedaCorp 0:65004368569c 33 CAM_DBG("");
AxedaCorp 0:65004368569c 34 dev_connected = false;
AxedaCorp 0:65004368569c 35 dev = NULL;
AxedaCorp 0:65004368569c 36 cam_intf = -1;
AxedaCorp 0:65004368569c 37 device_found = false;
AxedaCorp 0:65004368569c 38 caminfo_found = false;
AxedaCorp 0:65004368569c 39 }
AxedaCorp 0:65004368569c 40
AxedaCorp 0:65004368569c 41 bool USBHostCam::connected()
AxedaCorp 0:65004368569c 42 {
AxedaCorp 0:65004368569c 43 return dev_connected;
AxedaCorp 0:65004368569c 44 }
AxedaCorp 0:65004368569c 45
AxedaCorp 0:65004368569c 46 bool USBHostCam::connect()
AxedaCorp 0:65004368569c 47 {
AxedaCorp 0:65004368569c 48 if (dev_connected) {
AxedaCorp 0:65004368569c 49 return true;
AxedaCorp 0:65004368569c 50 }
AxedaCorp 0:65004368569c 51
AxedaCorp 0:65004368569c 52 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
AxedaCorp 0:65004368569c 53 if ((dev = host->getDevice(i)) != NULL) {
AxedaCorp 0:65004368569c 54
AxedaCorp 0:65004368569c 55 CAM_DBG("Trying to connect Cam device\r\n");
AxedaCorp 0:65004368569c 56
AxedaCorp 0:65004368569c 57 if(host->enumerate(dev, this)) {
AxedaCorp 0:65004368569c 58 break;
AxedaCorp 0:65004368569c 59 }
AxedaCorp 0:65004368569c 60 if (device_found) {
AxedaCorp 0:65004368569c 61 USB_INFO("New Cam: %s device: VID:%04x PID:%04x [dev: %p - intf: %d]\n", caminfo->name, dev->getVid(), dev->getPid(), dev, cam_intf);
AxedaCorp 0:65004368569c 62 dev->setName(caminfo->name, cam_intf);
AxedaCorp 0:65004368569c 63 //host->registerDriver(dev, cam_intf, this, &USBHostCam::onDisconnect);
AxedaCorp 0:65004368569c 64 int addr = dev->getAddress();
AxedaCorp 0:65004368569c 65 ep_iso_in->setDevice(dev);
AxedaCorp 0:65004368569c 66 ep_iso_in->setAddress(caminfo->en);
AxedaCorp 0:65004368569c 67 ep_iso_in->setSize(caminfo->mps);
AxedaCorp 0:65004368569c 68 //ep_iso_in->init(addr, caminfo->en, caminfo->mps, caminfo->frameCount, caminfo->queueLimit);
AxedaCorp 0:65004368569c 69 uint8_t buf[26];
AxedaCorp 0:65004368569c 70 memset(buf, 0, sizeof(buf));
AxedaCorp 0:65004368569c 71 buf[2] = caminfo->formatIndex;
AxedaCorp 0:65004368569c 72 buf[3] = caminfo->frameIndex;
AxedaCorp 0:65004368569c 73 *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval;
AxedaCorp 0:65004368569c 74 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf));
AxedaCorp 0:65004368569c 75 if (res != USB_TYPE_OK) {
AxedaCorp 0:65004368569c 76 CAM_DBG("SET_CUR VS_COMMIT_CONTROL FAILED");
AxedaCorp 0:65004368569c 77 }
AxedaCorp 0:65004368569c 78 res = setInterfaceAlternate(1, caminfo->if_alt);
AxedaCorp 0:65004368569c 79 if (res != USB_TYPE_OK) {
AxedaCorp 0:65004368569c 80 CAM_DBG("SET_INTERFACE FAILED");
AxedaCorp 0:65004368569c 81 }
AxedaCorp 0:65004368569c 82 dev_connected = true;
AxedaCorp 0:65004368569c 83 return true;
AxedaCorp 0:65004368569c 84 }
AxedaCorp 0:65004368569c 85 }
AxedaCorp 0:65004368569c 86 }
AxedaCorp 0:65004368569c 87 init();
AxedaCorp 0:65004368569c 88 return false;
AxedaCorp 0:65004368569c 89 }
AxedaCorp 0:65004368569c 90
AxedaCorp 0:65004368569c 91 #if 0
AxedaCorp 0:65004368569c 92 void USBHostCam::setup() {
AxedaCorp 0:65004368569c 93 caminfo = CamInfoList;
AxedaCorp 0:65004368569c 94 bool found = false;
AxedaCorp 0:65004368569c 95 while(caminfo->vid != 0) {
AxedaCorp 0:65004368569c 96 if (caminfo->vid == host->getDevice(0)->getVid() &&
AxedaCorp 0:65004368569c 97 caminfo->pid == host->getDevice(0)->getPid() &&
AxedaCorp 0:65004368569c 98 caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) {
AxedaCorp 0:65004368569c 99 found = true;
AxedaCorp 0:65004368569c 100 break;
AxedaCorp 0:65004368569c 101 }
AxedaCorp 0:65004368569c 102 caminfo++;
AxedaCorp 0:65004368569c 103 }
AxedaCorp 0:65004368569c 104 if (!found) {
AxedaCorp 0:65004368569c 105 CAM_INFO("caminfo not found.\n");
AxedaCorp 0:65004368569c 106 exit(1);
AxedaCorp 0:65004368569c 107 }
AxedaCorp 0:65004368569c 108 CAM_INFO("Found: %s\n", caminfo->name);
AxedaCorp 0:65004368569c 109
AxedaCorp 0:65004368569c 110 ep_iso_in.setAddress(caminfo->en);
AxedaCorp 0:65004368569c 111 ep_iso_in.setSize(caminfo->mps);
AxedaCorp 0:65004368569c 112 uint8_t buf[26];
AxedaCorp 0:65004368569c 113 memset(buf, 0, sizeof(buf));
AxedaCorp 0:65004368569c 114 buf[2] = caminfo->formatIndex;
AxedaCorp 0:65004368569c 115 buf[3] = caminfo->frameIndex;
AxedaCorp 0:65004368569c 116 *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval;
AxedaCorp 0:65004368569c 117 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf));
AxedaCorp 0:65004368569c 118 if (res != USB_TYPE_OK) {
AxedaCorp 0:65004368569c 119 CAM_DBG("SET_CUR VS_COMMIT_CONTROL FAILED");
AxedaCorp 0:65004368569c 120 }
AxedaCorp 0:65004368569c 121 res = setInterfaceAlternate(1, caminfo->if_alt);
AxedaCorp 0:65004368569c 122 if (res != USB_TYPE_OK) {
AxedaCorp 0:65004368569c 123 CAM_DBG("SET_INTERFACE FAILED");
AxedaCorp 0:65004368569c 124 }
AxedaCorp 0:65004368569c 125 }
AxedaCorp 0:65004368569c 126 #endif
AxedaCorp 0:65004368569c 127
AxedaCorp 0:65004368569c 128
AxedaCorp 0:65004368569c 129 /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid)
AxedaCorp 0:65004368569c 130 {
AxedaCorp 0:65004368569c 131 CAM_DBG("vid:%04x,pid:%04x", vid, pid);
AxedaCorp 0:65004368569c 132 caminfo = CamInfoList;
AxedaCorp 0:65004368569c 133 while(caminfo->vid != 0) {
AxedaCorp 0:65004368569c 134 if (caminfo->vid == vid && caminfo->pid == pid &&
AxedaCorp 0:65004368569c 135 caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) {
AxedaCorp 0:65004368569c 136 caminfo_found = true;
AxedaCorp 0:65004368569c 137 break;
AxedaCorp 0:65004368569c 138 }
AxedaCorp 0:65004368569c 139 caminfo++;
AxedaCorp 0:65004368569c 140 }
AxedaCorp 0:65004368569c 141 }
AxedaCorp 0:65004368569c 142
AxedaCorp 0:65004368569c 143 /*virtual*/ bool USBHostCam::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
AxedaCorp 0:65004368569c 144 {
AxedaCorp 0:65004368569c 145 CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
AxedaCorp 0:65004368569c 146 if ((cam_intf == -1) && caminfo_found) {
AxedaCorp 0:65004368569c 147 cam_intf = intf_nb;
AxedaCorp 0:65004368569c 148 device_found = true;
AxedaCorp 0:65004368569c 149 return true;
AxedaCorp 0:65004368569c 150 }
AxedaCorp 0:65004368569c 151 return false;
AxedaCorp 0:65004368569c 152 }
AxedaCorp 0:65004368569c 153
AxedaCorp 0:65004368569c 154 /*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
AxedaCorp 0:65004368569c 155 {
AxedaCorp 0:65004368569c 156 CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir);
AxedaCorp 0:65004368569c 157 return false;
AxedaCorp 0:65004368569c 158 }
AxedaCorp 0:65004368569c 159
AxedaCorp 0:65004368569c 160 #define SEQ_READ_IDOL 0
AxedaCorp 0:65004368569c 161 #define SEQ_READ_EXEC 1
AxedaCorp 0:65004368569c 162 #define SEQ_READ_DONE 2
AxedaCorp 0:65004368569c 163
AxedaCorp 0:65004368569c 164 int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) {
AxedaCorp 0:65004368569c 165 _buf = buf;
AxedaCorp 0:65004368569c 166 _pos = 0;
AxedaCorp 0:65004368569c 167 _size = size;
AxedaCorp 0:65004368569c 168 _seq = SEQ_READ_IDOL;
AxedaCorp 0:65004368569c 169 setOnResult(this, &USBHostCam::callback_motion_jpeg);
AxedaCorp 0:65004368569c 170 Timer timeout_t;
AxedaCorp 0:65004368569c 171 timeout_t.reset();
AxedaCorp 0:65004368569c 172 timeout_t.start();
AxedaCorp 0:65004368569c 173 while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE) {
AxedaCorp 0:65004368569c 174 poll();
AxedaCorp 0:65004368569c 175 }
AxedaCorp 0:65004368569c 176 return _pos;
AxedaCorp 0:65004368569c 177 }
AxedaCorp 0:65004368569c 178
AxedaCorp 0:65004368569c 179 /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG
AxedaCorp 0:65004368569c 180 if (_seq == SEQ_READ_IDOL) {
AxedaCorp 0:65004368569c 181 if (status == JPEG_START) {
AxedaCorp 0:65004368569c 182 _pos = 0;
AxedaCorp 0:65004368569c 183 _seq = SEQ_READ_EXEC;
AxedaCorp 0:65004368569c 184 }
AxedaCorp 0:65004368569c 185 }
AxedaCorp 0:65004368569c 186 if (_seq == SEQ_READ_EXEC) {
AxedaCorp 0:65004368569c 187 if (_pos < _size) {
AxedaCorp 0:65004368569c 188 _buf[_pos++] = c;
AxedaCorp 0:65004368569c 189 }
AxedaCorp 0:65004368569c 190 if (status == JPEG_END) {
AxedaCorp 0:65004368569c 191 _seq = SEQ_READ_DONE;
AxedaCorp 0:65004368569c 192 }
AxedaCorp 0:65004368569c 193 }
AxedaCorp 0:65004368569c 194 }
AxedaCorp 0:65004368569c 195
AxedaCorp 0:65004368569c 196 void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
AxedaCorp 0:65004368569c 197 inputPacket(buf, len);
AxedaCorp 0:65004368569c 198 }