Booten in Titelbild.
[hackover2013-badge-firmware.git] / drivers / displays / character / samsung_20T202DA2JA / samsung_20T202DA2JA.c
1 /**************************************************************************/
2 /*!
3 @file samsung_20T202DA2JA.c
4 @author Original source : Limor Fried/ladyada (Adafruit Industries)
5 LPC1343 port : K. Townsend
6
7 @section DESCRIPTION
8
9 Driver for Samsung 20T202DA2JA 20x2 VFD display module.
10
11 This driver is based on the SPI_VFD Library from Limor Fried
12 (Adafruit Industries) at: https://github.com/adafruit/SPI_VFD
13
14 The displays can be purchased at:
15 https://www.adafruit.com/products/347
16
17 @section LICENSE
18
19 Software License Agreement (BSD License)
20
21 Copyright (c) 2012 Adafruit Industries
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26 1. Redistributions of source code must retain the above copyright
27 notice, this list of conditions and the following disclaimer.
28 2. Redistributions in binary form must reproduce the above copyright
29 notice, this list of conditions and the following disclaimer in the
30 documentation and/or other materials provided with the distribution.
31 3. Neither the name of the copyright holders nor the
32 names of its contributors may be used to endorse or promote products
33 derived from this software without specific prior written permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
36 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
39 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 */
46 /**************************************************************************/
47 #include <string.h>
48
49 #include "samsung_20T202DA2JA.h"
50
51 #include "core/gpio/gpio.h"
52 #include "core/systick/systick.h"
53
54 /**************************************************************************/
55 /* Private Methods */
56 /**************************************************************************/
57
58 uint8_t samsungvfd_displayfunction;
59 uint8_t samsungvfd_displaycontrol;
60 uint8_t samsungvfd_displaymode;
61 uint8_t samsungvfd_initialized;
62 uint8_t samsungvfd_numlines, samsungvfd_currline;
63
64 /**************************************************************************/
65 /*!
66 @brief Low-level SPI bit-banging routing
67 */
68 /**************************************************************************/
69 static inline void samsungvfd_sendByte(uint8_t c) {
70 int8_t i;
71
72 // Make sure clock pin starts high
73 gpioSetValue(SAMSUNGVFD_SCK_PORT, SAMSUNGVFD_SCK_PIN, 1);
74
75 // Write from MSB to LSB
76 for (i=7; i>=0; i--)
77 {
78 // Set clock pin low
79 gpioSetValue(SAMSUNGVFD_SCK_PORT, SAMSUNGVFD_SCK_PIN, 0);
80 // Set data pin high or low depending on the value of the current bit
81 gpioSetValue(SAMSUNGVFD_SIO_PORT, SAMSUNGVFD_SIO_PIN, c & (1 << i) ? 1 : 0);
82 // Set clock pin high
83 gpioSetValue(SAMSUNGVFD_SCK_PORT, SAMSUNGVFD_SCK_PIN, 1);
84 }
85 }
86
87 /**************************************************************************/
88 /*!
89 @brief Sends a command to the VFD display
90 */
91 /**************************************************************************/
92 void samsungvfd_command(uint8_t value)
93 {
94 gpioSetValue(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, 0);
95 samsungvfd_sendByte(SAMSUNGVFD_SPICOMMAND);
96 samsungvfd_sendByte(value);
97 gpioSetValue(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, 1);
98 }
99
100 /**************************************************************************/
101 /*!
102 @brief Initialises the VFD character display
103 */
104 /**************************************************************************/
105 void samsungvfd_begin(uint8_t cols, uint8_t lines, uint8_t brightness)
106 {
107 // set number of lines
108 if (lines > 1)
109 samsungvfd_displayfunction = SAMSUNGVFD_2LINE;
110 else
111 samsungvfd_displayfunction = SAMSUNGVFD_1LINE;
112
113 if (brightness>SAMSUNGVFD_BRIGHTNESS25) //catch bad values
114 brightness = SAMSUNGVFD_BRIGHTNESS100;
115
116 // set the brightness and push the linecount with VFD_SETFUNCTION
117 samsungvfdSetBrightness(brightness);
118
119 samsungvfd_numlines = lines;
120 samsungvfd_currline = 0;
121
122 // Initialize to default text direction (for romance languages#include "SPI_VFD.h"
123 samsungvfd_displaymode = SAMSUNGVFD_ENTRYLEFT | SAMSUNGVFD_ENTRYSHIFTDECREMENT;
124 // set the entry mode
125 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET | samsungvfd_displaymode);
126
127 samsungvfd_command(SAMSUNGVFD_SETDDRAMADDR); // go to address 0
128
129 // turn the display on with no cursor or blinking default
130 samsungvfd_displaycontrol = SAMSUNGVFD_DISPLAYON;
131 samsungvfdDisplay();
132
133 samsungvfdClear();
134 samsungvfdHome();
135 }
136
137 /**************************************************************************/
138 /* Public Methods */
139 /**************************************************************************/
140
141 /**************************************************************************/
142 /*!
143 @brief Initialises the VFD character display
144 */
145 /**************************************************************************/
146 void samsungvfdInit(uint8_t brightness)
147 {
148 // Set all pins to output
149 gpioSetDir(SAMSUNGVFD_SIO_PORT, SAMSUNGVFD_SIO_PIN, gpioDirection_Output);
150 gpioSetDir(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, gpioDirection_Output);
151 gpioSetDir(SAMSUNGVFD_SCK_PORT, SAMSUNGVFD_SCK_PIN, gpioDirection_Output);
152
153 // Set strobe and clock pins high by default
154 gpioSetValue(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, 1);
155 gpioSetValue(SAMSUNGVFD_SCK_PORT, SAMSUNGVFD_SCK_PIN, 1);
156
157 // Default to 2x20 display (SAMSUNG 20T202DA2JA)
158 samsungvfd_begin(20, 2, brightness);
159 }
160
161 /**************************************************************************/
162 /*!
163 @brief Writes a single character to the VFD display
164 */
165 /**************************************************************************/
166 void samsungvfdWrite(uint8_t value)
167 {
168 gpioSetValue(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, 0);
169 samsungvfd_sendByte(SAMSUNGVFD_SPIDATA);
170 samsungvfd_sendByte(value);
171 gpioSetValue(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, 1);
172 }
173
174 /**************************************************************************/
175 /*!
176 @brief Writes a full string to the VFD display
177 */
178 /**************************************************************************/
179 void samsungvfdWriteString(const char * str)
180 {
181 while(*str)
182 {
183 gpioSetValue(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, 0);
184 samsungvfd_sendByte(SAMSUNGVFD_SPIDATA);
185 samsungvfd_sendByte(*str++);
186 gpioSetValue(SAMSUNGVFD_STB_PORT, SAMSUNGVFD_STB_PIN, 1);
187 }
188 }
189
190 /**************************************************************************/
191 /*!
192 @brief Sets the display brightness
193 */
194 /**************************************************************************/
195 void samsungvfdSetBrightness(uint8_t brightness)
196 {
197 // set the brightness (only if a valid value is passed
198 if (brightness <= SAMSUNGVFD_BRIGHTNESS25)
199 {
200 samsungvfd_displayfunction &= ~SAMSUNGVFD_BRIGHTNESS25;
201 samsungvfd_displayfunction |= brightness;
202
203 samsungvfd_command(SAMSUNGVFD_FUNCTIONSET | samsungvfd_displayfunction);
204 }
205 }
206
207 /**************************************************************************/
208 /*!
209 @brief Quickly turn the display off
210 */
211 /**************************************************************************/
212 void samsungvfdNoDisplay(void)
213 {
214 samsungvfd_displaycontrol &= ~SAMSUNGVFD_DISPLAYON;
215 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL | samsungvfd_displaycontrol);
216 }
217
218 /**************************************************************************/
219 /*!
220 @brief Quickly turn the display on
221 */
222 /**************************************************************************/
223 void samsungvfdDisplay(void)
224 {
225 samsungvfd_displaycontrol |= SAMSUNGVFD_DISPLAYON;
226 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL | samsungvfd_displaycontrol);
227 }
228
229 /**************************************************************************/
230 /*!
231 @brief Turns the cursor off
232 */
233 /**************************************************************************/
234 void samsungvfdNoCursor(void)
235 {
236 samsungvfd_displaycontrol &= ~SAMSUNGVFD_CURSORON;
237 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL | samsungvfd_displaycontrol);
238 }
239
240 /**************************************************************************/
241 /*!
242 @brief Enables the cursor
243 */
244 /**************************************************************************/
245 void samsungvfdCursor(void)
246 {
247 samsungvfd_displaycontrol |= SAMSUNGVFD_CURSORON;
248 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL | samsungvfd_displaycontrol);
249 }
250
251 /**************************************************************************/
252 /*!
253 @brief Disable cursor blinking
254 */
255 /**************************************************************************/
256 void samsungvfdNoBlink(void)
257 {
258 samsungvfd_displaycontrol &= ~SAMSUNGVFD_BLINKON;
259 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL | samsungvfd_displaycontrol);
260 }
261
262 /**************************************************************************/
263 /*!
264 @brief Enable cursor blinking
265 */
266 /**************************************************************************/
267 void samsungvfdBlink(void)
268 {
269 samsungvfd_displaycontrol |= SAMSUNGVFD_BLINKON;
270 samsungvfd_command(SAMSUNGVFD_DISPLAYCONTROL | samsungvfd_displaycontrol);
271 }
272
273 /**************************************************************************/
274 /*!
275 @brief Scroll the display left without changing the RAM
276 */
277 /**************************************************************************/
278 void samsungvfdScrollDisplayLeft(void)
279 {
280 samsungvfd_command(SAMSUNGVFD_CURSORSHIFT | SAMSUNGVFD_DISPLAYMOVE | SAMSUNGVFD_MOVELEFT);
281 }
282
283 /**************************************************************************/
284 /*!
285 @brief Scroll the display right without changing the RAM
286 */
287 /**************************************************************************/
288 void samsungvfdScrollDisplayRight(void)
289 {
290 samsungvfd_command(SAMSUNGVFD_CURSORSHIFT | SAMSUNGVFD_DISPLAYMOVE | SAMSUNGVFD_MOVERIGHT);
291 }
292
293 /**************************************************************************/
294 /*!
295 @brief Cause text to flow left to right
296 */
297 /**************************************************************************/
298 void samsungvfdLeftToRight(void)
299 {
300 samsungvfd_displaymode |= SAMSUNGVFD_ENTRYLEFT;
301 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET | samsungvfd_displaymode);
302 }
303
304 /**************************************************************************/
305 /*!
306 @brief Cause text to flow from right to left
307 */
308 /**************************************************************************/
309 void samsungvfdRightToLeft(void)
310 {
311 samsungvfd_displaymode &= ~SAMSUNGVFD_ENTRYLEFT;
312 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET | samsungvfd_displaymode);
313 }
314
315 /**************************************************************************/
316 /*!
317 @brief Right justify text from the cursor
318 */
319 /**************************************************************************/
320 void samsungvfdAutoscroll(void)
321 {
322 samsungvfd_displaymode |= SAMSUNGVFD_ENTRYSHIFTINCREMENT;
323 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET | samsungvfd_displaymode);
324 }
325
326 /**************************************************************************/
327 /*!
328 @brief Left justify text from the cursor
329 */
330 /**************************************************************************/
331 void samsungvfdNoAutoscroll(void)
332 {
333 samsungvfd_displaymode &= ~SAMSUNGVFD_ENTRYSHIFTINCREMENT;
334 samsungvfd_command(SAMSUNGVFD_ENTRYMODESET | samsungvfd_displaymode);
335 }
336
337 /**************************************************************************/
338 /*!
339 @brief Clears the display
340 */
341 /**************************************************************************/
342 void samsungvfdClear(void)
343 {
344 samsungvfd_command(SAMSUNGVFD_CLEARDISPLAY); // clear display, set cursor position to zero
345 systickDelay(2); // this command takes a long time!
346 }
347
348 /**************************************************************************/
349 /*!
350 @brief Places the cursor at 0, 0
351 */
352 /**************************************************************************/
353 void samsungvfdHome(void)
354 {
355 samsungvfd_command(SAMSUNGVFD_RETURNHOME); // Set cursor position to zero
356 systickDelay(2); // this command takes a long time!
357 }
358
359 /**************************************************************************/
360 /*!
361 @brief Sets the cursor to the specified row and column position
362 */
363 /**************************************************************************/
364 void samsungvfdSetCursor(uint8_t row, uint8_t col)
365 {
366 int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
367 if ( row > samsungvfd_numlines )
368 {
369 row = samsungvfd_numlines-1; // we count rows starting w/0
370 }
371
372 samsungvfd_command(SAMSUNGVFD_SETDDRAMADDR | (col + row_offsets[row]));
373 }
374
This page took 0.065583 seconds and 5 git commands to generate.