12
Dependencies: BLE_API mbed nRF51822
Revision 17:6abf0e1667a1, committed 2017-10-19
- Comitter:
- oguzhan19
- Date:
- Thu Oct 19 11:23:08 2017 +0000
- Parent:
- 16:bd87bb636883
- Commit message:
- 12
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.cpp Thu Oct 19 11:23:08 2017 +0000 @@ -0,0 +1,245 @@ +#include "MbedJSONValue.h" + +# include <stdlib.h> +# include <stdio.h> + +// Clean up +void MbedJSONValue::clean() { + switch (_type) { + case TypeString: + delete _value.asString; + break; + case TypeArray: + for (int i = 0; i < index_array; i++) + delete array[i]; + index_array = 0; + break; + case TypeObject: + for (int i = 0; i < index_token; i++) { + delete token[i]; + delete token_name[i]; + } + index_token = 0; + break; + default: + break; + } + _type = TypeNull; + _type = TypeNull; +} + +bool MbedJSONValue::hasMember(char * name) +{ + for(int i = 0; i < index_token; i++) + if( !strcmp(name, (*(token_name[i])).c_str() )) + return true; + return false; +} + + +void copy(const std::string& s, std::back_insert_iterator<std::string> oi) { + std::copy(s.begin(), s.end(), oi); +} + +void serialize_str(const std::string& s, std::back_insert_iterator<std::string> oi) { + *oi++ = '"'; + for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { + switch (*i) { +#define MAP(val, sym) case val: copy(sym, oi); break + MAP('"', "\\\""); + MAP('\\', "\\\\"); + MAP('/', "\\/"); + MAP('\b', "\\b"); + MAP('\f', "\\f"); + MAP('\n', "\\n"); + MAP('\r', "\\r"); + MAP('\t', "\\t"); +#undef MAP + default: + if ((unsigned char)*i < 0x20 || *i == 0x7f) { + char buf[7]; + sprintf(buf, "\\u%04x", *i & 0xff); + copy(buf, buf + 6, oi); + } else { + *oi++ = *i; + } + break; + } + } + *oi++ = '"'; +} + +std::string MbedJSONValue::serialize(){ + std::string s; + serialize(std::back_inserter(s)); + return s; +} + +std::string MbedJSONValue::to_str(){ + switch (_type) { + case TypeNull: + return "null"; + case TypeBoolean: + return _value.asBool ? "true" : "false"; + case TypeInt: { + char buf[10]; + sprintf(buf, "%d", _value.asInt); + return buf; + } + case TypeDouble: { + char buf[10]; + sprintf(buf, "%f", _value.asDouble); + return buf; + } + default: + break; + } + return NULL; +} + + + +void MbedJSONValue::serialize(std::back_insert_iterator<std::string> oi) { + switch (_type) { + case TypeString: + serialize_str(*_value.asString, oi); + break; + case TypeArray: { + *oi++ = '['; + for (int i = 0; i < index_array; i++) { + if (i) + *oi++ = ','; + (*this)[i].serialize(oi); + } + *oi++ = ']'; + break; + } + case TypeObject: { + *oi++ = '{'; + for (int i = 0; i < index_token; i++) { + if (i) + *oi++ = ','; + serialize_str(*(token_name[i]), oi); + *oi++ = ':'; + (*(token[i])).serialize(oi); + } + *oi++ = '}'; + break; + } + default: + copy(to_str(), oi); + break; + } +} + + + +MbedJSONValue& MbedJSONValue::operator[](int i) { + _type = TypeArray; + if (i < NB_TOKEN && index_array == i ) { +#ifdef DEBUG + printf("will add an element to the array\r\n"); +#endif + array[i] = new MbedJSONValue(); + index_array++; + return *(array[i]); + } + if (i < NB_TOKEN && index_array > i) + return *(array[i]); + + //if the user is not doing something wrong, this code is never executed!! + return *(new MbedJSONValue()); +} + +MbedJSONValue& MbedJSONValue::operator[](std::string k) { + _type = TypeObject; + for (int i = 0; i < index_token; i++) { +#ifdef DEBUG + printf("k: %s\r\n", k.c_str()); + printf("str: %s\r\n", token_name[i]->c_str()); +#endif + //existing token + if (!strcmp(k.c_str(), token_name[i]->c_str())) { +#ifdef DEBUG + printf("token found: %d\r\n", i); +#endif + return *(token[i]); + } + } + + if(index_token >= NB_TOKEN) + index_token = NB_TOKEN - 1; + //non existing token + token_name[index_token] = new std::string(k); + token[index_token] = new MbedJSONValue(); + index_token++; + return *(token[index_token - 1]); +} + +MbedJSONValue& MbedJSONValue::operator[](std::string k) const +{ + for (int i = 0; i < index_token; i++) { +#ifdef DEBUG + printf("k: %s\r\n", k.c_str()); + printf("str: %s\r\n", token_name[i]->c_str()); +#endif + if (!strcmp(k.c_str(), token_name[i]->c_str())) { +#ifdef DEBUG + printf("token found: %d\r\n", i); +#endif + return *(token[i]); + } + } + + //if the user is not doing something wrong, this code is never executed!! + return *(new MbedJSONValue()); +} + + +// Operators +MbedJSONValue& MbedJSONValue::operator=(MbedJSONValue const& rhs) { + if (this != &rhs) { + clean(); + _type = rhs._type; + switch (_type) { + case TypeBoolean: + _value.asBool = rhs._value.asBool; + break; + case TypeInt: + _value.asInt = rhs._value.asInt; + break; + case TypeDouble: + _value.asDouble = rhs._value.asDouble; + break; + case TypeString: + _value.asString = new std::string(*rhs._value.asString); + break; + case TypeArray: + for (int i = 0; i < rhs.index_array; i++) + (*this)[i] = rhs[i]; + case TypeObject: + for (int i = 0; i < rhs.index_token; i++) + (*this)[*(rhs.token_name[i])] = rhs[*(rhs.token_name[i])]; + default: + break; + } + } + return *this; +} + + +// Works for strings, arrays, and structs. +int MbedJSONValue::size() const { + switch (_type) { + case TypeString: + return int(_value.asString->size()); + case TypeArray: + return index_array; + case TypeObject: + return index_token; + default: + break; + } + return -1; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.h Thu Oct 19 11:23:08 2017 +0000 @@ -0,0 +1,591 @@ +/** +* @author Samuel Mokrani +* +* @section LICENSE +* +* Copyright (c) 2011 mbed +* +* 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. +* +* @section DESCRIPTION +* Types Abstraction. Minimalist JSON serializer and parser (inspired by picojson). Is used by MbedJSONRpc. +* +*/ + +#ifndef _Mbed_RPC_VALUE_H_ +#define _Mbed_RPC_VALUE_H_ + +#define NB_TOKEN 20 +/*!< Number maximum of MbedJSONValue in an array or an object */ + +#include <string> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/** MbedJSONValue class + * + * Example: + * - creation of an MbedJSONValue of type TypeObject containing two others MbedJSONValue: + * -one array of one string and one integer identified by "my_array" + * -a boolean identified by "my_boolean" + * - serialization in JSON format of this object + * @code + * #include "mbed.h" + * #include "MbedJSONValue.h" + * #include <string> + * + * int main() { + * + * MbedJSONValue demo; + * std::string s; + * + * //fill the object + * demo["my_array"][0] = "demo_string"; + * demo["my_array"][1] = 10; + * demo["my_boolean"] = false; + * + * //serialize it into a JSON string + * s = demo.serialize(); + * printf("json: %s\r\n", s.c_str()); + * } + * + * @endcode + * + * Example: + * - creation of an MbedJSONValue from a JSON string + * - extraction of different values from this existing MbedJSONValue + * @code + * #include "mbed.h" + * #include "MbedJSONValue.h" + * #include <string> + * + * int main() { + * MbedJSONValue demo; + * + * const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}"; + * + * //parse the previous string and fill the object demo + * parse(demo, json); + * + * std::string my_str; + * int my_int; + * bool my_bool; + * + * my_str = demo["my_array"][0].get<std::string>(); + * my_int = demo["my_array"][1].get<int>(); + * my_bool = demo["my_boolean"].get<bool>(); + * + * printf("my_str: %s\r\n", my_str.c_str()); + * printf("my_int: %d\r\n", my_int); + * printf("my_bool: %s\r\n", my_bool ? "true" : "false"); + * } + * @endcode + */ +class MbedJSONValue { +public: + + /** + * \enum Type + * \brief All types which can be used + */ + enum Type { + TypeNull, /*!< Null type */ + TypeBoolean, /*!< Boolean */ + TypeInt, /*!< Integer */ + TypeDouble, /*!< Double */ + TypeString, /*!< String */ + TypeArray, /*!< Array (contains MbedJSONValue) */ + TypeObject /*!< Object (contains MbedJSONValue identified by a name)*/ + }; + + /** + * MbedJSONValue constructor of type TypeNull + */ + MbedJSONValue() : _type(TypeNull), index_array(0), index_token(0) {} + + /** + * MbedJSONValue constructor of type TypeBoolean + * + * @param value the object created will be initialized with this boolean + */ + MbedJSONValue(bool value) : _type(TypeBoolean), index_array(0), index_token(0) { + _value.asBool = value; + } + + /** + * MbedJSONValue constructor of type TypeInt + * + * @param value the object created will be initialized with this integer + */ + MbedJSONValue(int value) : _type(TypeInt), index_array(0), index_token(0) { + _value.asInt = value; + } + + /** + * MbedJSONValue constructor of type TypeDouble + * + * @param value the object created will be initialized with this double + */ + MbedJSONValue(double value) : _type(TypeDouble), index_array(0), index_token(0) { + _value.asDouble = value; + } + + /** + * MbedJSONValue constructor of type TypeString + * + * @param value the object created will be initialized with this string + */ + MbedJSONValue(std::string const& value) : _type(TypeString), index_array(0), index_token(0) { + _value.asString = new std::string(value); + } + + /** + * MbedJSONValue constructor of type TypeString + * + * @param value the object created will be initialized with this string + */ + MbedJSONValue(const char* value) : _type(TypeString), index_array(0), index_token(0) { + _value.asString = new std::string(value); + } + + /** + * Copy constructor + * + * @param rhs object which will be copied + */ + MbedJSONValue(MbedJSONValue const& rhs) : _type(TypeNull) { *this = rhs; } + + /** + * Destructor + */ + ~MbedJSONValue() { clean(); } + + /** + * = Operator overloading for an MbedJSONValue from an MbedJSONValue + * + * @param rhs object + * @return a reference on the MbedJSONValue affected + */ + MbedJSONValue& operator=(MbedJSONValue const & rhs); + + /** + * = Operator overloading for an MbedJSONValue from an int + * + * @param rhs integer + * @return a reference on the MbedJSONValue affected + */ + MbedJSONValue& operator=(int const& rhs) { return operator=(MbedJSONValue(rhs)); } + + /** + * = Operator overloading for an MbedJSONValue from a boolean + * + * @param rhs boolean + * @return a reference on the MbedJSONValue affected + */ + MbedJSONValue& operator=(bool const& rhs) { return operator=(MbedJSONValue(rhs)); } + + /** + * = Operator overloading for an MbedJSONValue from a double + * + * @param rhs double + * @return a reference on the MbedJSONValue affected + */ + MbedJSONValue& operator=(double const& rhs) { return operator=(MbedJSONValue(rhs)); } + + /** + * = Operator overloading for an MbedJSONValue from a string + * + * @param rhs string + * @return a reference on the MbedJSONValue affected + */ + MbedJSONValue& operator=(const char* rhs) { return operator=(MbedJSONValue(std::string(rhs))); } + + + /** + * [] Operator overloading for an MbedJSONValue. + * Each TypeObject object can contain an array of NB_TOKEN MbedJSONValue. + * This operator is useful to create an array or to retrieve an MbedJSONValue of an existing array. + * + * @param i index of the array + * @return a reference on the MbedJSONValue created or retrieved + */ + MbedJSONValue& operator[](int i); + + /** + * [] Operator overloading for an MbedJSONValue. + * Each TypeObject MbedJSONValue can contain NB_TOKEN MbedJSONValue IDENTIFIED BY A NAME + * This operator is useful to create a TypeObject MbedJSONValue or to retrieve an MbedJSONValue of an existing TypeObject. + * + * + * @param str identifier of the sub MbedJSONValue + * @return a reference on the MbedJSONValue created or retrieved + */ + MbedJSONValue& operator[](std::string str); + + /** + * Retrieve the value of an MbedJSONValue object. + * + * Let's suppose that we have an MbedJSONValue of type TypeString. + * To retrieve this string, you have to do: + * my_obj.get<std::string>(); + * + * @return A contant reference on the value of the object + */ + template <typename T> const T& get() const; + + /** + * Retrieve the value of an MbedJSONValue object. + * + * Let's suppose that we have an MbedJSONValue of type TypeInt. + * To retrieve this integer, you have to do: + * my_obj.get<int>(); + * + * @return A reference on the value of the object + */ + template <typename T> T& get(); + + + /** + * Return the type of the MbedJSONValue object + * + * @return type of the MbedJSONValue object + */ + Type const &getType() const { + return _type; + } + + /** + * Return the size of an MbedJSONValue object (works for TypeString, TypeArray or TypeObject) + * + * @return size + */ + int size() const; + + /** + * Check for the existence in a TypeObject object of member identified by name + * + * @param name Identifier + * @return true if the object is of type TypeObject AND contains a member named "name", false otherwise + */ + bool hasMember(char * name); + + /** + * Convert an MbedJSONValue in a JSON frame + * + * @return JSON string + */ + std::string serialize(); + +protected: + + // object type + Type _type; + + //indexes of TypeObject and TypeArray + int index_array; + int index_token; + + //an object can contain NB_TOKEN tokens. Each token have a name + MbedJSONValue * token[NB_TOKEN]; + std::string * token_name[NB_TOKEN]; + + //an object can contain an array of NB_TOKEN elements + MbedJSONValue * array[NB_TOKEN]; + + // Clean up + void clean(); + + union { + bool asBool; + int asInt; + double asDouble; + std::string* asString; + } _value; + + + MbedJSONValue& operator[](int i) const { return *(array[i]); } + MbedJSONValue& operator[](std::string k) const; + + std::string to_str(); + void serialize(std::back_insert_iterator<std::string> os); + +}; + + +#define GET(ctype, var) \ + template <> inline const ctype& MbedJSONValue::get<ctype>() const { \ + return var; \ + } \ + template <> inline ctype& MbedJSONValue::get<ctype>() { \ + return var; \ + } +GET(bool, _value.asBool) +GET(double, _value.asDouble) +GET(int, _value.asInt) +GET(std::string, *_value.asString) +#undef GET + + +//Input class for JSON parser +class input { +protected: + const char * cur_; + const char * end_; + int last_ch_; + bool ungot_; + int line_; +public: + input(const char * first, const char * last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {}; + + int getc() { + if (ungot_) { + ungot_ = false; + return last_ch_; + } + if (cur_ == end_) { + last_ch_ = -1; + return -1; + } + if (last_ch_ == '\n') { + line_++; + } + last_ch_ = *cur_++ & 0xff; + return last_ch_; + } + + void ungetc() { + if (last_ch_ != -1) { + ungot_ = true; + } + } + + const char * cur() const { + return cur_; + } + int line() const { + return line_; + } + void skip_ws() { + while (1) { + int ch = getc(); + if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { + ungetc(); + break; + } + } + } + int expect(int expect) { + skip_ws(); + if (getc() != expect) { + ungetc(); + return false; + } + return true; + } + bool match(const std::string& pattern) { + for (std::string::const_iterator pi(pattern.begin()); + pi != pattern.end(); + ++pi) { + if (getc() != *pi) { + ungetc(); + return false; + } + } + return true; + } +}; + + + +inline const char * parse(MbedJSONValue& out, const char * first, const char * last, std::string* err); + +/** +* JSON string parser and creation of an MbedJSONValue +* +* @param out reference of an MbedJSONValue which will be filled according to the JSON string +* @param str JSON string +* @return A non empty string if there is a parsing error +* +*/ + +inline std::string parse(MbedJSONValue& out, const char * str); +inline bool _parse(MbedJSONValue& out, input& in); +inline bool _parse_number(MbedJSONValue& out, input& in); +inline bool _parse_string(MbedJSONValue& out, input& in); +inline bool _parse_array(MbedJSONValue& out, input& in); +inline bool _parse_object(MbedJSONValue& out, input& in); + + +inline bool _parse_string(MbedJSONValue& out, input& in) { +#ifdef DEBUG + printf("string detected\r\n"); +#endif + out = MbedJSONValue(std::string("")); + std::string& s = out.get<std::string>(); + while (1) { + int ch = in.getc(); + if (ch < ' ') { + in.ungetc(); + return false; + } else if (ch == '"') { + return true; + } else if (ch == '\\') { + if ((ch = in.getc()) == -1) { + return false; + } + switch (ch) { +#define MAP(sym, val) case sym: s.push_back(val); break + MAP('"', '\"'); + MAP('\\', '\\'); + MAP('/', '/'); + MAP('b', '\b'); + MAP('f', '\f'); + MAP('n', '\n'); + MAP('r', '\r'); + MAP('t', '\t'); +#undef MAP + default: + return false; + } + } else { + s.push_back(ch); + } + } +} + +inline bool _parse_array(MbedJSONValue& out, input& in) { +#ifdef DEBUG + printf("array detected\r\n"); +#endif + int i = 0; + if (in.expect(']')) { + return true; + } + do { + if (! _parse(out[i], in)) { + return false; + } + i++; + } while (in.expect(',')); + return in.expect(']'); +} + +inline bool _parse_object(MbedJSONValue& out, input& in) { +#ifdef DEBUG + printf("object detected\r\n"); +#endif + if (in.expect('}')) { + return true; + } + do { + MbedJSONValue key, val; + if (in.expect('"') && _parse_string(key, in) && in.expect(':') && _parse(val, in)) { + out[key.get<std::string>().c_str()] = val; +#ifdef DEBUG + printf("key: %s \r\n", key.get<std::string>().c_str()); +#endif + } else { + return false; + } + } while (in.expect(',')); + return in.expect('}'); +} + +inline bool _parse_number(MbedJSONValue& out, input& in) { +#ifdef DEBUG + printf("number detected\r\n"); +#endif + std::string num_str; + while (1) { + int ch = in.getc(); + if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.' + || ch == 'e' || ch == 'E') { + num_str.push_back(ch); + } else { + in.ungetc(); + break; + } + } + char* endp; + if (strchr(num_str.c_str(), '.') != NULL || strchr(num_str.c_str(), 'e') != NULL || strchr(num_str.c_str(), '+') != NULL) + out = MbedJSONValue(strtod(num_str.c_str(), &endp)); + else + out = MbedJSONValue((int)strtod(num_str.c_str(), &endp)); + return endp == num_str.c_str() + num_str.size(); +} + +inline bool _parse(MbedJSONValue& out, input& in) { + in.skip_ws(); + int ch = in.getc(); + switch (ch) { +#define IS(ch, text, val) case ch: \ + if (in.match(text)) { \ + out = val; \ + return true; \ + } else { \ + return false; \ + } + IS('n', "ull", MbedJSONValue()); + IS('f', "alse", MbedJSONValue(false)); + IS('t', "rue", MbedJSONValue(true)); +#undef IS + case '"': + return _parse_string(out, in); + case '[': + return _parse_array(out, in); + case '{': + return _parse_object(out, in); + default: + if (('0' <= ch && ch <= '9') || ch == '-') { + in.ungetc(); + return _parse_number(out, in); + } + break; + } + in.ungetc(); + return false; +} + +inline std::string parse(MbedJSONValue& out, const char * pos) { + const char * last = pos + strlen(pos); + std::string err; + pos = parse(out, pos, last, &err); + return err; +} + +inline const char * parse(MbedJSONValue& out, const char * first, const char * last, std::string* err) { + input in = input(first, last); + if (! _parse(out, in) && err != NULL) { + char buf[64]; + sprintf(buf, "syntax error at line %d near: ", in.line()); + *err = buf; + while (1) { + int ch = in.getc(); + if (ch == -1 || ch == '\n') { + break; + } else if (ch >= ' ') { + err->push_back(ch); + } + } + } + return in.cur(); +} + +#endif // _MbedMbedJSONValue_H_
--- a/main.cpp Mon Oct 16 11:53:06 2017 +0000 +++ b/main.cpp Thu Oct 19 11:23:08 2017 +0000 @@ -5,7 +5,6 @@ #include "mbed.h" #include "toolchain.h" #include "ble/BLE.h" -#include <SPISlave.h> #include <string> #include <stdio.h> @@ -16,14 +15,10 @@ //#include <stdio.h> //#include <conio.h> #include <stdlib.h> - -//# CS P0_14 //? -//# SCK P0_25 //+ -//# MOSI P0_20 //+ -//# MISO P0_22 //+ -SPISlave device(p25, p28, p29, p24); // mosi, miso, sclk, ssel -//SPISlave device(p20, p22, p25, p14); // mosi, miso, sclk, ssel - +#include "MbedJSONValue.h" +MbedJSONValue demo; +std::string s; + DigitalIn esp_baglimi(P0_11); // ############################### DigitalOut alivenessLED(p2, 1); // ############################### @@ -38,14 +33,6 @@ uint8_t buffertoSpi[100]; uint8_t say=0; int boyut, byt, databoyut; - -//int y[8]= {0x08, 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x0F}; -///////int vx[15]= {15, 10, 2, 3, 4, 5, 6, 7,8,9,10,11,12,13, 15}; // ilk, boyut, data, 15 -//uint8_t z[8]= {0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; -//uint8_t w[8]= {8, 1, 2, 3, 4, 5, 6, 7}; -//v = {0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00}; -//v[] = [0x07; 0x01; 0x02; 0x03; 0x04; 0x05; 0x06; 0x07]; -//v[] = [7; 1; 2; 3; 4; 5; 6; 7]; int a, sayac = 1, sayac2 = 2, sayac3 = 0, sayac4 = 0; int gelen, gelene; @@ -58,201 +45,51 @@ uint8_t PAKET_LENGTH;//SPI -> STM32 GÖNDERİLCEK DATANIN TOPLAM UZUNLUGU. } PACKED_Data[ToplamCihaz]; +int *f; +int size=0; -void dataGndr(int DataLen, int *D){ - - T: - - byt = DataLen; +void enqueue(int data){ // kuyruğa ekle // data + int * b = (int * ) malloc(sizeof(int)*size+1); + int i=0; - int data[byt]; - //gelen = 5; - - //while(gelen != 3){ - - //gelen = 9; - - if(device.receive()){ - - yineal: + printf("\r\n\r\nENQ_Giris = data= %d %x", data, data); - gelen = device.read(); - //pc.printf("\r\nA = %d %x", a, a); - - //while(gelen != 3){ - - if(gelene != gelen){ - pc.printf("\r\n"); - } - gelene = gelen; - - //if(gelen != 9){ // ???? - - pc.printf("\r\nGelen = %d %x", gelen, gelen); - - if(gelen == 0){ - device.reply(0); - pc.printf("\r\ngelen = 0 !!!!!!!!!"); - //gelen = 9; - goto T; - //goto deneme; - //reset(); //?????????????????????????????????????????????? - } - else if(gelen == 1){ // (boyut) 1 ise boyutu (D[0]) gönder - device.reply(D[0]); - //wait_ms(500); - //pc.printf("\r\nGelen = %d %x", gelen, gelen); - pc.printf("\t\tData[0] (boyut) = %d %x", D[0], D[0]); - sayac = 1; - } - else if(gelen == 2){ // gitmesi gereken data // D[1] - D[boyut-1] arasını gönderir - device.reply(D[sayac]); - pc.printf("\t\tData[%d] = %d %x", sayac, D[sayac], D[sayac]); - sayac++; - int s = sayac; - boyut = D[0]; - if(s == (boyut)){ // boyut -1'e kadar olmalı - s = 1; - sayac = 1; - } - } - else if(gelen == 3){ - //gelen = 9; - device.reply(0); - pc.printf("connection out\r\n"); - wait_ms(100); - goto T; - } - else{ - pc.printf("goto al\r\n"); - goto yineal; - } - - //} // if gelen != 9 - } // if device.recieve - else - { - pc.printf("\r\nMaster bagli degil\r\n\r\n"); - led1 != led1; - //b = 0; - wait_ms(500); - } + for(i = 0; i<size; i++){ + b[i]=f[i]; + printf("\r\nENQ_for b[i] = %d %x \t_data = %d %x", b[i], b[i], data, data); + } - //} // while -} // data gönder - - -//Flash_Buff_WriteBytes(0, buffertoSpi, bufferSize); -//Flash_Buff_WriteBytes(bufferSize, buffertoSpi); -//void Flash_Buff_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t len) -/*void Flash_Buff_WriteBytes(uint8_t len, uint8_t *gndr) -{ - tekrar: - int v[len]; + b[i]=data; + //b[i]=*data; + + printf("\r\n\r\nENQ_ i = %d b[i] = %d %x \tENQ_data_son = %d %x", i, b[i], b[i], data, data); - //pc.printf("\r\n\r\nlen = %d %x\tGndr = %d %x\t**Gndr = %d %x\t&Gndr = %d %x", len, len, gndr, gndr, *gndr, *gndr, &gndr, &gndr); - for(int n = 0; n <60; n++){ - //pc.printf("\r\n%d\tlen = %d %x\tGndr = %d %x\t**Gndr = %d %x\t&Gndr = %d %x", n, len, len, gndr, gndr, *gndr, *gndr, &gndr, &gndr); - int d = n+2; - if(d < (len-1)){ // 2-58 arası - v[d] = *gndr; - } - - gndr++; - //wait_ms(500); - } - - //wait_ms(500); - - v[0] = len; - v[1] = 10; - pc.printf("\r\n"); - for(int g = 2; g < (len-1); g++){ - //pc.printf("\r\nV[g] = %d %x", v[g], v[g]); - } - v[(len-1)] = 15; // dizinin son elemanı + size++; + f=b; +} + +int dequeue(){ // kuyruktan çıkar + if(size<=0) + return -1; - pc.printf("\r\n\r\n"); - for(int j = 0; j < len; j++){ - pc.printf("\r\n%d\tDATAM[%d] = %d %x", j, j, v[j], v[j]); - } - pc.printf("\r\n\r\n"); - - //for(int f = 0; f < len; f++){ - //wait_ms(500); - //pc.printf("\r\ndata2[len] = %d %x \tGndr = %d %x\t**Gndr = %d %x\t&&Gndr = %d %x", v[len], v[len], gndr, gndr, *gndr, *gndr, &gndr, &gndr); - //} - - return(len, v); - //dataGndr(len, v); - //tekrar: - - //byt = len; - //int data[byt]; -/ * - gelen = 5; - - while(gelen != 3){ - - gelen = 9; - - if(device.receive()){ + int * b = (int * ) malloc(sizeof(int)*size-1); - gelen = device.read(); - //pc.printf("\r\nA = %d %x", a, a); - - //while(gelen != 3){ - - if(gelene != gelen){ - pc.printf("\r\n"); - } - gelene = gelen; - - if(gelen != 9){ // ???? - - pc.printf("\r\nGelen = %d %x", gelen, gelen); - - if(gelen == 0){ - wait_ms(500); - gelen = 9; - //return; - goto tekrar; - //reset(); //?????????????????????????????????????????????? - } - else if(gelen == 1){ // (boyut) 1 ise boyutu (D[0]) gönder - device.reply(v[0]); - //wait_ms(500); - //pc.printf("\r\nGelen = %d %x", gelen, gelen); - pc.printf("\t\tData[0] (boyut) = %d %x", v[0], v[0]); - sayac = 1; - } - else if(gelen == 2){ // gitmesi gereken data // D[1] - D[boyut-1] arasını gönderir - device.reply(v[sayac]); - pc.printf("\t\tData[%d] = %d %x", sayac, v[sayac], v[sayac]); - sayac++; - int s = sayac; - boyut = v[0]; - if(s == boyut){ // boyut -1'e kadar olmalı - s = 1; - sayac = 1; - } - } - - } // if gelen != 9 - } // if device.recieve - else - { - pc.printf("\r\nMaster bagli degil"); - led1 != led1; - //b = 0; - wait_ms(500); - } - - } // while -* / + for(int i = 0;i<size-1;i++){ + b[i]=f[i+1]; + } + + int result=f[0]; + size--; + f=b; + return result; } -*/ - + +void printqueue(){ + printf("\n"); + for(int i = 0;i<size;i++) + printf("%d_",f[i]); +} + void periodicCallback(void) { // Do blinky on LED1 while we're waiting for BLE events. This is optional. @@ -370,7 +207,12 @@ for (unsigned a = 0; a < bufferSize; a++) { - pc.printf("%02x",buffertoSpi[a]); + pc.printf("\r\n%02x",buffertoSpi[a]); + + //string str[] = buffertoSpi[a]; + pc.printf("\r\n%02x",buffertoSpi[a]); + demo["my_array"][0] = "demo_string"; + //enqueue(); } pc.printf("\r\n"); @@ -382,7 +224,7 @@ //Flash_Buff_WriteBytes(0, buffertoSpi, bufferSize); // eski //Flash_Buff_WriteBytes(bufferSize, buffertoSpi); // BU! //databoyut = bufferSize; - dataGndr(bufferSize, buffertoSpi); + //dataGndr(bufferSize, buffertoSpi); wait_ms(20); bulunan_index[i]=0x00; @@ -436,16 +278,7 @@ pc.baud(115200); //local terminal baud pc.printf("\r\n\r\nSLAVE main \r\n"); - - - - - - //mac_id_gir(); - - //int v[byt]; - - device.reply(0x00); + //mac_id_gir(); //ticker.attach(periodicCallback, 1); // trigger sensor polling every 2 seconds @@ -458,256 +291,36 @@ BLE &ble = BLE::Instance(); ble.init(bleInitComplete); - while(true){ - - tekrar: - - //deneme: - //wait_ms(3000); - - ble.waitForEvent(); - -//tekrar: - - //byt = DataLen; - - //int data[databoyutu]; - int data[byt]; -// son - gelen = 5; - - while(gelen != 3){ - - gelen = 9; - - if(device.receive()){ - - gelen = device.read(); - //pc.printf("\r\nA = %d %x", a, a); - - //while(gelen != 3){ - - if(gelene != gelen){ - pc.printf("\r\n"); - } - gelene = gelen; - - if(gelen != 9){ // ???? - - pc.printf("\r\nGelen = %d %x", gelen, gelen); - - if(gelen == 0){ - wait_ms(500); - gelen = 9; - //return; - goto tekrar; - //reset(); //?????????????????????????????????????????????? - } - else if(gelen == 1){ // (boyut) 1 ise boyutu (D[0]) gönder - device.reply(data[0]); - //wait_ms(500); - //pc.printf("\r\nGelen = %d %x", gelen, gelen); - pc.printf("\t\tData[0] (boyut) = %d %x", data[0], data[0]); - sayac = 1; - } - else if(gelen == 2){ // gitmesi gereken data // D[1] - D[boyut-1] arasını gönderir - device.reply(data[sayac]); - pc.printf("\t\tData[%d] = %d %x", sayac, data[sayac], data[sayac]); - sayac++; - int s = sayac; - boyut = data[0]; - if(s == (boyut)){ // boyut -1'e kadar olmalı - s = 1; - sayac = 1; - } - } - - } // if gelen != 9 - } // if device.recieve - else - { - pc.printf("\r\nMaster bagli degil\r\n"); - led1 != led1; - //b = 0; - wait_ms(500); - } - - } // while 3 -// son -//* / - - //byt = DataLen; - -/***** - if(device.receive()){ - //while(device.receive()){ - AL: - gelen = device.read(); - //pc.printf("\r\nA = %d %x", a, a); - - if(gelene != gelen){ // gelen farklıysa boşluk bırak - gelene = gelen; - pc.printf("\r\n"); - } - //gelene = gelen; - - //if(gelen != 9){ - //if(gelen != 3){ - - pc.printf("\r\nGelen = %d %x", gelen, gelen); - - if(gelen == 0){ - //wait_ms(1000); - device.reply(0); - pc.printf("\r\ngelen = 0 !"); - //gelen = 9; - goto tekrar; - //goto deneme; - //reset(); //?????????????????????????????????????????????? - } - else if(gelen == 1){ // (boyut) 1 ise boyutu (vx[0]) gönder - device.reply(data[0]); - //wait_ms(500); - //pc.printf("\r\nGelen = %d %x", gelen, gelen); - pc.printf("\t\tData[0] (boyut) = %d %x", data[0], data[0]); - sayac = 1; - } - else if(gelen == 2){ // gitmesi gereken data // vx[1] - vx[boyut-1] arasını gönderir - device.reply(D[sayac]); - pc.printf("\t\tData[%d] = %d %x", sayac, data[sayac], data[sayac]); - sayac++; - int s = sayac; - boyut = data[0]; - if(s == (boyut)){ // boyut -1'e kadar olmalı - s = 1; - sayac = 1; - } - } - else if(gelen == 3){ - //gelen = 9; - device.reply(0); - pc.printf("connection out\r\n"); - wait_ms(100); - goto tekrar; - } - else{ - pc.printf("goto al\r\n"); - goto AL; - } - - //}// if gelen != 3; - / *else{ - gelen = 9; - device.reply(0); - wait_ms(500); - pc.printf("connection out\r\n"); - goto tekrar; - }* / - - - //} // if gelen != 9 - - //gelen = 9; - - }// if device.recieve - -// spi beacon gönderme -*********/ -/* -///////// SPI sabit data gönderme ÇALIŞAN: /////////// - //AL: - if(device.receive()){ - //while(device.receive()){ - AL: - gelen = device.read(); - //pc.printf("\r\nA = %d %x", a, a); - - if(gelene != gelen){ // gelen farklıysa boşluk bırak - gelene = gelen; - pc.printf("\r\n"); - } - //gelene = gelen; - - //if(gelen != 9){ - //if(gelen != 3){ + + + + //fill the object + //demo["my_array"][0] = "demo_string"; + demo["my_array"][1] = 10; + demo["my_boolean"] = false; + + //serialize it into a JSON string + s = demo.serialize(); + printf("json: %s\r\n", s.c_str()); - pc.printf("\r\nGelen = %d %x", gelen, gelen); - - if(gelen == 0){ - //wait_ms(1000); - device.reply(0); - pc.printf("\r\ngelen = 0 !"); - //gelen = 9; - goto tekrar; - //goto deneme; - //reset(); //?????????????????????????????????????????????? - } - else if(gelen == 1){ // (boyut) 1 ise boyutu (vx[0]) gönder - device.reply(vx[0]); - //wait_ms(500); - //pc.printf("\r\nGelen = %d %x", gelen, gelen); - pc.printf("\t\tvx[0] (boyut) = %d %x", vx[0], vx[0]); - sayac = 1; - } - else if(gelen == 2){ // gitmesi gereken data // vx[1] - vx[boyut-1] arasını gönderir - device.reply(vx[sayac]); - pc.printf("\t\tvx[%d] = %d %x", sayac, vx[sayac], vx[sayac]); - sayac++; - int s = sayac; - boyut = vx[0]; - if(s == (boyut)){ // boyut -1'e kadar olmalı - s = 1; - sayac = 1; - } - } - else if(gelen == 3){ - //gelen = 9; - device.reply(0); - pc.printf("connection out\r\n"); - wait_ms(100); - goto tekrar; - } - else{ - pc.printf("goto al\r\n"); - goto AL; - } - - //}// if gelen != 3; - / *else{ - gelen = 9; - device.reply(0); - wait_ms(500); - pc.printf("connection out\r\n"); - goto tekrar; - }* / - - - //} // if gelen != 9 - - //gelen = 9; - - }// if device.recieve - / *else if(device.receive() && gelen == 3){ - goto tekrar; - } - else if(gelen != 3){ - goto AL; - }* / - - else // bağlı değilse veriyi alana kaydet (fifo yap) - { - pc.printf("\r\nMaster bagli degil"); - led1 != led1; - wait_ms(200); - - } -*/ //spi bilinen data son + + + while(true){ + + ble.waitForEvent(); + + //enqueue(n); + //printf("\r\ndequeue -> %d",dequeue()) + + + } // while } // ###################### MAIN ############### +//############################# SLAVE ################################### -//########## SLAVE ########## +