Final

Dependencies:   DebounceIn NokiaLCD WiflyInterface mbed

Fork of Websocket_Wifly_HelloWorld by Samuel Mokrani

Committer:
Ifrah
Date:
Fri Dec 14 15:38:15 2012 +0000
Revision:
4:6ed9c8bb82c3
Parent:
3:3c7906d60f89
Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:31e50fea8be8 1 #include "mbed.h"
samux 1:31e50fea8be8 2 #include "WiflyInterface.h"
Ifrah 3:3c7906d60f89 3 #include "NokiaLCD.h"
Ifrah 3:3c7906d60f89 4 #include "picojpeg.h"
Ifrah 3:3c7906d60f89 5 #include "DebounceIn.h"
Ifrah 3:3c7906d60f89 6 #include "GPS.h"
Ifrah 3:3c7906d60f89 7 #include <vector>
samux 1:31e50fea8be8 8
Ifrah 3:3c7906d60f89 9 #define DEFAULTZOOM 15
samux 1:31e50fea8be8 10
samux 1:31e50fea8be8 11 /* wifly interface:
Ifrah 3:3c7906d60f89 12 * - p13 and p14 are for the serial communication
Ifrah 3:3c7906d60f89 13 * - p28 is for the reset pin
Ifrah 3:3c7906d60f89 14 * - p27 is for the connection status
Ifrah 3:3c7906d60f89 15 * - 5th field is the ssid of the network
Ifrah 3:3c7906d60f89 16 * - 6th field is the password
samux 1:31e50fea8be8 17 * - WPA is the security
samux 1:31e50fea8be8 18 */
Ifrah 3:3c7906d60f89 19
Ifrah 4:6ed9c8bb82c3 20 WiflyInterface wifly(p13, p14, p28, p27, "username", "password", WPA);
Ifrah 3:3c7906d60f89 21 Serial pc(USBTX,USBRX);
Ifrah 3:3c7906d60f89 22 LocalFileSystem local("local");// Create the local filesystem under the name "local"
Ifrah 3:3c7906d60f89 23 GPS gps(p9, p10);
Ifrah 3:3c7906d60f89 24 DebounceIn ZoomIn(p18);
Ifrah 3:3c7906d60f89 25 DebounceIn Refresh(p19);
Ifrah 3:3c7906d60f89 26 DebounceIn ZoomOut(p20);
Ifrah 3:3c7906d60f89 27 FILE *jpegfile;
Ifrah 3:3c7906d60f89 28 int jpeg_filesize = 0;
Ifrah 3:3c7906d60f89 29 int jpeg_filepos = 0;
Ifrah 3:3c7906d60f89 30 NokiaLCD lcd(p5, p7, p8, p11, NokiaLCD::LCD6610);
Ifrah 3:3c7906d60f89 31 int zoom = DEFAULTZOOM;
Ifrah 3:3c7906d60f89 32
Ifrah 3:3c7906d60f89 33
Ifrah 3:3c7906d60f89 34 unsigned char pjpeg_need_bytes_callback(unsigned char* pBuf, unsigned char buf_size, unsigned char *pBytes_actually_read, void *pCallback_data)
Ifrah 3:3c7906d60f89 35 {
Ifrah 3:3c7906d60f89 36 unsigned int n = min((unsigned int)(jpeg_filesize - jpeg_filepos), (unsigned int)buf_size);
Ifrah 3:3c7906d60f89 37 if (n && (fread(pBuf, 1, n, jpegfile) != n))
Ifrah 3:3c7906d60f89 38 return PJPG_STREAM_READ_ERROR;
Ifrah 3:3c7906d60f89 39 *pBytes_actually_read = (unsigned char)(n);
Ifrah 3:3c7906d60f89 40 jpeg_filepos += n;
Ifrah 3:3c7906d60f89 41 return 0;
Ifrah 3:3c7906d60f89 42 }
Ifrah 3:3c7906d60f89 43
Ifrah 3:3c7906d60f89 44 void ReadJPEGFromFile(const char *filename, NokiaLCD *lcd)
Ifrah 3:3c7906d60f89 45 {
Ifrah 3:3c7906d60f89 46 pjpeg_image_info_t imageInfo;
Ifrah 3:3c7906d60f89 47 jpegfile = fopen(filename,"rb");
Ifrah 3:3c7906d60f89 48 if(!jpegfile) {
Ifrah 3:3c7906d60f89 49 pc.printf("File not Found: %s\n\r",filename);
Ifrah 3:3c7906d60f89 50 }
Ifrah 3:3c7906d60f89 51 fseek(jpegfile, 0, SEEK_END);
Ifrah 3:3c7906d60f89 52 jpeg_filesize = ftell(jpegfile);
Ifrah 3:3c7906d60f89 53 jpeg_filepos = 0;
Ifrah 3:3c7906d60f89 54 fseek(jpegfile, 0, SEEK_SET);
Ifrah 3:3c7906d60f89 55 int status = pjpeg_decode_init(&imageInfo, pjpeg_need_bytes_callback, NULL);
Ifrah 3:3c7906d60f89 56 //const unsigned int row_pitch = imageInfo.m_width * imageInfo.m_comps;
Ifrah 3:3c7906d60f89 57 int mcu_x = 0;
Ifrah 3:3c7906d60f89 58 int mcu_y = 0;
Ifrah 3:3c7906d60f89 59
Ifrah 3:3c7906d60f89 60 for ( ; ; ) {
Ifrah 3:3c7906d60f89 61 status = pjpeg_decode_mcu();
Ifrah 3:3c7906d60f89 62
Ifrah 3:3c7906d60f89 63 if (status) {
Ifrah 3:3c7906d60f89 64 if (status != PJPG_NO_MORE_BLOCKS) {
Ifrah 3:3c7906d60f89 65 //pc.printf("pjpeg_decode_mcu() failed with status %u\n", status);
Ifrah 3:3c7906d60f89 66 fclose(jpegfile);
Ifrah 3:3c7906d60f89 67 return;
Ifrah 3:3c7906d60f89 68 }
samux 1:31e50fea8be8 69
Ifrah 3:3c7906d60f89 70 break;
Ifrah 3:3c7906d60f89 71 }
Ifrah 3:3c7906d60f89 72
Ifrah 3:3c7906d60f89 73 if (mcu_y >= imageInfo.m_MCUSPerCol) {
Ifrah 3:3c7906d60f89 74 fclose(jpegfile);
Ifrah 3:3c7906d60f89 75 return;
Ifrah 3:3c7906d60f89 76 }
Ifrah 3:3c7906d60f89 77
Ifrah 3:3c7906d60f89 78 // Copy MCU's pixel blocks into the destination bitmap.
Ifrah 3:3c7906d60f89 79
Ifrah 3:3c7906d60f89 80 for (int y = 0; y < imageInfo.m_MCUHeight; y += 8) {
Ifrah 3:3c7906d60f89 81 const int by_limit = min(8, imageInfo.m_height - (mcu_y * imageInfo.m_MCUHeight + y));
Ifrah 3:3c7906d60f89 82 for (int x = 0; x < imageInfo.m_MCUWidth; x += 8) {
Ifrah 3:3c7906d60f89 83
Ifrah 3:3c7906d60f89 84 unsigned int src_ofs = (x * 8U) + (y * 16U);
Ifrah 3:3c7906d60f89 85 const unsigned char *pSrcR = imageInfo.m_pMCUBufR + src_ofs;
Ifrah 3:3c7906d60f89 86 const unsigned char *pSrcG = imageInfo.m_pMCUBufG + src_ofs;
Ifrah 3:3c7906d60f89 87 const unsigned char *pSrcB = imageInfo.m_pMCUBufB + src_ofs;
Ifrah 3:3c7906d60f89 88
Ifrah 3:3c7906d60f89 89 const int bx_limit = min(8, imageInfo.m_width - (mcu_x * imageInfo.m_MCUWidth + x));
Ifrah 3:3c7906d60f89 90
Ifrah 3:3c7906d60f89 91 if (imageInfo.m_scanType == PJPG_GRAYSCALE) {
Ifrah 3:3c7906d60f89 92 for (int by = 0; by < by_limit; by++) {
Ifrah 3:3c7906d60f89 93 for (int bx = 0; bx < bx_limit; bx++) {
Ifrah 3:3c7906d60f89 94 unsigned int color = ((*pSrcR++) << 16);
Ifrah 3:3c7906d60f89 95 (*lcd).pixel(mcu_x*imageInfo.m_MCUWidth+x+bx,mcu_y*imageInfo.m_MCUHeight+y+by,color);
Ifrah 3:3c7906d60f89 96 }
Ifrah 3:3c7906d60f89 97 pSrcR += (8 - bx_limit);
Ifrah 3:3c7906d60f89 98 }
Ifrah 3:3c7906d60f89 99 } else {
Ifrah 3:3c7906d60f89 100 for (int by = 0; by < by_limit; by++) {
Ifrah 3:3c7906d60f89 101 for (int bx = 0; bx < bx_limit; bx++) {
Ifrah 3:3c7906d60f89 102 unsigned int color = ((*pSrcR++) << 16) | ((*pSrcG++) << 8) | (*pSrcB++);
Ifrah 3:3c7906d60f89 103 (*lcd).pixel((130-imageInfo.m_width)/2+mcu_x*imageInfo.m_MCUWidth+x+bx,(130-imageInfo.m_height)/2+mcu_y*imageInfo.m_MCUHeight+y+by,color);
Ifrah 3:3c7906d60f89 104 }
Ifrah 3:3c7906d60f89 105
Ifrah 3:3c7906d60f89 106 pSrcR += (8 - bx_limit);
Ifrah 3:3c7906d60f89 107 pSrcG += (8 - bx_limit);
Ifrah 3:3c7906d60f89 108 pSrcB += (8 - bx_limit);
Ifrah 3:3c7906d60f89 109
Ifrah 3:3c7906d60f89 110 }
Ifrah 3:3c7906d60f89 111 }
Ifrah 3:3c7906d60f89 112 }
Ifrah 3:3c7906d60f89 113 }
Ifrah 3:3c7906d60f89 114
Ifrah 3:3c7906d60f89 115 mcu_x++;
Ifrah 3:3c7906d60f89 116 if (mcu_x == imageInfo.m_MCUSPerRow) {
Ifrah 3:3c7906d60f89 117 mcu_x = 0;
Ifrah 3:3c7906d60f89 118 mcu_y++;
Ifrah 3:3c7906d60f89 119 }
Ifrah 3:3c7906d60f89 120 }
samux 1:31e50fea8be8 121
Ifrah 3:3c7906d60f89 122 fclose(jpegfile);
Ifrah 3:3c7906d60f89 123 }
Ifrah 3:3c7906d60f89 124
Ifrah 3:3c7906d60f89 125 void wiflyConnect()
Ifrah 3:3c7906d60f89 126 {
Ifrah 3:3c7906d60f89 127 wifly.disconnect();
Ifrah 3:3c7906d60f89 128 wifly.init();
Ifrah 3:3c7906d60f89 129 while (!wifly.connect()) {
Ifrah 3:3c7906d60f89 130 pc.printf("waiting\n\r");
Ifrah 3:3c7906d60f89 131 }// join the network
Ifrah 3:3c7906d60f89 132 }
Ifrah 3:3c7906d60f89 133
Ifrah 3:3c7906d60f89 134 void getNewImage(float latitude, float longitude, int zoom)
Ifrah 3:3c7906d60f89 135 {
Ifrah 3:3c7906d60f89 136 TCPSocketConnection sock;
Ifrah 3:3c7906d60f89 137 sock.connect("maps.googleapis.com", 80);
Ifrah 3:3c7906d60f89 138 int ret = 0;
Ifrah 3:3c7906d60f89 139 int total_bytes = 0;
Ifrah 3:3c7906d60f89 140 char http_cmd[300];
Ifrah 3:3c7906d60f89 141 sprintf(http_cmd, "GET /maps/api/staticmap?center=%f%%2C%f&zoom=%d&size=130x130&sensor=true&format=jpg-baseline HTTP/1.0\n\n",latitude,longitude,zoom);
Ifrah 3:3c7906d60f89 142 sock.send_all(http_cmd, sizeof(http_cmd)-1);
samux 1:31e50fea8be8 143
Ifrah 3:3c7906d60f89 144 sock.set_blocking(false,10000);
Ifrah 3:3c7906d60f89 145 char buffer[300];
Ifrah 3:3c7906d60f89 146 FILE* pFile;
Ifrah 3:3c7906d60f89 147 pc.printf("%s\n\r",http_cmd);
Ifrah 3:3c7906d60f89 148 pFile = fopen ("/local/image.jpg","w");
Ifrah 3:3c7906d60f89 149 pc.printf("Opening File\n\r");
Ifrah 3:3c7906d60f89 150 bool found_start = false;
Ifrah 3:3c7906d60f89 151 bool found_end = false;
Ifrah 3:3c7906d60f89 152 while (true) {
Ifrah 3:3c7906d60f89 153 ret = sock.receive(buffer, sizeof(buffer)-1);
Ifrah 3:3c7906d60f89 154 if (ret <= 0) {
Ifrah 3:3c7906d60f89 155 pc.printf("Ret < 0. Break. \n\r");
Ifrah 3:3c7906d60f89 156 //wiflyConnect();
Ifrah 3:3c7906d60f89 157 break;
Ifrah 3:3c7906d60f89 158 }
Ifrah 3:3c7906d60f89 159 buffer[ret] = '\0';
Ifrah 3:3c7906d60f89 160 total_bytes += ret;
Ifrah 3:3c7906d60f89 161 pc.printf("Received %d chars from server, total %d: %s\n\r", ret,total_bytes, buffer);
Ifrah 3:3c7906d60f89 162 if(found_start) {
Ifrah 3:3c7906d60f89 163 for(int i=1; i<ret; i++) {
Ifrah 3:3c7906d60f89 164 if(buffer[i-1]==0xFF && buffer[i]==0xD9) { //jpg file end at FFD9
Ifrah 3:3c7906d60f89 165 pc.printf("found end. \n\r");
Ifrah 3:3c7906d60f89 166 fwrite(buffer,1,i+1,pFile);
Ifrah 3:3c7906d60f89 167 found_end = true;
Ifrah 3:3c7906d60f89 168 break;
Ifrah 3:3c7906d60f89 169 }
Ifrah 3:3c7906d60f89 170 }
Ifrah 3:3c7906d60f89 171 if(!found_end)
Ifrah 3:3c7906d60f89 172 fwrite(buffer,1,ret,pFile);
Ifrah 3:3c7906d60f89 173 else {
Ifrah 3:3c7906d60f89 174 pc.printf("Found Start and End. Break. \n\r");
Ifrah 3:3c7906d60f89 175 break;
Ifrah 3:3c7906d60f89 176 }
Ifrah 3:3c7906d60f89 177
Ifrah 3:3c7906d60f89 178 } else {
Ifrah 3:3c7906d60f89 179 for(int i=1; i<ret; i++) {
Ifrah 3:3c7906d60f89 180 if(buffer[i-1]==0xFF && buffer[i]==0xD8) { //jpg file starts from FFD8
Ifrah 3:3c7906d60f89 181 pc.printf("found start.\n\r");
Ifrah 3:3c7906d60f89 182 fwrite(&buffer[i-1],1,ret-i+1,pFile);
Ifrah 3:3c7906d60f89 183 found_start = true;
Ifrah 3:3c7906d60f89 184 break;
Ifrah 3:3c7906d60f89 185 }
Ifrah 3:3c7906d60f89 186 }
Ifrah 3:3c7906d60f89 187 }
samux 1:31e50fea8be8 188 }
Ifrah 3:3c7906d60f89 189
Ifrah 3:3c7906d60f89 190 fclose (pFile);
Ifrah 3:3c7906d60f89 191 pc.printf("Closing File\n\r");
Ifrah 3:3c7906d60f89 192 sock.close();
Ifrah 3:3c7906d60f89 193 lcd.cls();
Ifrah 3:3c7906d60f89 194 ReadJPEGFromFile("/local/IMAGE.JPG", &lcd);
Ifrah 3:3c7906d60f89 195 }
Ifrah 3:3c7906d60f89 196
Ifrah 3:3c7906d60f89 197 int main()
Ifrah 3:3c7906d60f89 198 {
Ifrah 3:3c7906d60f89 199 pc.printf("Hello\n\r");
Ifrah 3:3c7906d60f89 200 wifly.init(); //Use DHCP
Ifrah 3:3c7906d60f89 201 while (!wifly.connect()) {
Ifrah 3:3c7906d60f89 202 pc.printf("waiting\n\r");
Ifrah 3:3c7906d60f89 203 }// join the network
Ifrah 3:3c7906d60f89 204 pc.printf("IP Address is %s\n", wifly.getIPAddress());
Ifrah 3:3c7906d60f89 205
Ifrah 3:3c7906d60f89 206 float longitude = 0.0;
Ifrah 3:3c7906d60f89 207 float latitude = 0.0;
Ifrah 3:3c7906d60f89 208 while(1) {
Ifrah 3:3c7906d60f89 209 if(gps.sample()) {
Ifrah 3:3c7906d60f89 210 // pc.printf("I'm at %f, %f\n", gps.longitude, gps.latitude);
Ifrah 3:3c7906d60f89 211 longitude = gps.longitude;
Ifrah 3:3c7906d60f89 212 latitude = gps.latitude;
Ifrah 3:3c7906d60f89 213 } else {
Ifrah 3:3c7906d60f89 214 pc.printf("Oh Dear! No lock :(\n");
Ifrah 3:3c7906d60f89 215 }
Ifrah 3:3c7906d60f89 216 if(Refresh)
Ifrah 3:3c7906d60f89 217 getNewImage(latitude,longitude,zoom);
Ifrah 3:3c7906d60f89 218 else if(ZoomIn)
Ifrah 3:3c7906d60f89 219 getNewImage(latitude,longitude,++zoom);
Ifrah 3:3c7906d60f89 220 else if(ZoomOut)
Ifrah 3:3c7906d60f89 221 getNewImage(latitude,longitude,--zoom);
Ifrah 3:3c7906d60f89 222 }
Ifrah 3:3c7906d60f89 223 wifly.disconnect();
samux 0:64c683256184 224 }