1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Driver for Sharp Memory Displays.
10 This driver uses a bit-banged 3-pin SPI interface. For the SPI
11 interface, the select line (CS) is active high, and the clock
12 line (SCK) is active high.
14 Note: The LCD expects the address and data
15 bits to arrive LSB, though the M3 shifts the bits out MSB so they
16 need to be switched in SW first.
24 // Render some text and a line into the image buffer
26 sharpmemDrawString(1, 10, "5x8 System", Font_System5x8);
27 sharpmemDrawString(1, 20, "7x8 System", Font_System7x8);
28 for (i = 0; i < 96; i++)
30 sharpmemDrawPixel(i, i);
35 // Screen must be refreshed at least once per second
44 Software License Agreement (BSD License)
46 Copyright (c) 2010, microBuilder SARL
49 Redistribution and use in source and binary forms, with or without
50 modification, are permitted provided that the following conditions are met:
51 1. Redistributions of source code must retain the above copyright
52 notice, this list of conditions and the following disclaimer.
53 2. Redistributions in binary form must reproduce the above copyright
54 notice, this list of conditions and the following disclaimer in the
55 documentation and/or other materials provided with the distribution.
56 3. Neither the name of the copyright holders nor the
57 names of its contributors may be used to endorse or promote products
58 derived from this software without specific prior written permission.
60 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
61 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
62 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
63 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
64 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
66 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
67 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
69 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 /**************************************************************************/
76 #include "core/systick/systick.h"
77 #include "core/gpio/gpio.h"
78 #include "drivers/displays/smallfonts.h"
80 #define TOGGLE_VCOM do { _sharpmem_vcom = _sharpmem_vcom ? 0x00 : SHARPMEM_BIT_VCOM; } while(0);
82 static uint8_t _sharpmembuffer
[(SHARPMEM_LCDWIDTH
* SHARPMEM_LCDHEIGHT
) / 8];
83 static volatile uint8_t _sharpmem_vcom
= SHARPMEM_BIT_VCOM
;
85 /*************************************************/
87 /*************************************************/
89 /**************************************************************************/
91 @brief Swaps the bit order from MSB to LSB, since the LCD expects LSB,
92 but the M3 shifts bits out MSB.
94 /**************************************************************************/
95 uint8_t sharpmemSwap(uint8_t data
)
100 if(data
& 0x01) out
|= 0x80;
101 if(data
& 0x02) out
|= 0x40;
102 if(data
& 0x04) out
|= 0x20;
103 if(data
& 0x08) out
|= 0x10;
104 if(data
& 0x10) out
|= 0x08;
105 if(data
& 0x20) out
|= 0x04;
106 if(data
& 0x40) out
|= 0x02;
107 if(data
& 0x80) out
|= 0x01;
113 /*************************************************/
114 void sharpmemSendByte(uint8_t data
)
118 // Make sure clock pin starts low
121 // Write from MSB to LSB
122 // LCD expects LSB first
133 // Clock is active high
136 __asm
volatile("nop");
141 /**************************************************************************/
143 @brief Draws a single graphic character using the supplied font
145 /**************************************************************************/
146 static void sharpmemDrawChar(uint16_t x
, uint16_t y
, const char c
, struct FONT_DEF font
)
148 uint8_t col
, column
[font
.u8Width
];
150 // Check if the requested character is available
151 if ((c
>= font
.u8FirstChar
) && (c
<= font
.u8LastChar
))
153 // Retrieve appropriate columns from font data
154 for (col
= 0; col
< font
.u8Width
; col
++)
156 column
[col
] = font
.au8FontTable
[((c
- 32) * font
.u8Width
) + col
]; // Get first column of appropriate character
161 // Requested character is not available in this font ... send a space instead
162 for (col
= 0; col
< font
.u8Width
; col
++)
164 column
[col
] = 0xFF; // Send solid space
168 // Render each column
169 uint16_t xoffset
, yoffset
;
170 for (xoffset
= 0; xoffset
< font
.u8Width
; xoffset
++)
172 for (yoffset
= 0; yoffset
< (font
.u8Height
+ 1); yoffset
++)
175 bit
= (column
[xoffset
] << (8 - (yoffset
+ 1))); // Shift current row bit left
176 bit
= (bit
>> 7); // Shift current row but right (results in 0x01 for black, and 0x00 for white)
179 sharpmemDrawPixel(x
+ xoffset
, y
+ yoffset
);
185 /*************************************************/
187 /*************************************************/
189 /*************************************************/
190 void sharpmemInit(void)
192 // Set control pins to output
193 gpioSetDir(SHARPMEM_PORT
, SHARPMEM_SCLK_PIN
, 1);
194 gpioSetDir(SHARPMEM_PORT
, SHARPMEM_MOSI_PIN
, 1);
195 gpioSetDir(SHARPMEM_PORT
, SHARPMEM_CS_PIN
, 1);
196 gpioSetDir(SHARPMEM_PORT
, SHARPMEM_DISP_PIN
, 1);
198 // Set pins to default state
204 // Set the vcom bit to a defined state
205 _sharpmem_vcom
= SHARPMEM_BIT_VCOM
;
207 // Clear the display and turn it off by default
208 sharpmemClearScreen();
209 sharpmemEnable(false);
212 /**************************************************************************/
214 @brief Turns the display on or off (memory is retained even when the
218 Whether the display should be on (TRUE/1) or off (FALSE/0)
220 /**************************************************************************/
221 void sharpmemEnable(bool enable
)
233 /**************************************************************************/
235 @brief Draws a single pixel in image buffer
238 The x position (0 based)
240 The y position (0 based)
242 /**************************************************************************/
243 void sharpmemDrawPixel(uint16_t x
, uint16_t y
)
245 if ((x
>= SHARPMEM_LCDWIDTH
) || (y
>= SHARPMEM_LCDHEIGHT
))
248 _sharpmembuffer
[(y
*SHARPMEM_LCDWIDTH
+ x
) /8] |= (1 << x
% 8);
251 /**************************************************************************/
253 @brief Clears a single pixel in image buffer
256 The x position (0 based)
258 The y position (0 based)
260 /**************************************************************************/
261 void sharpmemClearPixel(uint16_t x
, uint16_t y
)
263 if ((x
>= SHARPMEM_LCDWIDTH
) || (y
>= SHARPMEM_LCDHEIGHT
))
266 _sharpmembuffer
[(y
*SHARPMEM_LCDWIDTH
+ x
) /8] &= ~(1 << x
% 8);
269 /**************************************************************************/
271 @brief Gets the value (1 or 0) of the specified pixel from the buffer
274 The x position (0 based)
276 The y position (0 based)
278 @return 1 if the pixel is enabled, 0 if disabled
280 /**************************************************************************/
281 uint8_t sharpmemGetPixel(uint16_t x
, uint16_t y
)
283 if ((x
>=SHARPMEM_LCDWIDTH
) || (y
>=SHARPMEM_LCDHEIGHT
)) return 0;
284 return _sharpmembuffer
[(y
*SHARPMEM_LCDWIDTH
+ x
) /8] & (1 << x
% 8) ? 1 : 0;
287 /**************************************************************************/
289 @brief Clears the screen
291 /**************************************************************************/
292 void sharpmemClearScreen()
294 memset(_sharpmembuffer
, 0x00, (SHARPMEM_LCDWIDTH
* SHARPMEM_LCDHEIGHT
) / 8);
295 // Send the clear screen command rather than doing a HW refresh (quicker)
297 sharpmemSendByte(_sharpmem_vcom
| SHARPMEM_BIT_CLEAR
);
298 sharpmemSendByte(0x00);
303 /**************************************************************************/
305 @brief Renders the contents of the pixel buffer on the LCD
307 /**************************************************************************/
308 void sharpmemRefresh(void)
310 uint16_t i
, totalbytes
, currentline
, oldline
;
311 totalbytes
= (SHARPMEM_LCDWIDTH
* SHARPMEM_LCDHEIGHT
) / 8;
313 // Send the write command
315 sharpmemSendByte(SHARPMEM_BIT_WRITECMD
| _sharpmem_vcom
);
318 // Send the address for line 1
319 oldline
= currentline
= 1;
320 sharpmemSendByte(sharpmemSwap(currentline
));
323 for (i
=0; i
<totalbytes
; i
++)
325 sharpmemSendByte(sharpmemSwap(_sharpmembuffer
[i
]));
326 currentline
= ((i
+1)/(SHARPMEM_LCDWIDTH
/8)) + 1;
327 if(currentline
!= oldline
)
329 // Send end of line and address bytes
330 sharpmemSendByte(0x00);
331 if (currentline
<= SHARPMEM_LCDHEIGHT
)
333 sharpmemSendByte(sharpmemSwap(currentline
));
335 oldline
= currentline
;
339 // Send another trailing 8 bits for the last line
340 sharpmemSendByte(0x00);
344 /**************************************************************************/
346 @brief Draws a string using the supplied font data.
349 Starting x co-ordinate
351 Starting y co-ordinate
355 Pointer to the FONT_DEF to use when drawing the string
361 #include "drivers/displays/bitmap/sharpmem/sharpmem.h"
362 #include "drivers/displays/smallfonts.h"
364 // Configure the pins and initialise the LCD screen
367 // Render some text on the screen
368 sharpmemDrawString(1, 10, "5x8 System", Font_System5x8);
369 sharpmemDrawString(1, 20, "7x8 System", Font_System7x8);
371 // Refresh the screen to see the results
376 /**************************************************************************/
377 void sharpmemDrawString(uint16_t x
, uint16_t y
, const char* text
, struct FONT_DEF font
)
380 for (l
= 0; l
< strlen(text
); l
++)
382 sharpmemDrawChar(x
+ (l
* (font
.u8Width
+ 1)), y
, text
[l
], font
);
This page took 0.072801 seconds and 5 git commands to generate.