Nino Der Kinderen
/
nRF24L01P_ProjectSensor
se
Fork of nRF24L01P_Project by
Revision 5:87f98ed1ee9a, committed 2015-05-19
- Comitter:
- ninoderkinderen
- Date:
- Tue May 19 17:25:21 2015 +0000
- Parent:
- 4:25a500ef61fc
- Commit message:
- sens
Changed in this revision
diff -r 25a500ef61fc -r 87f98ed1ee9a RPCInterface/RPCFunction.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RPCInterface/RPCFunction.cpp Tue May 19 17:25:21 2015 +0000 @@ -0,0 +1,115 @@ + /** +* @section LICENSE +*Copyright (c) 2010 ARM Ltd. +* +*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 +*This class provides an object which can be called over RPC to run the function which is attached to it. +* +*/ +#include "RPCFunction.h" +#include "rpc.h" + +//Parse a char argument without delimiting by anything that is non alphanumeric - based on version in rpc.h line 153 + char *parse_arg_char(const char *arg, const char **next) { + const char *ptr = arg; + char *res = NULL; + if(*arg == '"') { + /* quoted string */ + ptr = ++arg; + int len = 0; + /* find the end (and length) of the quoted string */ + for(char c = *ptr; c != 0 && c != '"'; c = *++ptr) { + len++; + if(c == '\\') { + ptr++; + } + } + /* copy the quoted string, and unescape characters */ + if(len != 0) { + res = new char[len+1]; + char *resptr = res; + while(arg != ptr) { + *resptr++ = parse_char(arg, &arg); + } + *resptr = 0; + } + } else { + /* unquoted string */ + while(isalnum(*ptr) || isgraph(*ptr) || *ptr=='_' || *ptr == ' ') { //Edit this line to change which types of characters are allowed and which delimit + ptr++; + } + int len = ptr-arg; + if(len!=0) { //Chnages made to just pass whole string with no next arg or delimiters, these changes just removes space at the beginning + res = new char[len]; //was len+1 + memcpy(res, arg + 1, len - 1); // was arg, len + res[len-1] = 0; //was len + } + } + + if(next != NULL) { + *next = ptr; + } + return res; + } + + //Custom rpc method caller for execute so that the string will not be delimited by anything + //See line 436 of rpc.h + void rpc_method_caller_run(Base *this_ptr, const char *arguments, char *result) { + + const char *next = arguments; + char* arg1 = parse_arg_char(next,NULL); + + char * res = (static_cast<RPCFunction*>(this_ptr)->run)(arg1); + if(result != NULL) { + write_result<char*>(res, result); + } + delete arg1; // Seems to stop a memory leak issue which prevented an RPCFunction being run more than ~500 times + } + + RPCFunction::RPCFunction(void(*f)(char*, char*), const char* name) : Base(name){ + _ftr = f; + } + + + //Just run the attached function using the string thats in private memory - or just using null values, + char * RPCFunction::run(char * input){ + strcpy(_input, input); + (*_ftr)(_input,_output); + return(_output); + } + + //Just read the output string + char* RPCFunction::read(){ + return(_output); + } + + + #ifdef MBED_RPC + const rpc_method *RPCFunction::get_rpc_methods() { + static const rpc_method rpc_methods[] = { + { "run", rpc_method_caller_run }, //Run using custom caller, all characters accepted in string + { "read", rpc_method_caller<char*, RPCFunction, &RPCFunction::read> }, + RPC_METHOD_SUPER(Base) + }; + return rpc_methods; + } + +#endif
diff -r 25a500ef61fc -r 87f98ed1ee9a RPCInterface/RPCFunction.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RPCInterface/RPCFunction.h Tue May 19 17:25:21 2015 +0000 @@ -0,0 +1,84 @@ + /** +* @section LICENSE +*Copyright (c) 2010 ARM Ltd. +* +*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 +*This class provides an object which can be called over RPC to run the function which is attached to it. +* +*/ +#ifndef RPCFUNCTION_RPC +#define RPCFUNCTION_RPC +/** +*Includes +*/ +#include "mbed.h" +#include "platform.h" +#include "rpc.h" +#define STR_LEN 64 +#include "platform.h" + +#ifdef MBED_RPC +#include "rpc.h" +#endif +/** +* +*Class to call custom functions over RPC +* +*/ +class RPCFunction : public Base{ +public: + /** + * Constructor + * + *@param f Pointer to the function to call. the function must be of the form void foo(char * input, char * output) + *@param name The name of this object + */ + RPCFunction(void(*f)(char*, char*), const char* = NULL); + + /** + *run + * + *Calls the attached function passing the string in but doesn't return the result. + *@param str The string to be passed into the attached function. This string can consist of any ASCII characters apart from escape codes. The usual limtations on argument content for RPC strings has been removed + *@return A string output from the function + */ + char * run(char* str); + + /** + *Reads the value of the output string. + * + *@returns the string outputted from the last time the function was called + */ + char * read(); + + + #ifdef MBED_RPC + virtual const struct rpc_method *get_rpc_methods(); + #endif + +private: + void (*_ftr)(char*, char*); + + char _input[STR_LEN]; + char _output[STR_LEN]; + +}; +#endif \ No newline at end of file
diff -r 25a500ef61fc -r 87f98ed1ee9a RPCInterface/RPCVariable.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RPCInterface/RPCVariable.h Tue May 19 17:25:21 2015 +0000 @@ -0,0 +1,102 @@ + /** +* @section LICENSE +*Copyright (c) 2010 ARM Ltd. +* +*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 +*This class provides an object to which a variable can be attached. Any type +*for which a parse_args function specilisation exists can be attached. This includes +*all of the standard types. +* +*/ + #ifndef RPCVARIABLE_H_ + #define RPCVARIABLE_H_ + +#include "mbed.h" +#include "platform.h" +#include "rpc.h" + /** + *Class to read and set an attached variable using the RPC + * + */ +template<class T> +class RPCVariable : public Base{ +public: + /** + * Constructor + * + *@param ptr Pointer to the variable to make accessible over RPC. Any type of + *variable can be connected + *@param name The name of that this object will be over RPC + */ + template<class A> + RPCVariable(A * ptr, const char * name) : Base(name){ + _ptr = ptr; + } + /** + *Read the variable over RPC. + * + *@return The value of the variable + */ + T read(){ + return(*_ptr); + } + /** + *Write a value to the variable over RPC + * + *@param The value to be written to the attached variable. + */ + void write(T value){ + *_ptr = value; + } + + #ifdef MBED_RPC + virtual const struct rpc_method *get_rpc_methods(); + static struct rpc_class *get_rpc_class(); + #endif + +private: + T * _ptr; + +}; + +//Set up RPC methods +#ifdef MBED_RPC +template <class T> + const rpc_method *RPCVariable<T>::get_rpc_methods() { + static const rpc_method rpc_methods[] = { + { "read", rpc_method_caller<T, RPCVariable, &RPCVariable::read> }, + { "write", rpc_method_caller<RPCVariable, T, &RPCVariable::write> }, + RPC_METHOD_SUPER(Base) + }; + return rpc_methods; + } + template <class T> + rpc_class *RPCVariable<T>::get_rpc_class() { + static const rpc_function funcs[] = {"new", rpc_function_caller<const char*, T,const char* , &Base::construct<RemoteVar, T ,const char*> >,RPC_METHOD_END}; + static rpc_class c = { "RPCVariable", funcs, NULL }; + return &c; + } +#endif + +//There could be specialisation for integer, to also give increment and decrements + + +#endif //RPCVARIABLE_H_ \ No newline at end of file
diff -r 25a500ef61fc -r 87f98ed1ee9a RPCInterface/SerialRPCInterface.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RPCInterface/SerialRPCInterface.cpp Tue May 19 17:25:21 2015 +0000 @@ -0,0 +1,79 @@ + /** +* @section LICENSE +*Copyright (c) 2010 ARM Ltd. +* +*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 +* +*This class sets up RPC communication. This allows objects on mbed to be controlled. Objects can be created or existing objects can be used +*/ +#include "SerialRPCInterface.h" + +using namespace mbed; + +//Requires multiple contstructors for each type, serial to set different pin numbers, TCP for port. +SerialRPCInterface::SerialRPCInterface(PinName tx, PinName rx, int baud):pc(tx, rx) { + _RegClasses(); + _enabled = true; + pc.attach(this, &SerialRPCInterface::_RPCSerial, Serial::RxIrq); + if(baud != 9600)pc.baud(baud); +} + +void SerialRPCInterface::_RegClasses(void){ + //Register classes with base + Base::add_rpc_class<AnalogIn>(); + Base::add_rpc_class<DigitalIn>(); + Base::add_rpc_class<DigitalOut>(); + Base::add_rpc_class<DigitalInOut>(); + Base::add_rpc_class<PwmOut>(); + Base::add_rpc_class<Timer>(); + Base::add_rpc_class<BusOut>(); + Base::add_rpc_class<BusIn>(); + Base::add_rpc_class<BusInOut>(); + Base::add_rpc_class<Serial>(); + + //AnalogOut not avaliable on mbed LPC11U24 so only compile for other devices + #if !defined(TARGET_LPC11U24) + Base::add_rpc_class<AnalogOut>(); + #endif +} + +void SerialRPCInterface::Disable(void){ + _enabled = false; + } +void SerialRPCInterface::Enable(void){ + _enabled = true; +} +void SerialRPCInterface::_MsgProcess(void) { + if(_enabled == true){ + rpc(_command, _response); + } +} + +void SerialRPCInterface::_RPCSerial() { + _RPCflag = true; + if(_enabled == true){ + pc.gets(_command, 256); + _MsgProcess(); + pc.printf("%s\n", _response); + } + _RPCflag = false; +}
diff -r 25a500ef61fc -r 87f98ed1ee9a RPCInterface/SerialRPCInterface.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RPCInterface/SerialRPCInterface.h Tue May 19 17:25:21 2015 +0000 @@ -0,0 +1,90 @@ +/** +* @section LICENSE +*Copyright (c) 2010 ARM Ltd. +* +*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 +* +*This class sets up RPC communication over serial. +*/ +#ifndef INTERFACE +#define INTERFACE + +/** +*Includes +*/ +#include "mbed.h" +#include "platform.h" +#include "rpc.h" +#include "RPCFunction.h" +#include "RPCVariable.h" + + +namespace mbed{ +/** +*Provides an Interface to mbed over RPC. +* +*For the chosen communication type this class sets up the necessary interrupts to receive RPC messages. Receives the messages, passes them to the rpc function and then returns the result. +*/ +class SerialRPCInterface{ +public: + /** + *Constructor + * + *Sets up RPC communication using serial communication. + * + *@param tx The transmit pin of the serial port. + *@param rx The receive pin of the serial port. + *@param baud Set the baud rate, default is 9600. + */ + SerialRPCInterface(PinName tx, PinName rx, int baud = 9600); + + /** + *Disable the RPC. + * + *This will stop RPC messages being recevied and interpreted by this library. This might be used to prevent RPC commands interrupting an important piece of code on mbed. + */ + void Disable(void); + + /** + *Enable the RPC + * + *This will set this class to receiving and executing RPC commands. The class starts in this mode so this function only needs to be called if you have previosuly disabled the RPC. + * + */ + void Enable(void); + + //The Serial Port + Serial pc; + + +private: + //Handle messgaes and take appropriate action + void _MsgProcess(void); + void _RegClasses(void); + void _RPCSerial(); + bool _enabled; + char _command[256]; + char _response[256]; + bool _RPCflag; +}; +} +#endif \ No newline at end of file
diff -r 25a500ef61fc -r 87f98ed1ee9a main.cpp --- a/main.cpp Mon May 18 16:27:46 2015 +0000 +++ b/main.cpp Tue May 19 17:25:21 2015 +0000 @@ -10,20 +10,19 @@ AnalogIn LM35(p20); AnalogIn LDR(p19); -#define TRANSFER_SIZE 5 +#define TRANSFER_SIZE 6 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; -int txDataCnt = 5; +int txDataCnt = 6; int rxDataCnt = 0; float Light = 0; float TemperatureC = 0; -char a[5]; - -float i = 0; -float j = 0; - +char a[6]; //temp +char b[6];//licht +float i = 0;//temp +float j = 0;//licht int main() { @@ -35,19 +34,6 @@ my_nrf24l01p.setRfFrequency(2450); my_nrf24l01p.setRfOutputPower(0); - - //my_nrf24l01p.enableAutoRetransmit(1000, 5); //enableAutoRetransmit ( int delay, int count); - // delay the delay between restransmits, in uS (250uS..4000uS) - // count number of retransmits before generating an error (1..15) - /* - //Enable autoAcknowledge - my_nrf24l01p.enableAutoAcknowledge(0); - my_nrf24l01p.enableAutoAcknowledge(1); - my_nrf24l01p.enableAutoAcknowledge(2); - my_nrf24l01p.enableAutoAcknowledge(3); - my_nrf24l01p.enableAutoAcknowledge(4); - my_nrf24l01p.enableAutoAcknowledge(5); - */ // Display the (default) setup of the nRF24L01+ chip pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); @@ -60,81 +46,69 @@ my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); - //Put device in transmitmode + //Put device in receivemode my_nrf24l01p.setReceiveMode(); my_nrf24l01p.enable(); while (1) { + wait(0.2); i = 0; j = 0; + txDataCnt = 6; for (int t = 0; t < 100; t++) { i = i + LM35.read(); } - - pc.printf("Temperatuur in %4.1f graden C \n\r", i * 3.3); - wait(1); - - // If we've received anything over the host serial link. - - // ...add it to the transmit buffer - sprintf(a, "%f", i * 3.3); + for (int y = 0; y < 100; y++) + { + j = j + LDR.read(); + } + pc.printf("Temperatuur in %4.1f graden C \n\r", i * 3.3); + pc.printf("Lichtsterkte in %4.1f procent \n\r", j); + + sprintf(a, "%f", i * 3.3); //float to char + sprintf(b, "%f", j); //float to char + /* pc.printf("a %f \n\r", a); pc.printf("a1 %c \n\r", a[0]); pc.printf("a2 %c \n\r", a[1]); pc.printf("a3 %c \n\r", a[2]); pc.printf("a4 %c \n\r", a[3]); pc.printf("a5 %c \n\r", a[4]); - + */ for(int x = 0; x <=5; x++) { txData[x] = a[x]; - - pc.printf("txData[0] %c \n\r", txData[0]); + /*pc.printf("txData[0] %c \n\r", txData[0]); pc.printf("txData[1] %c \n\r", txData[1]); pc.printf("txData[2] %c \n\r", txData[2]); pc.printf("txData[3] %c \n\r", txData[3]); - pc.printf("txData[4] %c \n\r", txData[4]); - + pc.printf("txData[4] %c \n\r", txData[4]); */ } - - - - //pc.printf("lengte txData: %d \n\r", sizeof(txData)); - //pc.printf("txData count: %d \n\r", txDataCnt); - - // If the transmit buffer is full - //if ( txDataCnt >= sizeof(txData) ) { - //pc.printf("eerste if \n\r"); - // Send the transmitbuffer via the nRF24L01+ + txData[5] = 'T'; + // Toggle LED1 debug(Temperatuur) + myled1 = !myled1; my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); - pc.printf("Ik heb verzonden"); - txDataCnt = 5; - //} - - // Toggle LED1 (to help debug Host -> nRF24L01+ communication) - myled1 = !myled1; - - - /*// If we've received anything in the nRF24L01+... - if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) { - //pc.printf("tweede if \n\r"); - // ...read the data into the receive buffer - rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); - - // Display the receive buffer contents via the host serial link - for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { - - pc.putc( rxData[i] ); - } - - // Toggle LED2 (to help debug nRF24L01+ -> Host communication) - myled2 = !myled2; - }*/ + pc.printf("Ik heb mijn temperatuur verzonden\r\n"); + wait(0.3); + for(int x = 0; x <=5; x++) + { + txData[x] = b[x]; + /*pc.printf("txData[0] %c \n\r", txData[0]); + pc.printf("txData[1] %c \n\r", txData[1]); + pc.printf("txData[2] %c \n\r", txData[2]); + pc.printf("txData[3] %c \n\r", txData[3]); + pc.printf("txData[4] %c \n\r", txData[4]); */ + } + txData[5] = 'L'; + my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); + pc.printf("Ik heb mijn lichtwaarde verzonden\r\n"); + // Toggle LED2 debug(lichtwaarde) + myled2 = !myled2; } } \ No newline at end of file