Bare Metal Programming Tool Kit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
pcd8544.h
1 //***************************************************************************
2 //
3 // file : bmptk/chips/pcd8544.h
4 //
5 // LICENSE (MIT expat license, copy of license.txt)
6 //
7 // Copyright (c) 2013 Wouter van Ooijen (wouter@voti.nl)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE..
27 //
28 //***************************************************************************
29 
30 // - http://wiki.noccylabs.info/wiki/Philips_PCD8544
31 
32 namespace bmptk{
33 namespace chips {
34 
36 template<
37  typename arg_sce,
38  typename arg_res,
39  typename arg_dc,
40  typename arg_sdin,
41  typename arg_sclk,
42  int x_size,
43  int y_size
44 > class pcd8544 : public bmptk::graphics::frame {
45 private:
46 
52 
53  static unsigned char buf[ (( x_size * y_size ) + 7 ) / 8 ];
54 
55  static void send_byte( unsigned char d ){
56  for( int i = 0; i < 8; i++ ){
57  sdin::set( d & 0x80 );
58  bmptk::wait( 1 * bmptk::us );
59  sclk::set( 1 );
60  d = d << 1;
61  bmptk::wait( 1 * bmptk::us );
62  sclk::set( 0 );
63  }
64  }
65 
66  static void data( unsigned char d ){
67  dc::set( 1 );
68  sce::set( 0 );
69  send_byte( d );
70  sce::set( 1 );
71  }
72 
73  static void pixels( unsigned char x, unsigned char y, unsigned char d ){
74  command( 0x80 | x );
75  command( 0x40 | y );
76  data( d );
77  }
78 
79 protected:
80 
82  static void command( unsigned char d ){
83  dc::set( 0 );
84  sce::set( 0 );
85  send_byte( d );
86  sce::set( 1 );
87  }
88 
89 public:
90 
92  //
96  pcd8544():
97  bmptk::graphics::frame( bmptk::graphics::vector( x_size, y_size ))
98  {
99  sce::init();
100  res::init();
101  dc::init();
102  sdin::init();
103  sclk::init();
104 
105  sclk::set( 0 );
106  bmptk::wait( 1 * bmptk::us );
107  sce::set( 1 );
108  bmptk::wait( 1 * bmptk::us );
109  res::set( 0 );
110  bmptk::wait( 1 * bmptk::us );
111  res::set( 1 );
112  bmptk::wait( 1 * bmptk::us );
113 
114  // initialization is not done here
115  }
116 
119  const bmptk::graphics::vector p,
120  const bmptk::graphics::color c
121  ){
122  unsigned int a = p.x_get() + ( p.y_get() / 8 ) * 84;
123  unsigned int m = 1 << ( p.y_get() % 8 );
124  if( c == bmptk::graphics::color::black() ){
125  buf[ a ] |= m;
126  } else {
127  buf[ a ] &= ~m;
128  }
129  pixels( p.x_get(), p.y_get() / 8, buf[ a ] );
130  }
131 
133  void clear(
134  const bmptk::graphics::color c
136  ){
137  unsigned char fill = c == bmptk::graphics::color::white() ? 0 : 0xFF;
138  command( 0x80 | 0 );
139  command( 0x40 | 0 );
140  for( int i = 0; i < 504; i++ ){
141  buf[ i ] = fill;
142  data( fill );
143  }
144  }
145 
146 };
147 
148 // buf instance
149 template <
150  typename sce,
151  typename res,
152  typename dc,
153  typename sdin,
154  typename sclk,
155  int x_size,
156  int y_size
157 > unsigned char pcd8544 <
158  sce, res, dc, sdin, sclk, x_size, y_size
159 >::buf[ (( x_size * y_size ) + 7 ) / 8 ];
160 
162 template<
163  typename sce,
164  typename res,
165  typename dc,
166  typename sdin,
167  typename sclk
168 > class lcd5510 : public pcd8544 < sce, res, dc, sdin, sclk, 84, 48 > {
169 
170  void command( unsigned char d ){
172  }
173 
174 public:
175 
176  lcd5510(){
177  command( 0x21 ); // select exteded instructions
178  command( 0xC8 ); // Vop = 110000b
179  command( 0x06 ); // TCx = 00b
180  command( 0x13 ); // BSx = 100b
181  command( 0x20 ); // select basic instructions, horizontal addressing
182  command( 0x0C ); // normal mode
183  }
184 };
185 
187 template<
188  typename sce,
189  typename res,
190  typename dc,
191  typename sdin,
192  typename sclk
193 > class lcd3310 : public pcd8544< sce, res, dc, sdin, sclk, 84, 48 > {
194 
195  void command( unsigned char d ){
197  }
198 
199 public:
200  lcd3310(){
201  // initialization according to the Olimex example
202 
203  command( 0x21 ); // select exteded instructions
204  command( 0xC8 ); // Vop = 110000b
205  command( 0x06 ); // TCx = 00b
206  command( 0x12 ); // BSx = 100b, LCD bias mode 1:48.
207 
208  command( 0x04 | 1 ); // mystery command, set temp S6 for start line?
209  command( 0x40 | (64 - 3 ) ); // mystery command, set temp S[5:0] for start line?
210 
211  command( 0x20 ); // select basic instructions, horizontal addressing
212  command( 0x0C ); // normal mode
213  }
214 
215 };
216 
217 }; // namespace chips
218 }; // namespace bmptk