![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mbed RPC_Serial for LabVIEW interface: http://mbed.org/cookbook/Interfacing-with-LabVIEW http://mbed.org/cookbook/Interfacing-Using-RPC Steps: *Compile RPC_Serial main *Copy bit file to mbed flash and reset *Run examples in LabVIEW
Dependencies: C12832 MMA7660 RPCInterface mbed
Fork of RPC_Serial by
Revision 1:e245b0b4d96c, committed 2015-05-13
- Comitter:
- JonathanCaes
- Date:
- Wed May 13 07:42:37 2015 +0000
- Parent:
- 0:3ffd66df9efb
- Commit message:
- RPC
Changed in this revision
diff -r 3ffd66df9efb -r e245b0b4d96c C12832.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.h Wed May 13 07:42:37 2015 +0000 @@ -0,0 +1,306 @@ +/* mbed library for the mbed Lab Board 128*32 pixel LCD + * use C12832 controller + * Copyright (c) 2012 Peter Drescher - DC2PD + * Released under the MIT License: http://mbed.org/license/mit + * + * 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. + */ + +#ifndef C12832_H +#define C12832_H + +#include "mbed.h" +#include "GraphicsDisplay.h" + + +/** optional Defines : + * #define debug_lcd 1 enable infos to PC_USB + */ + +// some defines for the DMA use +#define DMA_CHANNEL_ENABLE 1 +#define DMA_TRANSFER_TYPE_M2P (1UL << 11) +#define DMA_CHANNEL_TCIE (1UL << 31) +#define DMA_CHANNEL_SRC_INC (1UL << 26) +#define DMA_MASK_IE (1UL << 14) +#define DMA_MASK_ITC (1UL << 15) +#define DMA_SSP1_TX (1UL << 2) +#define DMA_SSP0_TX (0) +#define DMA_DEST_SSP1_TX (2UL << 6) +#define DMA_DEST_SSP0_TX (0UL << 6) + +/** Draw mode + * NORMAl + * XOR set pixel by xor the screen + */ +enum {NORMAL,XOR}; + +/** Bitmap + */ +struct Bitmap{ + int xSize; + int ySize; + int Byte_in_Line; + char* data; + }; + +class C12832 : public GraphicsDisplay +{ +public: + /** Create a C12832 object connected to SPI1 + * + */ + + C12832(PinName mosi, PinName sck, PinName reset, PinName a0, PinName ncs, const char* name = "LCD"); + + + /** Get the width of the screen in pixel + * + * @param + * @returns width of screen in pixel + * + */ + virtual int width(); + + /** Get the height of the screen in pixel + * + * @returns height of screen in pixel + * + */ + virtual int height(); + + /** Draw a pixel at x,y black or white + * + * @param x horizontal position + * @param y vertical position + * @param colour ,1 set pixel ,0 erase pixel + */ + virtual void pixel(int x, int y,int colour); + + /** draw a circle + * + * @param x0,y0 center + * @param r radius + * @param colour ,1 set pixel ,0 erase pixel + * + */ + void circle(int x, int y, int r, int colour); + + /** draw a filled circle + * + * @param x0,y0 center + * @param r radius + * @param color ,1 set pixel ,0 erase pixel + * + * use circle with different radius, + * can miss some pixel + */ + void fillcircle(int x, int y, int r, int colour); + + /** draw a 1 pixel line + * + * @param x0,y0 start point + * @param x1,y1 stop point + * @param color ,1 set pixel ,0 erase pixel + * + */ + void line(int x0, int y0, int x1, int y1, int colour); + + /** draw a rect + * + * @param x0,y0 top left corner + * @param x1,y1 down right corner + * @param color 1 set pixel ,0 erase pixel + * * + */ + void rect(int x0, int y0, int x1, int y1, int colour); + + /** draw a filled rect + * + * @param x0,y0 top left corner + * @param x1,y1 down right corner + * @param color 1 set pixel ,0 erase pixel + * + */ + void fillrect(int x0, int y0, int x1, int y1, int colour); + + /** copy display buffer to lcd + * + */ + + void copy_to_lcd(void); + + /** set the orienation of the screen + * + */ + + + void set_contrast(unsigned int o); + + /** read the contrast level + * + */ + unsigned int get_contrast(void); + + + /** invert the screen + * + * @param o = 0 normal, 1 invert + */ + void invert(unsigned int o); + + /** clear the screen + * + */ + virtual void cls(void); + + /** set the drawing mode + * + * @param mode NORMAl or XOR + */ + + void setmode(int mode); + + virtual int columns(void); + + /** calculate the max number of columns + * + * @returns max column + * depends on actual font size + * + */ + virtual int rows(void); + + /** put a char on the screen + * + * @param value char to print + * @returns printed char + * + */ + virtual int _putc(int value); + + /** draw a character on given position out of the active font to the LCD + * + * @param x x-position of char (top left) + * @param y y-position + * @param c char to print + * + */ + virtual void character(int x, int y, int c); + + /** setup cursor position + * + * @param x x-position (top left) + * @param y y-position + */ + virtual void locate(int x, int y); + + /** setup auto update of screen + * + * @param up 1 = on , 0 = off + * if switched off the program has to call copy_to_lcd() + * to update screen from framebuffer + */ + void set_auto_up(unsigned int up); + + /** get status of the auto update function + * + * @returns if auto update is on + */ + unsigned int get_auto_up(void); + + /** Vars */ + SPI _spi; + DigitalOut _reset; + DigitalOut _A0; + DigitalOut _CS; + unsigned char* font; + unsigned int draw_mode; + + + /** select the font to use + * + * @param f pointer to font array + * + * font array can created with GLCD Font Creator from http://www.mikroe.com + * you have to add 4 parameter at the beginning of the font array to use: + * - the number of byte / char + * - the vertial size in pixel + * - the horizontal size in pixel + * - the number of byte per vertical line + * you also have to change the array to char[] + * + */ + void set_font(unsigned char* f); + + /** print bitmap to buffer + * + * @param bm Bitmap in flash + * @param x x start + * @param y y start + * + */ + + void print_bm(Bitmap bm, int x, int y); + +protected: + + /** draw a horizontal line + * + * @param x0 horizontal start + * @param x1 horizontal stop + * @param y vertical position + * @param ,1 set pixel ,0 erase pixel + * + */ + void hline(int x0, int x1, int y, int colour); + + /** draw a vertical line + * + * @param x horizontal position + * @param y0 vertical start + * @param y1 vertical stop + * @param ,1 set pixel ,0 erase pixel + */ + void vline(int y0, int y1, int x, int colour); + + /** Init the C12832 LCD controller + * + */ + void lcd_reset(); + + /** Write data to the LCD controller + * + * @param dat data written to LCD controller + * + */ + void wr_dat(unsigned char value); + + /** Write a command the LCD controller + * + * @param cmd: command to be written + * + */ + void wr_cmd(unsigned char value); + + void wr_cnt(unsigned char cmd); + + unsigned int orientation; + unsigned int char_x; + unsigned int char_y; + unsigned char buffer[512]; + unsigned int contrast; + unsigned int auto_up; + +}; + + + + +#endif
diff -r 3ffd66df9efb -r e245b0b4d96c C12832.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Wed May 13 07:42:37 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/JonathanCaes/code/C12832/#f20c0c9aaf99
diff -r 3ffd66df9efb -r e245b0b4d96c MMA7660.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7660.h Wed May 13 07:42:37 2015 +0000 @@ -0,0 +1,207 @@ +/* Copyright (c) <year> <copyright holders>, MIT License + * + * 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. + */ + +#include "mbed.h" + + +#ifndef MMA7660_H +#define MMA7660_H + +#define MMA7660_ADDRESS 0x98 +#define MMA7660_SENSITIVITY 21.33 + +#define MMA7660_XOUT_R 0x00 +#define MMA7660_YOUT_R 0x01 +#define MMA7660_ZOUT_R 0x02 +#define MMA7660_TILT_R 0x03 +#define MMA7660_INT_R 0x06 +#define MMA7660_MODE_R 0x07 +#define MMA7660_SR_R 0x08 + + +/** An interface for the MMA7660 triple axis accelerometer + * + * @code + * //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed + * + * #include "mbed.h" + * #include "MMA7660.h" + * + * MMA7660 MMA(p28, p27); + * + * DigitalOut connectionLed(LED1); + * PwmOut Zaxis_p(LED2); + * PwmOut Zaxis_n(LED3); + * + * int main() { + * if (MMA.testConnection()) + * connectionLed = 1; + * + * while(1) { + * Zaxis_p = MMA.z(); + * Zaxis_n = -MMA.z(); + * } + * + * } + * @endcode + */ +class MMA7660 +{ +public: + /** + * The 6 different orientations and unknown + * + * Up & Down = X-axis + * Right & Left = Y-axis + * Back & Front = Z-axis + * + */ + enum Orientation {Up, Down, + Right, Left, + Back, Front, + Unknown + }; + + /** + * Creates a new MMA7660 object + * + * @param sda - I2C data pin + * @param scl - I2C clock pin + * @param active - true (default) to enable the device, false to keep it standby + */ + MMA7660(PinName sda, PinName scl, bool active = true); + + /** + * Tests if communication is possible with the MMA7660 + * + * Because the MMA7660 lacks a WHO_AM_I register, this function can only check + * if there is an I2C device that responds to the MMA7660 address + * + * @param return - true for successfull connection, false for no connection + */ + bool testConnection( void ); + + /** + * Sets the active state of the MMA7660 + * + * Note: This is unrelated to awake/sleep mode + * + * @param state - true for active, false for standby + */ + void setActive( bool state); + + /** + * Reads acceleration data from the sensor + * + * When the parameter is a pointer to an integer array it will be the raw data. + * When it is a pointer to a float array it will be the acceleration in g's + * + * @param data - pointer to array with length 3 where the acceleration data will be stored, X-Y-Z + */ + void readData( int *data); + void readData( float *data); + + /** + * Get X-data + * + * @param return - X-acceleration in g's + */ + float x( void ); + + /** + * Get Y-data + * + * @param return - Y-acceleration in g's + */ + float y( void ); + + /** + * Get Z-data + * + * @param return - Z-acceleration in g's + */ + float z( void ); + + /** + * Sets the active samplerate + * + * The entered samplerate will be rounded to nearest supported samplerate. + * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second. + * + * @param samplerate - the samplerate that will be set + */ + void setSampleRate(int samplerate); + + /** + * Returns if it is on its front, back, or unknown side + * + * This is read from MMA7760s registers, page 12 of datasheet + * + * @param return - Front, Back or Unknown orientation + */ + Orientation getSide( void ); + + /** + * Returns if it is on it left, right, down or up side + * + * This is read from MMA7760s registers, page 12 of datasheet + * + * @param return - Left, Right, Down, Up or Unknown orientation + */ + Orientation getOrientation ( void ); + + +private: + + /** + * Writes data to the device + * + * @param adress - register address to write to + * @param data - data to write + */ + void write( char address, char data); + + /** + * Read data from the device + * + * @param adress - register address to write to + * @return - data from the register specified by RA + */ + char read( char adress); + + /** + * Read multiple regigsters from the device, more efficient than using multiple normal reads. + * + * @param adress - register address to write to + * @param length - number of bytes to read + * @param data - pointer where the data needs to be written to + */ + void read( char adress, char *data, int length); + + /** + * Reads single axis + */ + float getSingle(int number); + + I2C _i2c; + bool active; + float samplerate; +}; + + +#endif
diff -r 3ffd66df9efb -r e245b0b4d96c MMA7660.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7660.lib Wed May 13 07:42:37 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/JonathanCaes/code/MMA7660/#5ab73e26d2d4
diff -r 3ffd66df9efb -r e245b0b4d96c RPCInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RPCInterface.lib Wed May 13 07:42:37 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/JonathanCaes/code/RPCInterface/#c5ced46bff63
diff -r 3ffd66df9efb -r e245b0b4d96c SerialRPCInterface.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialRPCInterface.h Wed May 13 07:42:37 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 3ffd66df9efb -r e245b0b4d96c main.cpp --- a/main.cpp Sat Apr 21 00:47:46 2012 +0000 +++ b/main.cpp Wed May 13 07:42:37 2015 +0000 @@ -1,25 +1,90 @@ +//Uses the measured z-acceleration to drive leds 2 and 3 of the mbed + #include "mbed.h" -#include "rpc.h" -Serial pc(USBTX, USBRX); -int main() { - // setup the classes that can be created dynamically - Base::add_rpc_class<AnalogIn>(); - Base::add_rpc_class<AnalogOut>(); - 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<SPI>(); - Base::add_rpc_class<BusOut>(); - Base::add_rpc_class<BusIn>(); - Base::add_rpc_class<BusInOut>(); - Base::add_rpc_class<Serial>(); - // receive commands, and send back the responses - char buf[256], outbuf[256]; +//accelerometer header file +#include "MMA7660.h" +//LCD screen header file +#include "C12832.h" +//include PRPC command file +#include "SerialRPCInterface.h" + +//default pin connections for LCD +C12832 lcd(p5, p7, p6, p8, p11); +//default pins for accelerometer +MMA7660 MMA(p28, p27); +// +float X = 0; +float Y = 0; +float Z = 0; +float ai1=0; +float ai2=0; +int alarm1 = 0; +int alarm2 = 0; +RPCVariable<float> rpc_ai1(&ai1,"ai1"); + +RPCVariable<float> rpc_ai2(&ai2,"ai2"); + +RPCVariable<int> rpc_alarm1(&alarm1,"alarm1"); +RPCVariable<int> rpc_alarm2(&alarm2,"alarm2"); +AnalogIn pot2(p19); +DigitalOut led(LED1); +AnalogIn pot1(p20); +DigitalOut led2(LED2); + +RPCVariable<float> RPCX(&X, "X"); +RPCVariable<float> RPCY(&Y, "Y"); +RPCVariable<float> RPCZ(&Z, "Z"); +SerialRPCInterface SerieleInterface(USBTX, USBRX); +DigitalOut connectionLed(LED1); +//pwm led 1 and 2 +PwmOut Zaxis_p(LED2); +PwmOut Zaxis_n(LED3); +//pwm signals for RGB LED +PwmOut r (p23); +PwmOut g (p24); +PwmOut b (p25); + +int main() { + + //clear lcd screen + lcd.cls(); + //update axis period + r.period(0.001); + //test connection to board + if (MMA.testConnection()) + connectionLed = 1; + while(1) { - pc.gets(buf, 256); - rpc(buf, outbuf); - pc.printf("%s\n", outbuf); + Zaxis_p = MMA.z(); + Zaxis_n = -MMA.z(); + r = MMA.x(); + g = MMA.y(); + b = MMA.z(); + lcd.locate(0,0); + lcd.printf("X as : %.2f", MMA.x()); + lcd.locate(0,10); + lcd.printf("Y as : %.2f", MMA.y()); + lcd.locate(0,20); + lcd.printf("Z as : %.2f", MMA.z()); + X = MMA.x(); + Y = MMA.y(); + Z = MMA.z(); + ai1 = pot2; + ai2 = pot1; + if(pot2 > 0.3) { + led = 1; + alarm1 = led; + } else { + led = 0; + alarm1 = led; + } + if(pot1 > 0.7) { + led2 = 1; + alarm2 = led2; + } else { + led2 = 0; + alarm2 = led2; + } } -} + +} \ No newline at end of file