4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
32 * \brief 7 segments LED displays (impl)
34 * \author Fabio Bizzi <fbizzi@bizzi.org>
36 * \addtogroup SevenSegDisplay 7 Segments LED Displays Driver
41 #include "drv/led_7seg.h"
42 #include "hw/hw_led_7seg.h"
43 #include "cfg/cfg_arch.h"
44 #include <drv/timer.h>
48 * Define the timer for the refreshing
50 static Timer sseg_trefresh;
53 * FUNCTION: sseg_refresh
55 * This is the procedure that prints the seven_seg structure'string to the display.
56 * It prints a single digit at time and does all the checks to a proper display.
57 * It is called by the wrapper function fired by the timer set in the init procedure.
59 * param SS The void pointer that holds the pointer to the data structure
62 static void sseg_refresh(SevenSeg *SS)
64 /* First Check if the structure is being edited we do a silent exit*/
65 if (SS->busyedit == true)
68 /* If we have displayed all the digits */
69 if (SS->curdigit == CONFIG_LED_7SEG_DIGIT)
72 /* And if we have to display again all the digit */
77 SS->curpos -= CONFIG_LED_7SEG_DIGIT;
79 /* Else is time to scroll the text */
82 /* If we aren't at the end of the string */
83 if (SS->curpos < SS->string_len)
85 SS->curpos -= CONFIG_LED_7SEG_DIGIT;
88 SS->curspeed = SS->speed;
90 /* Else we are at the end of the string */
93 /* Set that the string was displayed at least one time and we can accept a new text*/
95 /* If the string has to be displayed only one time we set an empty string
96 * to display till next print request*/
97 if (SS->runonce == true)
99 /* Else we reset the text to it's first character ad do again the display */
104 SS->curspeed = SS->speed;
109 /* Else We have to do a print*/
112 /* If the text doesn't have to blink we write the character to the proper disply's digit */
113 if (SS->blink == false)
114 sseg_on(SS->string[SS->curpos], SS->curdigit);
115 /* Else we do the blinking */
118 /* If bdigit == 0 we have to blink all the digits */
121 if (SS->curspeed >= (SS->speed/2))
122 sseg_on(SS->string[SS->curpos], SS->curdigit);
126 /* else we have to blink only one digit (bdigit -1) */
128 /* is this the digit to blink? */
129 if (SS->curdigit == ((unsigned int)SS->bdigit-1))
131 if (SS->curspeed >= (SS->speed/2))
132 sseg_on(SS->string[SS->curpos], SS->curdigit);
136 /* no, so let do a normal print */
138 sseg_on(SS->string[SS->curpos], SS->curdigit);
140 /* Ok, next time next char.... ;) */
147 * FUNCTION: sseg_refresh_wrapper
149 * This is a "wrapper" procedure that is called by the timer_setSoftint()
150 * with the unique purpose to call the real sseg_refresh procedure without
151 * the cast of the structure from void to SevenSeg.
153 * param VSS The void pointer that holds the pointer to the data structure
156 static void sseg_refresh_wrapper(void *VSS)
158 /* Here we cast the Structure from void to proper type */
160 SS = (SevenSeg *)VSS;
161 /* now we call the right refresh routine */
163 /* ReStart the timer */
164 timer_add(&sseg_trefresh);
168 * FUNCTION: sseg_tabcheck
170 * This function return the position of the ascii character in the hex
173 * param source The ascii char to be positioned
175 INLINE uint8_t sseg_tabcheck(char source)
180 if ((((int)source) > 47) && (((int)source) < 58))
183 /* Capital Letters */
184 if ((((int)source) > 64) && (((int)source) < 91))
188 if ((((int)source) > 96) && (((int)source) < 123))
192 if (((int)source) == 45)
196 if (((int)source) == 32)
200 if (((int)source) == 46)
206 * \brief Print a string on the display
208 * This is the procedure that fills the seven_seg structure with the translated
209 * string to display. It swaps also the structures to display the new text when
210 * all the data is ready to display.
212 * \param SS Pointer to the SevenSeg structure
213 * \param sstring String to be displayed
215 * \return 0 if all went well, -1 if the display is locked, -2 if the string too long.
217 int sevenseg_print(SevenSeg *SS, const char *sstring)
219 size_t string_lenght;
220 unsigned int x,y,dotnumber;
221 bool dotjump = false;
224 /* Check if the display is unlocked */
225 if (SS->busyedit == false)
228 /* Check if the string is too big */
229 if (sizeof(&sstring) > (CONFIG_LED_7SEG_STRLEN-(2*CONFIG_LED_7SEG_DIGIT)))
232 /* get the string length and set the number of dots in the string to 0 */
233 string_lenght = strlen(sstring);
236 /* check if there are some dots in the string and report the number in dotnumber */
237 for (x=0;x<(unsigned int)string_lenght;x++)
239 if (((int)sstring[x]) == 46)
243 /* If the *REAL* lenght of the string is less or equal than the number of digits */
244 if (((int)string_lenght-dotnumber) <= CONFIG_LED_7SEG_DIGIT)
246 /* If the *REAL* lenght of the string is less than number of digits */
247 if (((int)string_lenght-dotnumber) < CONFIG_LED_7SEG_DIGIT)
249 /* Fill the left side of the string with blanks */
250 for (x=0; x<(CONFIG_LED_7SEG_DIGIT-((int)string_lenght-dotnumber)); x++)
251 SS->string[x] = segstable[38];
256 /* Else we have the exact string length of the display */
262 /* Else we have the string length bigger than the display and we need to fill
263 * the entire left side of the string with blanks to begin the scroll from the
264 * rigthest side of the display */
265 for (x=0; x<CONFIG_LED_7SEG_DIGIT; x++)
266 SS->string[x] = segstable[38];
267 y = CONFIG_LED_7SEG_DIGIT;
269 /* Here we start to fill the string with the Hex 7seg characters values */
271 for (x=0; x<(unsigned int)string_lenght; x++)
273 hexchar = sseg_tabcheck(sstring[x]);
274 /* do we have a dot? */
277 /* If we are at the first character of the string it has to be forced
278 * as "lonly" dot ;) */
281 #if CONFIG_LED_7SEG_CCAT
282 SS->string[y-1] = SS->string[y-1] | segstable[(int)hexchar];
284 SS->string[y-1] = SS->string[y-1] & segstable[(int)hexchar];
289 /* If the last character was a dot and we aren't at the first character of the string
290 * we have just inserted it */
293 /* Let's put the character in the structure's string */
296 SS->string[y] = segstable[(int)hexchar];
300 /* If we have the string length bigger than the display we need to fill
301 * the entire right side of the string with blanks to end the scroll
302 * to the rigthest side of the display */
303 if (((int)string_lenght-dotnumber) > CONFIG_LED_7SEG_DIGIT)
305 for (x=0; x<CONFIG_LED_7SEG_DIGIT; x++)
307 SS->string[y] = segstable[38];
311 /* Let's put the total string length to the structure */
318 * \brief initialize the structure and the timer for the display
320 * This is the procedure that inits all the structures that rules the 7 segments
321 * display and set the timer for the proper print/refresh of the text.
323 * \param SS Pointer to the SevenSeg structure
325 void sevenseg_init(SevenSeg *SS)
328 * Init the 7segments string structure
332 SS->busyedit = false;
335 * Init the I/O ports and set the display OFF
340 * Define the timer for the refresh of the display
341 * The timer calls the sevenseg_refresh function
342 * every "CONFIG_LED_7SEG_RTIME" milliseconds for
343 * an acceptable persistance of a single 7 segments
347 timer_setSoftint(&sseg_trefresh, sseg_refresh_wrapper, (void *)SS);
349 timer_setDelay(&sseg_trefresh, ms_to_ticks(CONFIG_LED_7SEG_RTIME));
351 timer_add(&sseg_trefresh);
355 * \brief check if is possible to do a new print
357 * This is the procedure that check if the print of the current text is run
358 * almost one time and we're ready to print a new text.
360 * \param SS Pointer to the SevenSeg structure
362 * \return true if we can print a new text, false if we're still printing the previous text for the first time and we have to wait.
364 bool sevenseg_isReady(SevenSeg *SS)
366 return !SS->firstrun;
370 * \brief unlock the SevenSeg structure and stops the print
372 * This is the procedure that check if the print of the current text is run
373 * almost one time and then set the status of the display to "unlocked".
375 * \param SS Pointer to the SevenSeg structure
377 * \return true if the display is unlocked, false if the dispaly is still locked.
379 bool sevenseg_unlock(SevenSeg *SS)
381 if (SS->firstrun == false)
394 * \brief lock the SeveSeg structure and starts a new print
396 * This is the procedure that lock the display and permit
397 * the print of the text.
399 * \param SS Pointer to the SevenSeg structure
401 * \return true if the display is now locked, false if the display was already locked.
403 bool sevenseg_lock(SevenSeg *SS)
405 if (SS->busyedit == true)
407 /* If the string is longer than the number of the digit of the display we
408 * reset the single digit blink to zero to be sure that the display of
409 * the text is clean from previous single digit blinking settings */
410 if (SS->string_len > CONFIG_LED_7SEG_DIGIT)
412 SS->busyedit = false;
420 * \brief set the blinking of the digits of the display
422 * This is the procedure that set the blinking of the display.
423 * You can choose to blink all the display or only a single
426 * \param SS Pointer to the SevenSeg structure
427 * \param blink if true the display will blink
428 * \param digit if 0 all the digits have to blink, else the digit that has to blink
430 * \return true if the set was succesfull, false if the set was not succesfull.
432 bool sevenseg_setBlink(SevenSeg *SS, bool blink, uint8_t digit)
434 if (SS->busyedit == true)
441 if ((digit-1) <= CONFIG_LED_7SEG_DIGIT)
454 * \brief set if the text has to be displayed just one time
456 * This is the procedure that set if the text has to be displayed
459 * \param SS Pointer to the SevenSeg structure
460 * \param runonce true if the text has to be displayed only one time, false if the text has to be displayed till next print
462 * \return true if the set was succesfull, false if the set was not succesfull.
464 bool sevenseg_setRunonce(SevenSeg *SS, bool runonce)
466 if (SS->busyedit == true)
467 SS->runonce = runonce;
474 * \brief set the scrolling speed of the text
476 * This is the procedure that set the scrolling speed of the text
477 * if the text is longer than the display digits or the
478 * duration of the display if the text is smaller or equal the
479 * length of display digits.
481 * \param SS Pointer to the SevenSeg structure
482 * \param r_speed the Scrolling speed or display time
484 * \return true if the set was succesfull, false if the set was not succesfull.
486 bool sevenseg_setRunspeed(SevenSeg *SS, unsigned int r_speed)
488 if (SS->busyedit == true)
491 SS->curspeed = r_speed;
499 * \brief clear the display
501 * This is the procedure that blanks the text to be displayed
502 * and so on clear the display.
504 * \param SS Pointer to the SevenSeg structure
506 * \return true if the clear was succesfull, false if the clear was not succesfull.
508 bool sevenseg_clear(SevenSeg *SS)
510 if (SS->busyedit == true)
512 memset(((void *)&SS->string),segstable[38],sizeof(SS->string));
513 SS->string_len = CONFIG_LED_7SEG_DIGIT;
519 SS->speed = CONFIG_LED_7SEG_SSPEED;
520 SS->curspeed = CONFIG_LED_7SEG_SSPEED;
521 SS->firstrun = false;
527 /** \} */ //defgroup drivers