Add seven segment LED driver.
[bertos.git] / bertos / drv / led_7seg.h
1 /**
2  * \file led_7seg.h
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \author Fabio Bizzi <fbizzi@bizzi.org>
33  *
34  * \defgroup SevenSegDisplay 7 Segments LED Displays Driver
35  * \ingroup drivers
36  * \{
37  *
38  * \brief 7 segments LED displays (headers)
39  *
40  * Here you find the prototypes and the data structures that
41  * format and holds the text that has to be displayed by the
42  * 7 segments display.
43  *
44  * The main function is the sevenseg_print()
45  * that is called by your software to display the text, also
46  * important is the sevenseg_init() that initialize the data
47  * structures and set the refresh timer, you need to call
48  * this procedure just one time in the init procedure of
49  * your software before any use of the sevenseg_print().
50  *
51  * Usage:
52  *
53  * To use succesfully the display you have to follow these steps:
54  *
55  * \li Create the structure and init the display
56  * \li Check if the display is ready to accept a new print with
57  *    the function sevenseg_isReady()
58  * \li Unlock the display with the function sevenseg_unlock()
59  *    NOTE: when the display is unlocked the displaying of any
60  *    text is stopped but the data (text and attributes like text
61  *    position, blinking etc etc) are not erased.
62  * \li Set the wanted text attributes with sevenseg_set* functions
63  * \li Print the wanted text with sevenseg_print()
64  * \li Lock the display with sevenseg_lock()
65  *
66  * When the display is locked the displaying of the text starts.
67  *
68  *
69  * \code
70  * static Seven_Seg display;
71  * // ...
72  * sevenseg_init(&display);
73  * while (!sevenseg_isReady(&display))
74  *    ;
75  * sevenseg_unlock(&display);
76  * sevenseg_setBlink(&display,false,0);
77  * sevenseg_setRunonce(&display,false);
78  * sevenseg_setRunspeed(&display,10);
79  * if ((sevenseg_print(&display, "made with bertos.")) != 0)
80  *              return -1;
81  * sevenseg_lock(&display);
82  * \endcode
83  *
84  * $WIZ$ module_name = "led_7seg"
85  * $WIZ$ module_depends = "timer"
86  * $WIZ$ module_configuration = "bertos/cfg/cfg_led_7seg.h"
87  * $WIZ$ module_hw = "bertos/hw/hw_led_7seg.h"
88  */
89
90 #ifndef DRV_LED_7SEG_H
91 #define DRV_LED_7SEG_H
92
93 #include "cfg/cfg_led_7seg.h"
94 #include <drv/timer.h>
95 #include <mware/event.h>
96
97 /**
98  * \name Numbers and Letters Table.
99  * \note These tables contain all the printable
100  *       characters on a 7 segment digit encoded
101  *       for common cathode and common anode
102  *       display type.
103  *
104  * 0, 1, 2, 3, 4, 5, 6, 7, 8,
105  * 9, ., -, A, B, C, D, E, F,
106  * G, H, I, J, K, L, M, N, O,
107  * P, Q, R, S, T, U, V, W, X,
108  * Y, Z, SPACE
109  *
110  * \{
111  */
112 #if CONFIG_LED_7SEG_CCAT
113                 static const uint8_t segstable[] =
114         {
115                 0x3f, 0x6, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x7, 0x7f,
116                                 0x6f, 0x80, 0x40, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71,
117                                 0x3d, 0x74, 0x30, 0x1e, 0x75, 0x38, 0x15, 0x37, 0x3f,
118                                 0x73, 0x67, 0x50, 0x6d, 0x78, 0x3e, 0x2a, 0x6a, 0x76,
119                                 0x6e, 0x5b, 0x0
120         };
121 #else
122                 static const uint8_t segstable[] =
123         {
124                                 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80,
125                                 0x90, 0x7f, 0xbf, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e,
126                                 0xc2, 0x8b, 0xcf, 0xe1, 0x8a, 0xc7, 0xea, 0xc8, 0xc0,
127                                 0x8c, 0x98, 0xaf, 0x92, 0x87, 0xc1, 0xd5, 0x95, 0x89,
128                                 0x91, 0xa4, 0xff
129                 };
130 #endif
131 /**\} */
132
133 /**
134  * Context data for Display functions
135  *
136  */
137 typedef struct SevenSeg
138 {
139                 /** The String to be displayed */
140                 uint8_t string[CONFIG_LED_7SEG_STRLEN];
141                 /** The length of the string to be displayed */
142                 unsigned int string_len;
143                 /** Has to blink? */
144                 bool blink;
145                 /** Has to blink only one digit? */
146                 uint8_t bdigit;
147                 /** Has to be displayed only one time? */
148                 bool runonce;
149                 /** Scrolling speed */
150                 unsigned int speed;
151                 /** Working scrolling speed */
152                 unsigned int curspeed;
153                 /** Is it printed at least one time? */
154                 bool firstrun;
155                 /** Working current position */
156                 unsigned int curpos;
157                 /** Working current digit */
158                 unsigned int curdigit;
159                 /** Is the structure in edit? */
160                 bool busyedit;
161 } SevenSeg;
162
163 /* Functions prototypes */
164
165 /*
166  * This is the procedure that fills the seven_seg structure with the translated
167  * string to display. It swaps also the structures to display the new text when
168  * all the data is ready to display.
169  */
170 int sevenseg_print(SevenSeg *SS, const char *sstring);
171
172 /*
173  * This is the procedure that inits all the structures that rules the 7 segments
174  * display and set the timer for the proper print/refresh of the text.
175  */
176 void sevenseg_init(SevenSeg *SS);
177
178 /*
179  * This is the procedure that does a short print of all segments of all
180  * digits of the display.
181  */
182 void sevenseg_test(SevenSeg *SS);
183
184 /*
185  * This is the procedure that check if the print of the current text is run
186  * almost one time and we're ready to print a new text.
187  */
188 bool sevenseg_isReady(SevenSeg *SS);
189
190 /*
191  * This is the procedure that check if the print of the current text is run
192  * almost one time and then set the status of the display to "unlocked".
193  */
194 bool sevenseg_unlock(SevenSeg *SS);
195
196 /*
197  * This is the procedure that lock the display and permit
198  * the print of the text.
199  */
200 bool sevenseg_lock(SevenSeg *SS);
201
202 /*
203  * This is the procedure that set the blinking of the display.
204  * You can choose to blink all the display or only a single
205  * digit.
206  */
207 bool sevenseg_setBlink(SevenSeg *SS, bool blink, uint8_t digit);
208
209 /*
210  * This is the procedure that set if the text has to be displayed
211  * just one time
212  */
213 bool sevenseg_setRunonce(SevenSeg *SS, bool runonce);
214
215 /*
216  * This is the procedure that set the scrolling speed of the text
217  * if the text is longer than the display digits or the
218  * duration of the display if the text is smaller or equal the
219  * length of display digits.
220  */
221 bool sevenseg_setRunspeed(SevenSeg *SS, unsigned int r_speed);
222
223 /*
224  * This is the procedure that blanks the text to be displayed
225  * and so on clear the display.
226  */
227 bool sevenseg_clear(SevenSeg *SS);
228
229 #endif /* DRV_LED_7SEG_H */
230  /** \} */ //defgroup drivers