Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TFTLCD by
Revision 22:4c169297f374, committed 2013-01-26
- Comitter:
- ttodorov
- Date:
- Sat Jan 26 04:36:46 2013 +0000
- Parent:
- 21:e5c1e8ffada1
- Child:
- 23:eca4414196ca
- Commit message:
- - added PWM backlight control
Changed in this revision
--- a/hx8340bs.cpp Sat Jan 26 02:55:46 2013 +0000
+++ b/hx8340bs.cpp Sat Jan 26 04:36:46 2013 +0000
@@ -22,11 +22,9 @@
#include "hx8340bs.h"
#include "helpers.h"
-HX8340S_LCD::HX8340S_LCD( PinName CS, PinName RESET, PinName SCL, PinName SDI, PinName BL )
- : LCD( 176, 220, CS, NC, RESET ), _lcd_pin_scl( SCL ), _lcd_pin_sdi( SDI )
+HX8340S_LCD::HX8340S_LCD( PinName CS, PinName RESET, PinName SCL, PinName SDI, PinName BL, backlight_t blType, float defaultBackLightLevel )
+ : LCD( 176, 220, CS, NC, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_scl( SCL ), _lcd_pin_sdi( SDI )
{
- if ( BL != NC ) _lcd_pin_bl = new DigitalOut( BL );
- else _lcd_pin_bl = 0;
}
void HX8340S_LCD::Initialize( orientation_t orientation, colordepth_t colors )
@@ -45,6 +43,8 @@
_lcd_pin_sdi = HIGH;
if ( _lcd_pin_bl != 0 )
*_lcd_pin_bl = HIGH;
+ else if ( _bl_pwm != 0 )
+ *_bl_pwm = _bl_pwm_default;
wait_ms( 55 );
Activate();
@@ -136,8 +136,7 @@
wait_ms( 10 );
WriteCmd( 0x10 );
wait_ms( 125 );
- if ( _lcd_pin_bl != 0 )
- *_lcd_pin_bl = LOW;
+ LCD::Sleep();
Deactivate();
}
@@ -148,8 +147,7 @@
wait_ms( 10 );
WriteCmd( 0x11 );
wait_ms( 125 );
- if ( _lcd_pin_bl != 0 )
- *_lcd_pin_bl = HIGH;
+ LCD::WakeUp();
Deactivate();
}
--- a/hx8340bs.h Sat Jan 26 02:55:46 2013 +0000
+++ b/hx8340bs.h Sat Jan 26 04:36:46 2013 +0000
@@ -84,8 +84,10 @@
* \param SCL Pin for the serial clock signal.
* \param SDI Pin for the serial data signal.
* \param BL Pin for controlling the backlight. By default not used.
+ * \param blType The backlight type, the default is to utilize the pin - if supplied - as a simple on/off switch
+ * \param defaultBacklightLevel If using PWM to control backlight, this would be the default brightness in percent after LCD initialization.
*/
- HX8340S_LCD( PinName CS, PinName RESET, PinName SCL, PinName SDI, PinName BL = NC );
+ HX8340S_LCD( PinName CS, PinName RESET, PinName SCL, PinName SDI, PinName BL = NC, backlight_t blType = Constant, float defaultBackLightLevel = 1.0 );
/** Initialize display.
*
@@ -182,7 +184,6 @@
private:
DigitalOut _lcd_pin_scl, _lcd_pin_sdi;
- DigitalOut* _lcd_pin_bl;
};
#ifdef __cplusplus
--- a/lcd_base.cpp Sat Jan 26 02:55:46 2013 +0000
+++ b/lcd_base.cpp Sat Jan 26 04:36:46 2013 +0000
@@ -22,12 +22,54 @@
#include "lcd_base.h"
#include "helpers.h"
-LCD::LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS, PinName RESET )
- : _disp_width( width ), _disp_height( height ), _lcd_pin_cs( CS ), _lcd_pin_rs( RS ), _lcd_pin_reset( RESET )
+LCD::LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS, PinName RESET, PinName BL, backlight_t blType, float defaultBacklight )
+ : _disp_width( width ), _disp_height( height ), _lcd_pin_cs( CS ), _lcd_pin_rs( RS ), _lcd_pin_reset( RESET ), _bl_type( blType )
{
SetForeground();
SetBackground();
_font = &TerminusFont;
+ if ( defaultBacklight < 0 ) _bl_pwm_default = 0;
+ else if ( defaultBacklight > 1.0 ) _bl_pwm_default = 1.0;
+ else _bl_pwm_default = defaultBacklight;
+ if ( BL != NC )
+ {
+ if ( blType == Constant )
+ {
+ _bl_pwm = 0;
+ _lcd_pin_bl = new DigitalOut( BL );
+ }
+ else
+ {
+ _lcd_pin_bl = 0;
+ _bl_pwm = new PwmOut( BL );
+ _bl_pwm->period_ms( 8.33 ); // 120 Hz
+ _bl_pwm_current = _bl_pwm_default;
+ // initially off
+ *_bl_pwm = 0;
+ }
+
+ }
+ else
+ {
+ _lcd_pin_bl = 0;
+ _bl_pwm = 0;
+ }
+}
+
+void LCD::Sleep( void )
+{
+ if ( _lcd_pin_bl != 0 )
+ *_lcd_pin_bl = LOW;
+ else if ( _bl_pwm != 0 )
+ *_bl_pwm = 0;
+}
+
+void LCD::WakeUp( void )
+{
+ if ( _lcd_pin_bl != 0 )
+ *_lcd_pin_bl = HIGH;
+ else if ( _bl_pwm != 0 )
+ *_bl_pwm = _bl_pwm_current;
}
inline
@@ -75,6 +117,26 @@
return 0;
}
+void LCD::SetBacklightLevel( float level )
+{
+ switch ( _bl_type )
+ {
+ case Direct:
+ if ( _bl_pwm != 0 )
+ {
+ *_bl_pwm = level;
+ _bl_pwm_current = level;
+ }
+ break;
+
+ case Indirect:
+ break;
+ case Constant:
+ default:
+ break;
+ }
+}
+
void LCD::FillScreen( int color )
{
unsigned int rgb = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
--- a/lcd_base.h Sat Jan 26 02:55:46 2013 +0000
+++ b/lcd_base.h Sat Jan 26 04:36:46 2013 +0000
@@ -158,6 +158,34 @@
*/
typedef struct Bitmap_struct bitmap_t;
+/** \struct BacklightPwmCtrl_enum
+ * \brief Type of backlight control for the LCD.
+ *
+ * When the selected type is \c Constant, the pin is simply on or off - there is no gradation in the intensity of the display.
+ * In this case any free pin can be used to control the backlight. On the other hand, when PWM is used to control brightness,
+ * take care to use only PWM-able mbed pins (p21, p22, p23, p24, p25, and p26), any other pins won't work. It is assumed that
+ * you know what you are doing, so no check is done to prevent using a non-PWM pin as assigned control pin, when either \c Direct
+ * or \c Indirect option is used.
+ *
+ * \version 0.1
+ * \remark When choosing PWM to control the backlight, you have the option to choose the pin to either source (\c Direct) or sink
+ * (\c Indirect) the current for LCD brightness control. Be aware that the mbed pins can source (and probably sink when
+ * configured as inputs) only 4 mA @+3V3 VDD. So if you are intending to use a bigger LCD, whith more LEDs in its backlight
+ * implementation, you probably want to interface it through a small signal transistor or a small MOSFET, in order to be able
+ * to handle a higher current without damaging your mbed.
+ * \remark As of version 0.1 (2013-01-25) the Indirect method of PWM has not been implemented yet.
+ */
+enum BacklightPwmCtrl_enum
+{
+ Constant, /**< When the pin is a simple on/off switch. */
+ Direct, /**< Control the brightness with PWM, as the control pin is sourcing the current to drive the backlight LEDs. */
+ Indirect, /**< Control the brightness with PWM, as the control pin is sinking the current which drives the backlight LEDs. */
+};
+/** \typedef backlight_t
+ * \brief Convenience shortcut for the backlight control type.
+ */
+typedef BacklightPwmCtrl_enum backlight_t;
+
/** Base class for LCD implementations.
*
@@ -202,7 +230,7 @@
* \remarks This function is controller-specific and needs to be
* implemented separately for each available display.
*/
- virtual void Sleep( void ) = 0;
+ virtual void Sleep( void );
/** Wakes up the display from sleep mode.
*
@@ -212,7 +240,7 @@
* \remarks This function is controller-specific and needs to be
* implemented separately for each available display.
*/
- virtual void WakeUp( void ) = 0;
+ virtual void WakeUp( void );
/** Set the foreground color for painting.
*
@@ -271,6 +299,11 @@
*/
virtual void FillScreen( int color = -1 );
+ /** Sets the backlight intensity in percent as a float value in the range [0.0,1.0].
+ * \param level The backligh intensity in percent, where 0.0 is off and 1.0 is full brightness.
+ */
+ virtual void SetBacklightLevel( float level );
+
/** Clears the screen.
*
* This is the same as calling #FillScreen() or #FillScreen( -1 ) to use the background color.
@@ -408,8 +441,11 @@
* \param CS Pin connected to the CS input of the display.
* \param RS Pin connected to the RS input of the display.
* \param RESET Pin connected to the RESET input of the display.
+ * \param BL Pin connected to the circuit controlling the LCD's backlight.
+ * \param blType The type of backlight to be used.
+ * \param defaultBacklight The standard backlight intensity (if using PWM control), expressed in percent as float value from 0.0 to 1.0
*/
- LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS, PinName RESET );
+ LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS, PinName RESET, PinName BL, backlight_t blType, float defaultBacklight );
/** Activates the display for command/data transfer.
*
@@ -539,6 +575,10 @@
colordepth_t _colorDepth;
unsigned int _foreground, _background;
const font_t* _font;
+ DigitalOut* _lcd_pin_bl;
+ PwmOut* _bl_pwm;
+ backlight_t _bl_type;
+ float _bl_pwm_default, _bl_pwm_current;
};
#ifdef __cplusplus
--- a/ssd1289.cpp Sat Jan 26 02:55:46 2013 +0000
+++ b/ssd1289.cpp Sat Jan 26 04:36:46 2013 +0000
@@ -22,12 +22,10 @@
#include "ssd1289.h"
#include "helpers.h"
-SSD1289_LCD::SSD1289_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL, PinName RD )
- : LCD( 240, 320, CS, RS, RESET ), _lcd_pin_wr( WR )
+SSD1289_LCD::SSD1289_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL, PinName RD, backlight_t blType, float defaultBackLightLevel )
+ : LCD( 240, 320, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
{
_lcd_port = DATA_PORT;
- if ( BL != NC ) _lcd_pin_bl = new DigitalOut( BL );
- else _lcd_pin_bl = 0;
if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
else _lcd_pin_rd = 0;
}
@@ -45,6 +43,8 @@
_lcd_pin_cs = HIGH;
if ( _lcd_pin_bl != 0 )
*_lcd_pin_bl = HIGH;
+ else if ( _bl_pwm != 0 )
+ *_bl_pwm = _bl_pwm_default;
if ( _lcd_pin_rd != 0 )
*_lcd_pin_rd = HIGH;
_lcd_pin_wr = HIGH;
@@ -197,15 +197,13 @@
void SSD1289_LCD::Sleep( void )
{
WriteCmdData( 0x10, 0x0001 ); // sleep mode: 0 = exit, 1 = enter
- if ( _lcd_pin_bl != 0 )
- *_lcd_pin_bl = LOW;
+ LCD::Sleep();
}
void SSD1289_LCD::WakeUp( void )
{
WriteCmdData( 0x10, 0x0000 ); // sleep mode: 0 = exit, 1 = enter
- if ( _lcd_pin_bl != 0 )
- *_lcd_pin_bl = HIGH;
+ LCD::WakeUp();
}
void SSD1289_LCD::WriteCmd( unsigned short cmd )
--- a/ssd1289.h Sat Jan 26 02:55:46 2013 +0000
+++ b/ssd1289.h Sat Jan 26 04:36:46 2013 +0000
@@ -91,8 +91,10 @@
* \param RD Pin for the RD signal. This line is not needed by the driver, so if you would like to
* use the pin on the mbed for something else, just pull-up the respective pin on the LCD high,
* and do not assign a value to this parameter when creating the controller instance.
+ * \param blType The backlight type, the default is to utilize the pin - if supplied - as a simple on/off switch
+ * \param defaultBacklightLevel If using PWM to control backlight, this would be the default brightness in percent after LCD initialization.
*/
- SSD1289_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL = NC, PinName RD = NC );
+ SSD1289_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL = NC, PinName RD = NC, backlight_t blType = Constant, float defaultBackLightLevel = 1.0 );
/** Initialize display.
*
--- a/st7735.cpp Sat Jan 26 02:55:46 2013 +0000
+++ b/st7735.cpp Sat Jan 26 04:36:46 2013 +0000
@@ -22,11 +22,9 @@
#include "st7735.h"
#include "helpers.h"
-ST7735_LCD::ST7735_LCD( PinName CS, PinName RESET, PinName RS, PinName SCL, PinName SDA, PinName BL )
- : LCD( 128, 160, CS, RS, RESET ), _lcd_pin_scl( SCL ), _lcd_pin_sda( SDA )
+ST7735_LCD::ST7735_LCD( PinName CS, PinName RESET, PinName RS, PinName SCL, PinName SDA, PinName BL, backlight_t blType, float defaultBackLightLevel )
+ : LCD( 128, 160, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_scl( SCL ), _lcd_pin_sda( SDA )
{
- if ( BL != NC ) _lcd_pin_bl = new DigitalOut( BL );
- else _lcd_pin_bl = 0;
}
void ST7735_LCD::Initialize( orientation_t orientation, colordepth_t colors )
@@ -46,6 +44,8 @@
_lcd_pin_sda = HIGH;
if ( _lcd_pin_bl != 0 )
*_lcd_pin_bl = HIGH;
+ else if ( _bl_pwm != 0 )
+ *_bl_pwm = _bl_pwm_default;
wait_ms( 55 );
Activate();
@@ -181,8 +181,7 @@
wait_ms( 10 );
WriteCmd( 0x10 );
wait_ms( 125 );
- if ( _lcd_pin_bl != 0 )
- *_lcd_pin_bl = LOW;
+ LCD::Sleep();
Deactivate();
}
@@ -193,8 +192,7 @@
wait_ms( 10 );
WriteCmd( 0x11 );
wait_ms( 125 );
- if ( _lcd_pin_bl != 0 )
- *_lcd_pin_bl = HIGH;
+ LCD::WakeUp();
Deactivate();
}
--- a/st7735.h Sat Jan 26 02:55:46 2013 +0000
+++ b/st7735.h Sat Jan 26 04:36:46 2013 +0000
@@ -85,8 +85,10 @@
* \param SCL Pin for the serial clock line.
* \param SDA Pin for the serial data line.
* \param BL Pin for controlling the backlight. By default not used.
+ * \param blType The backlight type, the default is to utilize the pin - if supplied - as a simple on/off switch
+ * \param defaultBacklightLevel If using PWM to control backlight, this would be the default brightness in percent after LCD initialization.
*/
- ST7735_LCD( PinName CS, PinName RESET, PinName RS, PinName SCL, PinName SDA, PinName BL = NC );
+ ST7735_LCD( PinName CS, PinName RESET, PinName RS, PinName SCL, PinName SDA, PinName BL = NC, backlight_t blType = Constant, float defaultBackLightLevel = 1.0 );
/** Initialize display.
*
