led_7seg.c
Go to the documentation of this file.00001
00041 #include "drv/led_7seg.h"
00042 #include "hw/hw_led_7seg.h"
00043 #include "cfg/cfg_arch.h"
00044 #include <drv/timer.h>
00045 #include <string.h>
00046
00047
00048
00049
00050 static Timer sseg_trefresh;
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 static void sseg_refresh(SevenSeg *SS)
00063 {
00064
00065 if (SS->busyedit == true)
00066 return;
00067
00068
00069 if (SS->curdigit == CONFIG_LED_7SEG_DIGIT)
00070 {
00071 sseg_off();
00072
00073 if (SS->curspeed > 0)
00074 {
00075 SS->curspeed--;
00076 SS->curdigit = 0;
00077 SS->curpos -= CONFIG_LED_7SEG_DIGIT;
00078 }
00079
00080 else
00081 {
00082
00083 if (SS->curpos < SS->string_len)
00084 {
00085 SS->curpos -= CONFIG_LED_7SEG_DIGIT;
00086 SS->curpos++;
00087 SS->curdigit = 0;
00088 SS->curspeed = SS->speed;
00089 }
00090
00091 else
00092 {
00093
00094 SS->firstrun = false;
00095
00096
00097 if (SS->runonce == true)
00098 sevenseg_clear(SS);
00099
00100 else
00101 {
00102 SS->curdigit = 0;
00103 SS->curpos = 0;
00104 SS->curspeed = SS->speed;
00105 }
00106 }
00107 }
00108 }
00109
00110 else
00111 {
00112
00113 if (SS->blink == false)
00114 sseg_on(SS->string[SS->curpos], SS->curdigit);
00115
00116 else
00117 {
00118
00119 if (SS->bdigit == 0)
00120 {
00121 if (SS->curspeed >= (SS->speed/2))
00122 sseg_on(SS->string[SS->curpos], SS->curdigit);
00123 else
00124 sseg_off();
00125 }
00126
00127 else
00128
00129 if (SS->curdigit == ((unsigned int)SS->bdigit-1))
00130 {
00131 if (SS->curspeed >= (SS->speed/2))
00132 sseg_on(SS->string[SS->curpos], SS->curdigit);
00133 else
00134 sseg_off();
00135 }
00136
00137 else
00138 sseg_on(SS->string[SS->curpos], SS->curdigit);
00139 }
00140
00141 SS->curdigit++;
00142 SS->curpos++;
00143 }
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 static void sseg_refresh_wrapper(void *VSS)
00157 {
00158
00159 SevenSeg *SS;
00160 SS = (SevenSeg *)VSS;
00161
00162 sseg_refresh(SS);
00163
00164 timer_add(&sseg_trefresh);
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 INLINE uint8_t sseg_tabcheck(char source)
00176 {
00177 uint8_t hexchar=38;
00178
00179
00180 if ((((int)source) > 47) && (((int)source) < 58))
00181 hexchar = source-48;
00182 else
00183
00184 if ((((int)source) > 64) && (((int)source) < 91))
00185 hexchar = source-53;
00186 else
00187
00188 if ((((int)source) > 96) && (((int)source) < 123))
00189 hexchar = source-85;
00190 else
00191
00192 if (((int)source) == 45)
00193 hexchar = 11;
00194 else
00195
00196 if (((int)source) == 32)
00197 hexchar = 38;
00198 else
00199
00200 if (((int)source) == 46)
00201 hexchar = 10;
00202 return hexchar;
00203 }
00204
00217 int sevenseg_print(SevenSeg *SS, const char *sstring)
00218 {
00219 size_t string_lenght;
00220 unsigned int x,y,dotnumber;
00221 bool dotjump = false;
00222 uint8_t hexchar;
00223
00224
00225 if (SS->busyedit == false)
00226 return -1;
00227
00228
00229 if (sizeof(&sstring) > (CONFIG_LED_7SEG_STRLEN-(2*CONFIG_LED_7SEG_DIGIT)))
00230 return -2;
00231
00232
00233 string_lenght = strlen(sstring);
00234 dotnumber = 0;
00235
00236
00237 for (x=0;x<(unsigned int)string_lenght;x++)
00238 {
00239 if (((int)sstring[x]) == 46)
00240 dotnumber++;
00241 }
00242
00243
00244 if (((int)string_lenght-dotnumber) <= CONFIG_LED_7SEG_DIGIT)
00245 {
00246
00247 if (((int)string_lenght-dotnumber) < CONFIG_LED_7SEG_DIGIT)
00248 {
00249
00250 for (x=0; x<(CONFIG_LED_7SEG_DIGIT-((int)string_lenght-dotnumber)); x++)
00251 SS->string[x] = segstable[38];
00252 y = x;
00253 }
00254 else
00255 {
00256
00257 y = 0;
00258 }
00259 }
00260 else
00261 {
00262
00263
00264
00265 for (x=0; x<CONFIG_LED_7SEG_DIGIT; x++)
00266 SS->string[x] = segstable[38];
00267 y = CONFIG_LED_7SEG_DIGIT;
00268 }
00269
00270 hexchar = 0;
00271 for (x=0; x<(unsigned int)string_lenght; x++)
00272 {
00273 hexchar = sseg_tabcheck(sstring[x]);
00274
00275 if (hexchar == 10)
00276 {
00277
00278
00279 if (x > 0)
00280 {
00281 #if CONFIG_LED_7SEG_CCAT
00282 SS->string[y-1] = SS->string[y-1] | segstable[(int)hexchar];
00283 #else
00284 SS->string[y-1] = SS->string[y-1] & segstable[(int)hexchar];
00285 #endif
00286 dotjump = true;
00287 }
00288 }
00289
00290
00291 if (dotjump)
00292 dotjump = false;
00293
00294 else
00295 {
00296 SS->string[y] = segstable[(int)hexchar];
00297 y++;
00298 }
00299 }
00300
00301
00302
00303 if (((int)string_lenght-dotnumber) > CONFIG_LED_7SEG_DIGIT)
00304 {
00305 for (x=0; x<CONFIG_LED_7SEG_DIGIT; x++)
00306 {
00307 SS->string[y] = segstable[38];
00308 y++;
00309 }
00310 }
00311
00312 SS->string_len = y;
00313
00314 return 0;
00315 }
00316
00325 void sevenseg_init(SevenSeg *SS)
00326 {
00327
00328
00329
00330 SS->busyedit = true;
00331 sevenseg_clear(SS);
00332 SS->busyedit = false;
00333
00334
00335
00336
00337 sseg_init();
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 timer_setSoftint(&sseg_trefresh, sseg_refresh_wrapper, (void *)SS);
00348
00349 timer_setDelay(&sseg_trefresh, ms_to_ticks(CONFIG_LED_7SEG_RTIME));
00350
00351 timer_add(&sseg_trefresh);
00352 }
00353
00364 bool sevenseg_isReady(SevenSeg *SS)
00365 {
00366 return !SS->firstrun;
00367 }
00368
00379 bool sevenseg_unlock(SevenSeg *SS)
00380 {
00381 if (SS->firstrun == false)
00382 {
00383 SS->busyedit = true;
00384 SS->firstrun = true;
00385 SS->curdigit = 0;
00386 SS->curpos = 0;
00387 }
00388 else
00389 return false;
00390 return true;
00391 }
00392
00403 bool sevenseg_lock(SevenSeg *SS)
00404 {
00405 if (SS->busyedit == true)
00406 {
00407
00408
00409
00410 if (SS->string_len > CONFIG_LED_7SEG_DIGIT)
00411 SS->bdigit = 0;
00412 SS->busyedit = false;
00413 }
00414 else
00415 return false;
00416 return true;
00417 }
00418
00432 bool sevenseg_setBlink(SevenSeg *SS, bool blink, uint8_t digit)
00433 {
00434 if (SS->busyedit == true)
00435 {
00436 if (blink == true)
00437 {
00438 if (digit == 0)
00439 SS->bdigit = digit;
00440 else
00441 if ((digit-1) <= CONFIG_LED_7SEG_DIGIT)
00442 SS->bdigit = digit;
00443 else
00444 return false;
00445 }
00446 SS->blink = blink;
00447 }
00448 else
00449 return false;
00450 return true;
00451 }
00452
00464 bool sevenseg_setRunonce(SevenSeg *SS, bool runonce)
00465 {
00466 if (SS->busyedit == true)
00467 SS->runonce = runonce;
00468 else
00469 return false;
00470 return true;
00471 }
00472
00486 bool sevenseg_setRunspeed(SevenSeg *SS, unsigned int r_speed)
00487 {
00488 if (SS->busyedit == true)
00489 {
00490 SS->speed = r_speed;
00491 SS->curspeed = r_speed;
00492 }
00493 else
00494 return false;
00495 return true;
00496 }
00497
00508 bool sevenseg_clear(SevenSeg *SS)
00509 {
00510 if (SS->busyedit == true)
00511 {
00512 memset(((void *)&SS->string),segstable[38],sizeof(SS->string));
00513 SS->string_len = CONFIG_LED_7SEG_DIGIT;
00514 SS->blink = false;
00515 SS->bdigit = 0;
00516 SS->runonce = false;
00517 SS->curdigit = 0;
00518 SS->curpos = 0;
00519 SS->speed = CONFIG_LED_7SEG_SSPEED;
00520 SS->curspeed = CONFIG_LED_7SEG_SSPEED;
00521 SS->firstrun = false;
00522 }
00523 else
00524 return false;
00525 return true;
00526 }