v1.0
Dependencies: SDFileSystem mbed
Revision 0:0073c8def9f1, committed 2016-05-09
- Comitter:
- jehoon
- Date:
- Mon May 09 00:13:40 2016 +0000
- Commit message:
- WiFi Tracker
Changed in this revision
diff -r 000000000000 -r 0073c8def9f1 RingBuffer/RingBuffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RingBuffer/RingBuffer.cpp Mon May 09 00:13:40 2016 +0000 @@ -0,0 +1,109 @@ +#include <RingBuffer.h> + +RingBuffer::RingBuffer() +{ + +} + +RingBuffer::RingBuffer(int length) +{ + this->buffer = (byte *)malloc(length); + this->max = length; + this->fill = 0; + this->ptr = 0; +} + +RingBuffer::~RingBuffer() +{ + free (this->buffer); +} + +void RingBuffer::init(int length) +{ + this->buffer = (byte *)malloc(length); + this->max = length; + this->fill = 0; + this->ptr = 0; +} + +void RingBuffer::clear() +{ + this->fill = 0; +} + +bool RingBuffer::isFull() +{ + return (this->max == this->fill); +} + +bool RingBuffer::hasData() +{ + return (this->fill != 0); +} + +bool RingBuffer::addByte(byte b) +{ + if (this->max == this->fill) + return false; + + int idx = (this->ptr + this->fill) % this->max; + this->buffer[idx] = b; + this->fill++; + return true; +} + +byte RingBuffer::consumeByte() +{ + if (this->fill == 0) + return 0; + + byte ret = this->buffer[this->ptr]; + this->fill--; + this->ptr++; + this->ptr %= this->max; + return ret; +} + +byte RingBuffer::peek(int idx) +{ + byte p = (this->ptr + idx) % this->max; + return this->buffer[p]; +} + +int RingBuffer::findBuf(char * str) +{ + int i=0; + int cnt = 0; + int len = strlen(str); + byte *tmp = (byte *) str; + + if( this->fill == 0 ) + return -1; + + for( i=0; i<this->fill; i++ ) + { + if( peek(i) == *tmp ) + { + tmp++; + cnt++; + + if(cnt == len) + { + return (i - len + 1); + } + } + else + { + tmp = (byte *)str; + cnt=0; + } + } + + return -1; +} + + +int RingBuffer::count() +{ + return this->fill; +} \ No newline at end of file
diff -r 000000000000 -r 0073c8def9f1 RingBuffer/RingBuffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RingBuffer/RingBuffer.h Mon May 09 00:13:40 2016 +0000 @@ -0,0 +1,55 @@ +/* +Copyright (c) 2016 Jorj Bauer <jorj@jorj.org> + + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +https://github.com/JorjBauer/RingBuffer +*/ + +#include <mbed.h> + +typedef uint8_t byte; + +class RingBuffer { + public: + RingBuffer(); + RingBuffer(int length); + ~RingBuffer(); + + void init(int length); + void clear(); + + bool isFull(); + bool hasData(); + bool addByte(byte b); + byte consumeByte(); + byte peek(int idx); + int count(); + int findBuf(char * str); + + + private: + byte *buffer; + int max; + int ptr; + int fill; + +};
diff -r 000000000000 -r 0073c8def9f1 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Mon May 09 00:13:40 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
diff -r 000000000000 -r 0073c8def9f1 WiFiTracker.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiFiTracker.cpp Mon May 09 00:13:40 2016 +0000 @@ -0,0 +1,141 @@ +#include "mbed.h" +#include "WiFiTracker.h" + + + +WiFiTracker::WiFiTracker() : dbg(USBTX, USBRX), sd(PB_3, PB_2, PB_1, PB_0, "sd"), led(LED3) +{ + + buffer.init(128); + printf("make wifi tracker!\n"); + g_store_cnt = 0; + mkdir("/sd/mydir", 0777); + + LedBlink(5, 20); + +} + + +WiFiTracker::~WiFiTracker() +{ + printf("destroy\n"); + fclose(fp); +} + + +//uint8_t WiFiTracker::store_probe_request(WiFiProbe wp) +//uint8_t WiFiTracker::store_probe_request(char c) +uint8_t WiFiTracker::write_sdcard(char c) +{ + dbg.printf("store\n"); + + return 0; +} + +uint8_t WiFiTracker::write_sdcard(char* str, int len) +{ + char temp_str[30] = {0,}; + + //dbg.printf("%s, (%d)", str, len); + + if(char_cnt(str,'/') && len == 30 ) + { + strncpy(temp_str, str, len); + char_change(temp_str, '/', ','); + dbg.printf("(%d), %s", len, temp_str); + fprintf(fp, temp_str); + g_store_cnt++; + } + + + if(g_store_cnt % 100 == 0) + { + dbg.printf("\r\n[100]\r\n"); + fclose(fp); + + fp = fopen("/sd/mydir/wifi_tracker.txt", "a"); + if(fp == NULL) { + error("Could not open file for write\n"); + } + + LedBlink(2,50); + } + + //dbg.printf("\r\nwrite sd card\r\n"); + return 0; +} + +char WiFiTracker::read() +{ + return (char)buffer.consumeByte(); +} + +int WiFiTracker::write(char c) +{ + //return buffer.addbyte((byte)c); + return 0; + +} + +int WiFiTracker::find(char* str) +{ + return buffer.findBuf(str); + +} + + + +int WiFiTracker::char_cnt(char* str, char c) +{ + int i = 0; + char* tmp = str; + + while( *tmp ) + { + if( *tmp++ == c ) + i++; + } + + return i; +} + + +int WiFiTracker::char_change(char* str, char dst_char, char new_char) +{ + int i = 0; + char* tmp = str; + + while( *tmp ) + { + if( *tmp == dst_char ) + *tmp = new_char; + tmp++; + } + + return i; + +} + +void WiFiTracker::LedBlink(int cnt, int interval) +{ + int i; + + for(i=0; i<cnt; i++) + { + led = 1; + wait_ms(interval); + led = 0; + } +} + +// +//int8_t WiFiTracker::send_command(char* cmd) +//{ +// +//} +//int8_t WiFiTracker::wait_response(char* rsp) +//{ +//} + + +
diff -r 000000000000 -r 0073c8def9f1 WiFiTracker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiFiTracker.h Mon May 09 00:13:40 2016 +0000 @@ -0,0 +1,44 @@ +#include "RingBuffer.h" +#include "SDFileSystem.h" +#include "mbed.h" + +typedef struct WiFiProbe +{ + uint8_t mac[6]; + int8_t rssi; + int8_t cnt; + +}WiFiProbe; + + +class WiFiTracker +{ +public: + + WiFiTracker() ; + ~WiFiTracker(); + + uint8_t write_sdcard(char c); + uint8_t write_sdcard(char* str, int len); + + char read(); + int write(char); + int find(char*); + + + int char_cnt(char*, char); + int char_change(char*, char, char); + + void LedBlink(int, int); + +private: + RingBuffer buffer; + Serial dbg; + SDFileSystem sd; + FILE *fp; + DigitalOut led; + + uint32_t g_store_cnt; + +}; +
diff -r 000000000000 -r 0073c8def9f1 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon May 09 00:13:40 2016 +0000 @@ -0,0 +1,94 @@ +#include "mbed.h" +#include "WiFiTracker.h" + +Serial wifi(P23, P24); + +int8_t send_command(char*); +void init_tracker(); +int tracking(); + +WiFiTracker tracker; + +int main() { + + wifi.baud(9600); + + printf("Start Tracking...\n"); + + init_tracker(); + + while(1) + { + tracking(); + } + +} + + +// 0x4000/04:1b:ba:21:6d:a1/-66 +int tracking() +{ + char buf[30] = {0,}; + int i=0; + + while(1) + { + if(wifi.readable()) + { + buf[i++] = wifi.getc(); + + if(strstr(buf,"\r\n")) + { + tracker.write_sdcard(buf, i); + return 0; + } + } + } +} + + +void init_tracker() +{ + + //char cmd[] = "AT+MWPACKET=3,1,1,0,4000,,11010111"; + char cmd[] = "AT+MWPACKET=3,1,3,0,4000,,10010001"; + int8_t ret = -1; + + + ret = send_command(cmd); + printf("init ret: %d",ret); + + +} + +int8_t send_command(char* cmd) +{ + int len = strlen(cmd); + int i; + + for(i=0; i<len; i++) + wifi.putc(cmd[i]); + + wifi.putc('\r'); + wifi.putc('\n'); + + return 0; +} + +//int8_t wait_response(char* rsp, uint32_t timeout) +//{ +// char buf[256] = {0,}; +// char c; +// Timer t; +// uint32_t time; +// t.start(); +// +// while(1) +// { +// if(wifi.readable()) +// tracker.write(wifi.getc()); +// +// // 수정해야 +// +// } +//}
diff -r 000000000000 -r 0073c8def9f1 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon May 09 00:13:40 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/aae6fcc7d9bb \ No newline at end of file