HOme Sheriff And Lamp

Dependencies:   CameraC328 HCSR04 SDFileSystem WIZnetInterface mbed

Fork of HoSAL by pi bae

Committer:
galucpia
Date:
Wed Aug 12 11:16:46 2015 +0000
Revision:
11:7db34a66a751
Parent:
9:416cbcabbddd
Child:
12:974f7a96d6ab
Add LED RGB On/Off

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uasonice 0:35211a622a44 1 #include "mbed.h"
uasonice 4:ca368c50f8c9 2 #include "EthernetInterface.h"
uasonice 4:ca368c50f8c9 3 #include "SDFileSystem.h"
uasonice 4:ca368c50f8c9 4 #include <stdio.h>
uasonice 4:ca368c50f8c9 5 #include <string.h>
uasonice 0:35211a622a44 6
uasonice 0:35211a622a44 7 #define DEBUG_TYPE 1
uasonice 0:35211a622a44 8 #define P_ uart.printf
uasonice 0:35211a622a44 9 #include "rev_config.h"
uasonice 0:35211a622a44 10
uasonice 3:8c4e0e7c8cea 11 #include "rev_Camera.h"
uasonice 4:ca368c50f8c9 12 #include "rev_httpFile.h"
uasonice 3:8c4e0e7c8cea 13 #include "rev_Hcsr04.h"
uasonice 0:35211a622a44 14
uasonice 0:35211a622a44 15
uasonice 3:8c4e0e7c8cea 16 #define MAC "\x00\x08\xDC\x11\x34\x78"
uasonice 3:8c4e0e7c8cea 17 #define IP "192.168.5.5"
uasonice 3:8c4e0e7c8cea 18 #define MASK "255.255.255.0"
uasonice 3:8c4e0e7c8cea 19 #define GATEWAY "192.168.5.1"
uasonice 3:8c4e0e7c8cea 20
uasonice 3:8c4e0e7c8cea 21 #define HTTPD_SERVER_PORT 80
uasonice 0:35211a622a44 22
uasonice 0:35211a622a44 23 Serial uart(USBTX, USBRX); // tx, rx
uasonice 0:35211a622a44 24
uasonice 4:ca368c50f8c9 25
uasonice 4:ca368c50f8c9 26 #if defined(USE_SDCARD)
uasonice 4:ca368c50f8c9 27 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500
uasonice 4:ca368c50f8c9 28 #endif // defined(USE_SDCARD)
uasonice 4:ca368c50f8c9 29
uasonice 4:ca368c50f8c9 30 #if defined(USE_HTTP_FILE_SERVER)
uasonice 4:ca368c50f8c9 31 EthernetInterface eth;
uasonice 4:ca368c50f8c9 32 TCPSocketServer server;
uasonice 4:ca368c50f8c9 33 TCPSocketConnection client;
uasonice 4:ca368c50f8c9 34 #endif // defined(USE_HTTP_FILE_SERVER)
uasonice 4:ca368c50f8c9 35
uasonice 3:8c4e0e7c8cea 36 #if defined(USE_CAMERA)
uasonice 3:8c4e0e7c8cea 37 //CameraC328 cam(PA_14, PA_13, CameraC328::Baud14400);
uasonice 3:8c4e0e7c8cea 38 CameraC328 *g_pCam;
uasonice 3:8c4e0e7c8cea 39 #endif // defined(USE_CAMERA)
uasonice 3:8c4e0e7c8cea 40
uasonice 3:8c4e0e7c8cea 41 #if defined(USE_MEASURE_DISTANCE)
uasonice 2:3c7526a1893a 42 //HCSR04 sensor(D12, D11);
uasonice 4:ca368c50f8c9 43 HCSR04 *g_pHcsr;
uasonice 3:8c4e0e7c8cea 44 #define CHECK_DISTANCE 50
uasonice 3:8c4e0e7c8cea 45 #endif // defined(USE_MEASURE_DISTANCE)
uasonice 0:35211a622a44 46
uasonice 0:35211a622a44 47 DigitalOut led1(LED1); //server listning status
uasonice 0:35211a622a44 48 DigitalOut led2(LED2); //socket connecting status
uasonice 0:35211a622a44 49
galucpia 11:7db34a66a751 50
galucpia 11:7db34a66a751 51 DigitalOut myled_R(D7);
galucpia 11:7db34a66a751 52 DigitalOut myled_G(D6);
galucpia 11:7db34a66a751 53 DigitalOut myled_B(D5);
galucpia 11:7db34a66a751 54
galucpia 11:7db34a66a751 55 void LED_ON()
galucpia 11:7db34a66a751 56 {
galucpia 11:7db34a66a751 57 DM_fLN("In LED_ON");
galucpia 11:7db34a66a751 58 myled_R = 1;
galucpia 11:7db34a66a751 59 myled_G = 1;
galucpia 11:7db34a66a751 60 myled_B = 1;
galucpia 11:7db34a66a751 61 }
galucpia 11:7db34a66a751 62
galucpia 11:7db34a66a751 63 void LED_OFF()
galucpia 11:7db34a66a751 64 {
galucpia 11:7db34a66a751 65 DM_fLN("In LED_OFF");
galucpia 11:7db34a66a751 66 myled_R = 0;
galucpia 11:7db34a66a751 67 myled_G = 0;
galucpia 11:7db34a66a751 68 myled_B = 0;
galucpia 11:7db34a66a751 69 }
uasonice 0:35211a622a44 70 //////////////////////////////////////////////////////////////////////////
uasonice 0:35211a622a44 71 Ticker ledTick;
uasonice 0:35211a622a44 72
uasonice 0:35211a622a44 73 //////////////////////////////////////////////////////////////////////////
uasonice 0:35211a622a44 74 void ledTickfunc()
uasonice 0:35211a622a44 75 {
uasonice 0:35211a622a44 76 led1 = !led1;
uasonice 0:35211a622a44 77 }
uasonice 0:35211a622a44 78
uasonice 0:35211a622a44 79 //////////////////////////////////////////////////////////////////////////
uasonice 0:35211a622a44 80 int main()
uasonice 0:35211a622a44 81 {
uasonice 3:8c4e0e7c8cea 82 Timer tm1;
uasonice 7:58b14840531c 83 char strFile[32];
uasonice 7:58b14840531c 84 uint32_t cntImage=1;
uasonice 3:8c4e0e7c8cea 85
uasonice 0:35211a622a44 86 ledTick.attach(&ledTickfunc,0.5);
uasonice 0:35211a622a44 87
uasonice 0:35211a622a44 88 uart.baud(115200);
galucpia 11:7db34a66a751 89
galucpia 11:7db34a66a751 90 LED_OFF();
galucpia 11:7db34a66a751 91
galucpia 11:7db34a66a751 92
uasonice 3:8c4e0e7c8cea 93
uasonice 3:8c4e0e7c8cea 94 DM_fLN("INIT DEVICE");
uasonice 4:ca368c50f8c9 95 #if defined(USE_SDCARD)
uasonice 4:ca368c50f8c9 96 // Check File System
uasonice 4:ca368c50f8c9 97 DM_fLN("Checking File System");
uasonice 4:ca368c50f8c9 98 DIR *d = opendir("/sd/");
uasonice 4:ca368c50f8c9 99 if (d != NULL) {
uasonice 4:ca368c50f8c9 100 DM_fLN("SD Card Present");
uasonice 4:ca368c50f8c9 101 } else {
uasonice 4:ca368c50f8c9 102 DM_fLN("SD Card Root Directory Not Found");
uasonice 4:ca368c50f8c9 103 }
uasonice 4:ca368c50f8c9 104 #endif // defined(USE_SDCARD)
uasonice 4:ca368c50f8c9 105
uasonice 4:ca368c50f8c9 106 #if defined(USE_HTTP_FILE_SERVER)
uasonice 4:ca368c50f8c9 107 // EthernetInterface eth;
uasonice 4:ca368c50f8c9 108 DM_fLN("Init Ethernet");
uasonice 4:ca368c50f8c9 109 //eth.init(); //Use DHCP
uasonice 4:ca368c50f8c9 110 eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway
uasonice 4:ca368c50f8c9 111 DM_fLN("Connecting");
uasonice 4:ca368c50f8c9 112 eth.connect();
uasonice 4:ca368c50f8c9 113 DM_fLN("IP Address is %s", eth.getIPAddress());
uasonice 4:ca368c50f8c9 114
uasonice 4:ca368c50f8c9 115 // TCPSocketServer server;
uasonice 4:ca368c50f8c9 116 server.bind(HTTPD_SERVER_PORT);
uasonice 4:ca368c50f8c9 117 server.listen();
uasonice 4:ca368c50f8c9 118 DM_fLN("Server Listening");
uasonice 4:ca368c50f8c9 119
uasonice 4:ca368c50f8c9 120 // FIXME: no work - non-block mode
uasonice 4:ca368c50f8c9 121 server.set_blocking(false, 1000);
uasonice 4:ca368c50f8c9 122 #endif // defined(USE_HTTP_FILE_SERVER)
uasonice 4:ca368c50f8c9 123
uasonice 3:8c4e0e7c8cea 124 #if defined(USE_CAMERA)
uasonice 3:8c4e0e7c8cea 125 DM_fLN("Init camera C328");
uasonice 9:416cbcabbddd 126 g_pCam = new CameraC328(PA_13, PA_14, CameraC328::Baud14400);
uasonice 9:416cbcabbddd 127 //g_pCam = new CameraC328(PA_13, PA_14, CameraC328::Baud38400);
uasonice 3:8c4e0e7c8cea 128 revSync(g_pCam);
uasonice 7:58b14840531c 129 #if 0
uasonice 3:8c4e0e7c8cea 130 tm1.reset();
uasonice 3:8c4e0e7c8cea 131 tm1.start();
uasonice 7:58b14840531c 132 revJpeg_snapshot(g_pCam, "/sd/test_shoot.jpg", CameraC328::JpegResolution640x480);
uasonice 3:8c4e0e7c8cea 133 tm1.stop();
uasonice 3:8c4e0e7c8cea 134 DM_fLN("time of capture: %d", tm1.read_ms());
uasonice 7:58b14840531c 135 #endif
uasonice 3:8c4e0e7c8cea 136 #endif // defined(USE_CAMERA)
uasonice 3:8c4e0e7c8cea 137 #if defined(USE_MEASURE_DISTANCE)
uasonice 4:ca368c50f8c9 138 g_pHcsr = new HCSR04(D12, D11);
uasonice 3:8c4e0e7c8cea 139 #endif // defined(USE_MEASURE_DISTANCE)
uasonice 3:8c4e0e7c8cea 140
uasonice 3:8c4e0e7c8cea 141 DM_fLN("start main loop");
uasonice 0:35211a622a44 142 while(1) {
uasonice 4:ca368c50f8c9 143 int dist_cm = 0;
uasonice 4:ca368c50f8c9 144 tm1.reset();
uasonice 4:ca368c50f8c9 145 tm1.start();
uasonice 3:8c4e0e7c8cea 146 #if defined(USE_MEASURE_DISTANCE)
uasonice 4:ca368c50f8c9 147 if( (dist_cm = get_distance(g_pHcsr)) < CHECK_DISTANCE )
uasonice 3:8c4e0e7c8cea 148 #endif // defined(USE_MEASURE_DISTANCE)
uasonice 4:ca368c50f8c9 149 {
galucpia 11:7db34a66a751 150 //Led On
galucpia 11:7db34a66a751 151 LED_ON();
uasonice 4:ca368c50f8c9 152 DM_fLN("capture image: %d", dist_cm);
uasonice 4:ca368c50f8c9 153 #if defined(USE_CAMERA)
uasonice 7:58b14840531c 154 memset(strFile, 0, 32);
uasonice 7:58b14840531c 155 sprintf(strFile, "/sd/file_%03d.jpg", cntImage++);
uasonice 7:58b14840531c 156 revJpeg_snapshot(g_pCam, strFile, CameraC328::JpegResolution640x480);
galucpia 11:7db34a66a751 157 //Led Off
galucpia 11:7db34a66a751 158 LED_OFF();
uasonice 4:ca368c50f8c9 159 #endif // defined(USE_CAMERA)
uasonice 4:ca368c50f8c9 160
uasonice 4:ca368c50f8c9 161 }
uasonice 4:ca368c50f8c9 162 fileServer();
uasonice 4:ca368c50f8c9 163
uasonice 4:ca368c50f8c9 164 //wait_ms(1000);
uasonice 4:ca368c50f8c9 165 tm1.stop();
uasonice 5:217f40f0a415 166 //DM_fLN("time: %d", tm1.read_ms());
uasonice 0:35211a622a44 167 }
uasonice 4:ca368c50f8c9 168 //return 0;
uasonice 0:35211a622a44 169 }