Forked para SNOCC
Fork of RA8875 by
RA8875.h
- Committer:
- WiredHome
- Date:
- 2014-01-12
- Revision:
- 20:6e2e4a8372eb
- Parent:
- 19:3f82c1161fd2
- Child:
- 21:3c1efb192927
File content as of revision 20:6e2e4a8372eb:
#ifndef RA8875_H #define RA8875_H #include <mbed.h> #include "GraphicsDisplay.h" #define RA8875_DEFAULT_SPI_FREQ 1000000 // Define this to enable code that monitors the performance of various // graphics commands. //#define PERF_METRICS // What better place for some test code than in here... #define TESTENABLE #define RGB(r,g,b) ( ((r<<8)&0xF800) | ((g<<3)&0x07E0) | (b>>3) ) /// DOS colors - slightly color enhanced #define Black (color_t)(RGB(0,0,0)) #define Blue (color_t)(RGB(0,0,187)) #define Green (color_t)(RGB(0,187,0)) #define Cyan (color_t)(RGB(0,187,187)) #define Red (color_t)(RGB(187,0,0)) #define Magenta (color_t)(RGB(187,0,187)) #define Brown (color_t)(RGB(187,187,0)) #define Gray (color_t)(RGB(187,187,187)) #define Charcoal (color_t)(RGB(85,85,85)) #define BrightBlue (color_t)(RGB(85,85,255)) #define BrightGreen (color_t)(RGB(85,255,85)) #define BrightCyan (color_t)(RGB(85,255,255)) #define Orange (color_t)(RGB(255,85,85)) #define Pink (color_t)(RGB(255,85,255)) #define Yellow (color_t)(RGB(255,255,85)) #define White (color_t)(RGB(255,255,255)) #define BrightRed (color_t)(RGB(255,0,0)) //namespace SW_graphics //{ /// color type definition to let the compiler help keep us honest. /// /// colors can be defined with the RGB(r,g,b) macro, and there /// are a number of predefined colors: /// - Black, Blue, Green, Cyan, /// - Red, Magenta, Brown, Gray, /// - Charcoal, BrightBlue, BrightGreen, BrightCyan, /// - Orange, Pink, Yellow, White /// typedef uint16_t color_t; /// background fill info for drawing Text, Rectangles, RoundedRectanges, Circles, Ellipses and Triangles. typedef enum { NOFILL, ///< do not fill the object with the background color FILL ///< fill the object space with the background color } fill_t; /// return values from functions //typedef enum //{ // noerror, // bad_parameter //} RetCode_t; class RA8875 : public GraphicsDisplay { public: /// font type selection. typedef enum { ISO8859_1, ISO8859_2, ISO8859_3, ISO8859_4 } font_t; /// font rotation selection typedef enum { normal, rotated } font_angle_t; /// alignment typedef enum { align_none, align_full } alignment_t; /// Scale factor - 1, 2, 3 4 typedef unsigned int HorizontalScale; /// Scale factor - 1, 2, 3, 4 typedef unsigned int VerticalScale; /// Clear screen region typedef enum { FULLWINDOW, ACTIVEWINDOW } Region_t; /// Constructor for a display based on the RAiO RA8875 /// display controller. /// /// @param mosi is the SPI master out slave in pin on the mbed. /// @param miso is the SPI master in slave out pin on the mbed. /// @param sclk is the SPI shift clock pin on the mbed. /// @param csel is the DigitalOut pin on the mbed to use as the /// active low chip select for the display controller. /// @param reset is the DigitalOut pin on the mbed to use as the /// active low reset input on the display controller - /// but this is not currently used. /// @param name is a text name for this object, which will permit /// capturing stdout and printf() directly to it. /// RA8875(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName reset, const char * name = "lcd"); /// Destructor doesn't have much to do. //~RA8875(); /// Write a command to the display /// /// This is a high level command, and may invoke several primitives. /// /// @param command is the command to write. /// @data is optional data to be written to the command register /// and only occurs if the data is in the range [0 - 0xFF]. /// @returns success/failure code. @see RetCode_t. /// RetCode_t WriteCommand(unsigned char command, unsigned int data = 0xFFFF); /// Write a data byte to the display /// /// This is a high level command, and may invoke several primitives. /// /// @param data is the data to write. /// @returns success/failure code. @see RetCode_t. /// RetCode_t WriteData(unsigned char data); /// Read a command register /// /// @param command is the command register to read. /// @returns the value read from the register. /// unsigned char ReadCommand(unsigned char command); /// Read a data byte to the display /// /// This is a high level command, and may invoke several primitives. /// /// @returns data that was read. /// unsigned char ReadData(void); /// Read the display status /// /// This is a high level command, and may invoke several primitives. /// /// @returns data that was read. /// unsigned char ReadStatus(void); /// get the width in pixels of the currently active font /// /// @returns font width in pixels. /// unsigned int fontwidth(void); /// get the height in pixels of the currently active font /// /// @returns font height in pixels. /// unsigned int fontheight(void); /// get the number of colums based on the currently active font /// /// @returns number of columns. /// virtual int columns(void); /// get the number of rows based on the currently active font /// /// @returns number of rows. /// virtual int rows(void); /// get the screen width in pixels /// /// @returns screen width in pixels. /// virtual int width(void); /// get the screen height in pixels /// /// @returns screen height in pixels. /// virtual int height(void); /// Set cursor position based on the current font size. /// /// @param x is the horizontal position in character positions /// @param y is the vertical position in character positions /// @returns success/failure code. @see RetCode_t. /// virtual RetCode_t locate(unsigned int x, unsigned int y); /// Prepare the controller to write text to the screen by positioning /// the cursor. /// /// @param x is the horizontal position in pixels (from the left edge) /// @param y is the vertical position in pixels (from the top edge) /// @returns success/failure code. @see RetCode_t. /// RetCode_t SetTextCursor(unsigned int x, unsigned int y); /// Select the ISO 8859-X font to use next. /// /// Supported fonts: ISO 8859-1, -2, -3, -4 /// /// @param font selects the font for the subsequent text rendering. /// /// @note if either hScale or vScale is outside of its permitted range, /// the command is not executed. /// @returns success/failure code. @see RetCode_t. /// RetCode_t SetTextFont(font_t font = ISO8859_1); /// Control the font behavior. /// /// This command lets you make several modifications to any text that /// is written. /// /// Options can be combined: /// Default: /// @li Full alignment disabled, /// @li Font with Background color, /// @li Font in normal orientiation, /// @li Horizontal scale x 1 /// @li Vertical scale x 1 /// @li alignment /// /// @param fillit defaults to FILL, but can be NOFILL /// @param angle defaults to normal, but can be rotated /// @param hScale defaults to 1, but can be 1, 2, 3, or 4, /// and scales the font size by this amount. /// @param vScale defaults to 1, but can be 1, 2, 3, or 4, /// and scales the font size by this amount. /// @param alignment defaults to align_none, but can be /// align_full. /// /// @note if either hScale or vScale is outside of its permitted range, /// the command is not executed. /// @returns success/failure code. @see RetCode_t. /// RetCode_t SetTextFontControl(fill_t fillit = FILL, font_angle_t angle = normal, HorizontalScale hScale = 1, VerticalScale vScale = 1, alignment_t alignment = align_none); /// Control the font size /// /// This command lets you set the font enlargement for both horizontal /// and vertical, independent of the rotation, background, and /// alignment. @see SetTextFontControl. /// /// @param hScale defaults to 1, but can be 1, 2, 3, or 4, /// and scales the font size by this amount. /// @param vScale defaults to 1, but can be 1, 2, 3, or 4, /// and scales the font size by this amount. /// /// @note if either hScale or vScale is outside of its permitted range, /// the command is not executed. /// @returns success/failure code. @see RetCode_t. /// RetCode_t SetTextFontSize(HorizontalScale hScale = 1, VerticalScale vScale = 1); /// put a character on the screen. /// /// @param c is the character. /// @returns the character, or EOF if there is an error. /// virtual int _putc(int c); /// Write string of text to the display /// /// @param string is the null terminated string to send to the display. /// void puts(const char * string); /// Write string of text to the display at the specified location. /// /// @param x is the horizontal position in pixels (from the left edge) /// @param y is the vertical position in pixels (from the top edge) /// @param string is the null terminated string to send to the display. /// void puts(unsigned int x, unsigned int y, const char * string); /// Prepare the controller to write binary data to the screen by positioning /// the memory cursor. /// /// @param x is the horizontal position in pixels (from the left edge) /// @param y is the vertical position in pixels (from the top edge) /// @returns success/failure code. @see RetCode_t. /// RetCode_t SetMemoryCursor(unsigned int x, unsigned int y); /// Set the window, which controls where items are written to the screen. /// /// When something hits the window width, it wraps back to the left side /// and down a row. If the initial write is outside the window, it will /// be captured into the window when it crosses a boundary. /// /// @param x is the left edge in pixels. /// @param y is the top edge in pixels. /// @param width is the window width in pixels. /// @param height is the window height in pixels. /// @returns success/failure code. @see RetCode_t. /// RetCode_t SetWindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height); /// Clear the screen. /// /// The behavior is to clear the whole screen. @see clsw(). /// /// @returns success/failure code. @see RetCode_t. /// virtual RetCode_t cls(void); /// Clear the screen, or clear only the active window. /// /// The default behavior is to clear the whole screen. With the optional /// parameter, the action can be restricted to the active window, which /// can be set with the @see SetWindow method. /// /// @param region is an optional parameter that defaults to FULLWINDOW /// or may be set to ACTIVEWINDOW. /// @returns success/failure code. @see RetCode_t. /// RetCode_t clsw(RA8875::Region_t region = FULLWINDOW); /// Set the background color. /// /// @param color is expressed in 16-bit format. /// @returns success/failure code. @see RetCode_t. /// virtual RetCode_t background(color_t color); /// Set the background color. /// /// @param r is the red element of the color. /// @param g is the green element of the color. /// @param b is the blue element of the color. /// @returns success/failure code. @see RetCode_t. /// virtual RetCode_t background(unsigned char r, unsigned char g, unsigned char b); /// Set the foreground color. /// /// @param color is expressed in 16-bit format. /// @returns success/failure code. @see RetCode_t. /// virtual RetCode_t foreground(color_t color); /// Set the foreground color. /// /// @param R is the red element of the color. /// @param G is the green element of the color. /// @param B is the blue element of the color. /// @returns success/failure code. @see RetCode_t. /// virtual RetCode_t foreground(unsigned char R, unsigned char G, unsigned char B); /// Get the current foreground color value. /// /// @returns the current foreground color. /// unsigned int GetForeColor(void); /// Draw a pixel in the specified color. /// /// @note As a side effect, this also sets the foreground color /// affecting all subsequent operations. /// /// @param x is the horizontal offset to this pixel. /// @param y is the vertical offset to this pixel. /// @param color defines the color for the pixel. /// @returns success/failure code. @see RetCode_t. /// virtual RetCode_t pixel(unsigned int x, unsigned int y, color_t color); /// Draw a pixel in the current foreground color. /// /// @param x is the horizontal offset to this pixel. /// @param y is the veritical offset to this pixel. /// @returns success/failure code. @see RetCode_t. /// virtual RetCode_t pixel(unsigned int x, unsigned int y); /// Draw a line in the specified color /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x1 is the horizontal start of the line. /// @param y1 is the vertical start of the line. /// @param x2 is the horizontal end of the line. /// @param y2 is the vertical end of the line. /// @param color defines the foreground color. /// @returns success/failure code. @see RetCode_t. /// RetCode_t line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, color_t color); /// Draw a line /// /// Draws a line using the foreground color setting. /// /// @param x1 is the horizontal start of the line. /// @param y1 is the vertical start of the line. /// @param x2 is the horizontal end of the line. /// @param y2 is the vertical end of the line. /// @returns success/failure code. @see RetCode_t. /// RetCode_t line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2); /// Draw a rectangle in the specified color /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x1 is the horizontal start of the line. /// @param y1 is the vertical start of the line. /// @param x2 is the horizontal end of the line. /// @param y2 is the vertical end of the line. /// @param color defines the foreground color. /// @param fillit is optional to FILL the rectangle. default is NOFILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t rect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, color_t color, fill_t fillit = NOFILL); /// Draw a filled rectangle in the specified color /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x1 is the horizontal start of the line. /// @param y1 is the vertical start of the line. /// @param x2 is the horizontal end of the line. /// @param y2 is the vertical end of the line. /// @param color defines the foreground color. /// @param fillit is optional to NOFILL the rectangle. default is FILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t fillrect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, color_t color, fill_t fillit = FILL); /// Draw a rectangle /// /// Draws a rectangle using the foreground color setting. /// /// @param x1 is the horizontal start of the line. /// @param y1 is the vertical start of the line. /// @param x2 is the horizontal end of the line. /// @param y2 is the vertical end of the line. /// @param fillit is optional to FILL the rectangle. default is NOFILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t rect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, fill_t fillit = NOFILL); /// Draw a filled rectangle with rounded corners using the specified color. /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x1 is the horizontal start of the line. /// @param y1 is the vertical start of the line. /// @param x2 is the horizontal end of the line. /// @param y2 is the vertical end of the line. /// @param radius1 defines the horizontal width of the curved corner. /// @param radius2 defines the vertical width of the curved corner. /// @param color defines the foreground color. /// @param fillit is optional to FILL the rectangle. default is NOFILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t fillroundrect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int radius1, unsigned int radius2, color_t color, fill_t fillit = FILL); /// Draw a rectangle with rounded corners using the specified color. /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x1 is the horizontal start of the line. /// @param y1 is the vertical start of the line. /// @param x2 is the horizontal end of the line. /// @param y2 is the vertical end of the line. /// @param radius1 defines the horizontal width of the curved corner. /// @param radius2 defines the vertical width of the curved corner. /// @param color defines the foreground color. /// @param fillit is optional to FILL the rectangle. default is NOFILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t roundrect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int radius1, unsigned int radius2, color_t color, fill_t fillit = NOFILL); /// Draw a rectangle with rounded corners. /// /// Draws a rectangle with rounded corners using the foreground color setting. /// /// @param x1 is the horizontal start of the line. /// @param y1 is the vertical start of the line. /// @param x2 is the horizontal end of the line. /// @param y2 is the vertical end of the line. /// @param radius1 defines the horizontal width of the curved corner. /// @param radius2 defines the vertical width of the curved corner. /// @param fillit is optional to FILL the rectangle. default is NOFILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t roundrect(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int radius1, unsigned int radius2, fill_t fillit = NOFILL); /// Draw a triangle in the specified color. /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x1 is the horizontal for point 1. /// @param y1 is the vertical for point 1. /// @param x2 is the horizontal for point 2. /// @param y2 is the vertical for point 2. /// @param x3 is the horizontal for point 3. /// @param y3 is the vertical for point 3. /// @param color defines the foreground color. /// @param fillit is optional to FILL the rectangle. default is NOFILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t triangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int x3, unsigned int y3, color_t color, fill_t fillit = NOFILL); /// Draw a filled triangle in the specified color. /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x1 is the horizontal for point 1. /// @param y1 is the vertical for point 1. /// @param x2 is the horizontal for point 2. /// @param y2 is the vertical for point 2. /// @param x3 is the horizontal for point 3. /// @param y3 is the vertical for point 3. /// @param color defines the foreground color. /// @param fillit is optional to FILL the rectangle. default is NOFILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t filltriangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int x3, unsigned int y3, color_t color, fill_t fillit = FILL); /// Draw a triangle /// /// Draws a triangle using the foreground color setting. /// /// @param x1 is the horizontal for point 1. /// @param y1 is the vertical for point 1. /// @param x2 is the horizontal for point 2. /// @param y2 is the vertical for point 2. /// @param x3 is the horizontal for point 3. /// @param y3 is the vertical for point 3. /// @param fillit is optional to FILL the rectangle. default is NOFILL. /// @returns success/failure code. @see RetCode_t. /// RetCode_t triangle(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int x3, unsigned int y3, fill_t fillit = NOFILL); /// Draw a circle using the specified color. /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x is the horizontal center of the circle. /// @param y is the vertical center of the circle. /// @param radius defines the size of the circle. /// @param color defines the foreground color. /// @returns success/failure code. @see RetCode_t. /// RetCode_t circle(unsigned int x, unsigned int y, unsigned int radius, color_t color, fill_t fillit = NOFILL); /// Draw a filled circle using the specified color. /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x is the horizontal center of the circle. /// @param y is the vertical center of the circle. /// @param radius defines the size of the circle. /// @param color defines the foreground color. /// @returns success/failure code. @see RetCode_t. /// RetCode_t fillcircle(unsigned int x, unsigned int y, unsigned int radius, color_t color, fill_t fillit = FILL); /// Draw a circle. /// /// Draws a circle using the foreground color setting. /// /// @param x is the horizontal center of the circle. /// @param y is the vertical center of the circle. /// @param radius defines the size of the circle. /// @returns success/failure code. @see RetCode_t. /// RetCode_t circle(unsigned int x, unsigned int y, unsigned int radius, fill_t fillit = NOFILL); /// Draw an Ellipse using the specified color /// /// @note As a side effect, this changes the current /// foreground color for subsequent operations. /// /// @param x is the horizontal center of the ellipse. /// @param y is the vertical center of the ellipse. /// @param radius1 defines the horizontal width of the ellipse. /// @param radius2 defines the vertical width of the ellipse. /// @param color defines the foreground color. /// @param fillit defines whether the circle is filled or not. /// @returns success/failure code. @see RetCode_t. /// RetCode_t ellipse(unsigned int x, unsigned int y, unsigned int radius1, unsigned int radius1, color_t color, fill_t fillit = NOFILL); /// Draw an Ellipse /// /// Draws it using the foreground color setting. /// /// @param x is the horizontal center of the ellipse. /// @param y is the vertical center of the ellipse. /// @param radius1 defines the horizontal width of the ellipse. /// @param radius2 defines the vertical width of the ellipse. /// @param fillit defines whether the circle is filled or not. /// @returns success/failure code. @see RetCode_t. /// RetCode_t ellipse(unsigned int x, unsigned int y, unsigned int radius1, unsigned int radius1, fill_t fillit = NOFILL); /// Control display power /// /// @param on when set to true will turn on the display, when false it is turned off. /// @returns success/failure code. @see RetCode_t. /// RetCode_t Power(bool on); /// Reset the display controller via the Software Reset interface. /// /// @returns success/failure code. @see RetCode_t. /// RetCode_t Reset(void); /// Set backlight brightness. /// /// When the built-in PWM is used to control the backlight, this /// API can be used to set the brightness. /// /// @param brightness ranges from 0 (off) to 255 (full on) /// @returns success/failure code. @see RetCode_t. /// RetCode_t Backlight_u8(unsigned char brightness); /// Set backlight brightness. /// /// When the built-in PWM is used to control the backlight, this /// API can be used to set the brightness. /// /// @param brightness ranges from 0.0 (off) to 1.0 (full on) /// @returns success/failure code. @see RetCode_t. /// RetCode_t Backlight(float brightness); /// Set a reference to a bitmap font, provided by the user. /// /// @note Tool to create the fonts is accessible from its creator /// available at http://www.mikroe.com. /// Change the data to an array of type char[]. /// /// @param font is a pointer to a specially formed font array. /// This special font array has a 4-byte header, followed by /// the data: /// - the number of bytes per char /// - the vertical size in pixels for each character /// - the horizontal size in pixels for each character /// - the number of bytes per vertical line (width of the array) /// @returns error code. /// RetCode_t set_font(const unsigned char * font); /// Get the RGB value for a DOS color. /// /// @param i is the color, in the range 0 to 15; /// @returns the RGB color of the selected index, or 0 /// if the index is out of bounds. /// color_t DOSColor(int i); /// Get the color name (string) for a DOS color. /// /// @param i is the color, in the range 0 to 15; /// @returns a pointer to a string with the color name, /// or NULL if the index is out of bounds. /// const char * DOSColorNames(int i); #ifdef PERF_METRICS /// Clear the performance metrics to zero. void ClearPerformance(); /// Report the performance metrics for drawing functions using /// printf() void ReportPerformance(); #endif private: /// Set the SPI port frequency (in Hz). /// /// @note attempts to call this API at runtime, with the display /// already online, cause the system to lockup. /// Not sure why, so moving this to private to run once. /// /// This uses the mbed SPI driver, and is therefore dependent on /// its capabilities. Limited tests were performed for the display /// in the range of 1,000,000 to 50,000,000 Hz. The display was /// a bit erratic above 20,000,000 Hz, so this became the default, /// even though it might have been the bench-level wiring that posed /// the limit. /// /// @returns success/failure code. @see RetCode_t. /// RetCode_t frequency(unsigned long Hz = RA8875_DEFAULT_SPI_FREQ); /// Initialize the chip, which is normally done as part of the /// constructor, so not called by the user. /// /// @returns success/failure code. @see RetCode_t. /// RetCode_t init(void); /// Select the peripheral to use it. /// /// @param chipsel when true will select the peripheral, and when false /// will deselect the chip. This is the logical selection, and /// the pin selection is the invert of this. /// @returns success/failure code. @see RetCode_t. /// RetCode_t select(bool chipsel); /// The most primitive - to write a data value to the SPI interface. /// /// @param data is the value to write. /// @returns a value read from the port, since SPI is often shift /// in while shifting out. /// unsigned char spiwrite(unsigned char data); /// The most primitive - to read a data value to the SPI interface. /// /// This is really just a specialcase of the write command, where /// the value zero is written in order to read. /// /// @returns a value read from the port, since SPI is often shift /// in while shifting out. /// unsigned char spiread(); SPI spi; ///< spi port DigitalOut cs; ///< chip select pin, assumed active low DigitalOut res; ///< reset pin, assumed active low const unsigned char * font; ///< reference to an external font somewhere in memory #ifdef PERF_METRICS typedef enum { PRF_CLS, PRF_DRAWPOINT, PRF_DRAWLINE, PRF_DRAWRECTANGLE, PRF_DRAWROUNDEDRECTANGLE, PRF_DRAWTRIANGLE, PRF_DRAWCIRCLE, PRF_DRAWELLIPSE, METRICCOUNT } method_e; unsigned long metrics[METRICCOUNT]; void RegisterPerformance(method_e method); Timer performance; #endif }; //} // namespace //using namespace SW_graphics; #endif