Simple Electronic Angle Meter and Spirit Level.

Dependencies:   MMA8452 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.h Source File

main.h

Go to the documentation of this file.
00001 /**
00002 @file main.h
00003 @brief Header file containing functions prototypes, defines and global variables.
00004 @brief Edited and compiled by Dr.Craig A. Evans.
00005 @brief Revision 1.0.
00006 @author Shengyuan Chu
00007 @date May 2015
00008 */
00009 
00010 #ifndef N5110_H
00011 #define N5110_H
00012 
00013 // Command Bytes - taken from Chris Yan's library
00014 // More information can be found in the display datasheet
00015 // H = 0 - Basic instructions
00016 #define CMD_DC_CLEAR_DISPLAY   0x08
00017 #define CMD_DC_NORMAL_MODE     0x0C
00018 #define CMD_DC_FILL_DISPLAY    0x09
00019 #define CMD_DC_INVERT_VIDEO    0x0D
00020 #define CMD_FS_HORIZONTAL_MODE 0x00
00021 #define CMD_FS_VERTICAL_MODE   0x02
00022 #define CMD_FS_BASIC_MODE      0x00
00023 #define CMD_FS_EXTENDED_MODE   0x01
00024 #define CMD_FS_ACTIVE_MODE     0x00
00025 #define CMD_FS_POWER_DOWN_MODE 0x04
00026 // H = 1 - Extended instructions
00027 #define CMD_TC_TEMP_0          0x04
00028 #define CMD_TC_TEMP_1          0x05
00029 #define CMD_TC_TEMP_2          0x06
00030 #define CMD_TC_TEMP_3          0x07
00031 #define CMD_BI_MUX_24          0x15
00032 #define CMD_BI_MUX_48          0x13
00033 #define CMD_BI_MUX_100         0x10
00034 #define CMD_VOP_6V06           0xB2
00035 #define CMD_VOP_7V38           0xC8
00036 
00037 // number of pixels on display
00038 #define WIDTH 84
00039 #define HEIGHT 48
00040 #define BANKS 6
00041 
00042 #include "mbed.h"
00043 
00044 /**
00045 @brief Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed.
00046 @brief Acknowledgements to Chris Yan's Nokia_5110 Library.
00047 */
00048 
00049 class N5110
00050 {
00051 
00052 public:
00053     /** Create a N5110 object connected to the specified pins
00054     *
00055     * @param pwr Pin connected to Vcc on the LCD display (pin 1)
00056     * @param sce Pin connected to chip enable (pin 3)
00057     * @param rst Pin connected to reset (pin 4)
00058     * @param dc  Pin connected to data/command select (pin 5)
00059     * @param mosi Pin connected to data input (MOSI) (pin 6)
00060     * @param sclk Pin connected to serial clock (SCLK) (pin 7)
00061     * @param led Pin connected to LED backlight (must be PWM) (pin 8)
00062     *
00063     */
00064     N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin);
00065 
00066     /** Initialise display
00067     *
00068     *   Powers up the display and turns on backlight (50% brightness default).
00069     *   Sets the display up in horizontal addressing mode and with normal video mode.
00070     */
00071     void init();
00072 
00073     /** Turn off
00074     *
00075     *   Powers down the display and turns of the backlight.
00076     *   Needs to be reinitialised before being re-used.
00077     */
00078     void turnOff();
00079 
00080     /** Clears
00081     *
00082     *   Clears the screen.
00083     */
00084     void clear();
00085 
00086     /** Turn on normal video mode (default)
00087     *  Black on white
00088     */
00089     void normalMode();
00090 
00091     /** Turn on inverse video mode (default)
00092     *  White on black
00093     */
00094     void inverseMode();
00095 
00096     /** Set Brightness
00097     *
00098     *   Sets brightness of LED backlight.
00099     *   @param brightness - float in range 0.0 to 1.0
00100     */
00101     void setBrightness(float brightness);
00102 
00103     /** Print String
00104     *
00105     *   Prints a string of characters to the display. String is cut-off after the 83rd pixel.
00106     *   @param x - the column number (0 to 83)
00107     *   @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row
00108     */
00109     void printString(const char * str,int x,int y);
00110 
00111     /** Set a Pixel
00112     *
00113     *   This function sets a pixel in the display. A call to refresh() must be made
00114     *   to update the display to reflect the change in pixels.
00115     *   @param  x - the x co-ordinate of the pixel (0 to 83)
00116     *   @param  y - the y co-ordinate of the pixel (0 to 47)
00117     */
00118     void setPixel(int x, int y);
00119 
00120     /** Clear a Pixel
00121     *
00122     *   This function clears pixel in the display. A call to refresh() must be made
00123     *   to update the display to reflect the change in pixels.
00124     *   @param  x - the x co-ordinate of the pixel (0 to 83)
00125     *   @param  y - the y co-ordinate of the pixel (0 to 47)
00126     */
00127     void clearPixel(int x, int y);
00128 
00129     /** Refresh display
00130     *
00131     *   This functions refreshes the display to reflect the current data in the buffer.
00132     */
00133     void refresh();
00134 
00135     /** Draw Circle
00136     *
00137     *   This function draws a circle at the specified origin with specified radius to the display.
00138     *   Uses the midpoint circle algorithm.
00139     *   @see http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
00140     *   @param  x0 - x-coordinate of centre
00141     *   @param  y0 - y-coordinate of centre
00142     *   @param  radius - radius of circle in pixels
00143     *   @param  fill - 0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)
00144     */
00145     void drawCircle(int x0,int y0,int radius,int fill);
00146 
00147     /** Draw Line
00148     *
00149     *   This function draws a line between the specified points using linear interpolation.
00150     *   @param  x0 - x-coordinate of first point
00151     *   @param  y0 - y-coordinate of first point
00152     *   @param  x1 - x-coordinate of last point
00153     *   @param  y1 - y-coordinate of last point
00154     *   @param  type - 0 white,1 black,2 dotted
00155     */
00156     void drawLine(int x0,int y0,int x1,int y1,int type);
00157 
00158 
00159 
00160 
00161 private:
00162 
00163     void setXYAddress(int x, int y);
00164     void initSPI();
00165     void turnOn();
00166     void reset();
00167     void clearRAM();
00168     void clearBuffer();
00169     void sendCommand(unsigned char command);
00170 
00171 
00172 public:
00173     unsigned char buffer[84][6];  // screen buffer - the 6 is for the banks - each one is 8 bits;
00174 
00175 private:  // private variables
00176     SPI*    spi;
00177     PwmOut* led;
00178     DigitalOut* pwr;
00179     DigitalOut* sce;
00180     DigitalOut* rst;
00181     DigitalOut* dc;
00182 
00183 };
00184 
00185 const unsigned char font5x7[480] = {
00186     0x00, 0x00, 0x00, 0x00, 0x00,// (space)
00187     0x00, 0x00, 0x5F, 0x00, 0x00,// !
00188     0x00, 0x07, 0x00, 0x07, 0x00,// "
00189     0x14, 0x7F, 0x14, 0x7F, 0x14,// #
00190     0x24, 0x2A, 0x7F, 0x2A, 0x12,// $
00191     0x23, 0x13, 0x08, 0x64, 0x62,// %
00192     0x36, 0x49, 0x55, 0x22, 0x50,// &
00193     0x00, 0x05, 0x03, 0x00, 0x00,// '
00194     0x00, 0x1C, 0x22, 0x41, 0x00,// (
00195     0x00, 0x41, 0x22, 0x1C, 0x00,// )
00196     0x08, 0x2A, 0x1C, 0x2A, 0x08,// *
00197     0x08, 0x08, 0x3E, 0x08, 0x08,// +
00198     0x00, 0x50, 0x30, 0x00, 0x00,// ,
00199     0x08, 0x08, 0x08, 0x08, 0x08,// -
00200     0x00, 0x60, 0x60, 0x00, 0x00,// .
00201     0x20, 0x10, 0x08, 0x04, 0x02,// /
00202     0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
00203     0x00, 0x42, 0x7F, 0x40, 0x00,// 1
00204     0x42, 0x61, 0x51, 0x49, 0x46,// 2
00205     0x21, 0x41, 0x45, 0x4B, 0x31,// 3
00206     0x18, 0x14, 0x12, 0x7F, 0x10,// 4
00207     0x27, 0x45, 0x45, 0x45, 0x39,// 5
00208     0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
00209     0x01, 0x71, 0x09, 0x05, 0x03,// 7
00210     0x36, 0x49, 0x49, 0x49, 0x36,// 8
00211     0x06, 0x49, 0x49, 0x29, 0x1E,// 9
00212     0x00, 0x36, 0x36, 0x00, 0x00,// :
00213     0x00, 0x56, 0x36, 0x00, 0x00,// ;
00214     0x00, 0x08, 0x14, 0x22, 0x41,// <
00215     0x14, 0x14, 0x14, 0x14, 0x14,// =
00216     0x41, 0x22, 0x14, 0x08, 0x00,// >
00217     0x02, 0x01, 0x51, 0x09, 0x06,// ?
00218     0x32, 0x49, 0x79, 0x41, 0x3E,// @
00219     0x7E, 0x11, 0x11, 0x11, 0x7E,// A
00220     0x7F, 0x49, 0x49, 0x49, 0x36,// B
00221     0x3E, 0x41, 0x41, 0x41, 0x22,// C
00222     0x7F, 0x41, 0x41, 0x22, 0x1C,// D
00223     0x7F, 0x49, 0x49, 0x49, 0x41,// E
00224     0x7F, 0x09, 0x09, 0x01, 0x01,// F
00225     0x3E, 0x41, 0x41, 0x51, 0x32,// G
00226     0x7F, 0x08, 0x08, 0x08, 0x7F,// H
00227     0x00, 0x41, 0x7F, 0x41, 0x00,// I
00228     0x20, 0x40, 0x41, 0x3F, 0x01,// J
00229     0x7F, 0x08, 0x14, 0x22, 0x41,// K
00230     0x7F, 0x40, 0x40, 0x40, 0x40,// L
00231     0x7F, 0x02, 0x04, 0x02, 0x7F,// M
00232     0x7F, 0x04, 0x08, 0x10, 0x7F,// N
00233     0x3E, 0x41, 0x41, 0x41, 0x3E,// O
00234     0x7F, 0x09, 0x09, 0x09, 0x06,// P
00235     0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
00236     0x7F, 0x09, 0x19, 0x29, 0x46,// R
00237     0x46, 0x49, 0x49, 0x49, 0x31,// S
00238     0x01, 0x01, 0x7F, 0x01, 0x01,// T
00239     0x3F, 0x40, 0x40, 0x40, 0x3F,// U
00240     0x1F, 0x20, 0x40, 0x20, 0x1F,// V
00241     0x7F, 0x20, 0x18, 0x20, 0x7F,// W
00242     0x63, 0x14, 0x08, 0x14, 0x63,// X
00243     0x03, 0x04, 0x78, 0x04, 0x03,// Y
00244     0x61, 0x51, 0x49, 0x45, 0x43,// Z
00245     0x00, 0x00, 0x7F, 0x41, 0x41,// [
00246     0x02, 0x04, 0x08, 0x10, 0x20,// "\"
00247     0x41, 0x41, 0x7F, 0x00, 0x00,// ]
00248     0x04, 0x02, 0x01, 0x02, 0x04,// ^
00249     0x40, 0x40, 0x40, 0x40, 0x40,// _
00250     0x00, 0x01, 0x02, 0x04, 0x00,// `
00251     0x20, 0x54, 0x54, 0x54, 0x78,// a
00252     0x7F, 0x48, 0x44, 0x44, 0x38,// b
00253     0x38, 0x44, 0x44, 0x44, 0x20,// c
00254     0x38, 0x44, 0x44, 0x48, 0x7F,// d
00255     0x38, 0x54, 0x54, 0x54, 0x18,// e
00256     0x08, 0x7E, 0x09, 0x01, 0x02,// f
00257     0x08, 0x14, 0x54, 0x54, 0x3C,// g
00258     0x7F, 0x08, 0x04, 0x04, 0x78,// h
00259     0x00, 0x44, 0x7D, 0x40, 0x00,// i
00260     0x20, 0x40, 0x44, 0x3D, 0x00,// j
00261     0x00, 0x7F, 0x10, 0x28, 0x44,// k
00262     0x00, 0x41, 0x7F, 0x40, 0x00,// l
00263     0x7C, 0x04, 0x18, 0x04, 0x78,// m
00264     0x7C, 0x08, 0x04, 0x04, 0x78,// n
00265     0x38, 0x44, 0x44, 0x44, 0x38,// o
00266     0x7C, 0x14, 0x14, 0x14, 0x08,// p
00267     0x08, 0x14, 0x14, 0x18, 0x7C,// q
00268     0x7C, 0x08, 0x04, 0x04, 0x08,// r
00269     0x48, 0x54, 0x54, 0x54, 0x20,// s
00270     0x04, 0x3F, 0x44, 0x40, 0x20,// t
00271     0x3C, 0x40, 0x40, 0x20, 0x7C,// u
00272     0x1C, 0x20, 0x40, 0x20, 0x1C,// v
00273     0x3C, 0x40, 0x30, 0x40, 0x3C,// w
00274     0x44, 0x28, 0x10, 0x28, 0x44,// x
00275     0x0C, 0x50, 0x50, 0x50, 0x3C,// y
00276     0x44, 0x64, 0x54, 0x4C, 0x44,// z
00277     0x00, 0x08, 0x36, 0x41, 0x00,// {
00278     0x00, 0x00, 0x7F, 0x00, 0x00,// |
00279     0x00, 0x41, 0x36, 0x08, 0x00,// }
00280     0x08, 0x08, 0x2A, 0x1C, 0x08,// ->
00281     0x08, 0x1C, 0x2A, 0x08, 0x08 // <-
00282 };
00283 
00284 #endif