Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7segments.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * DD2 Monitoring
  3. *
  4. * ncurses seven segment display
  5. */
  6. /**********************************************************************
  7. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  8. Version 2, December 2004
  9. Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  10. Everyone is permitted to copy and distribute verbatim or modified
  11. copies of this license document, and changing it is allowed as long
  12. as the name is changed.
  13. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  14. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  15. 0. You just DO WHAT THE FUCK YOU WANT TO.
  16. **********************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <getopt.h>
  22. #include <ncurses.h>
  23. #include "ecran.h"
  24. extern int verbosity;
  25. /* ---------------------------------------------------------------- */
  26. int aff7segs_base(WINDOW * win, int lig, int col, int bits, int k)
  27. {
  28. int numbit, mask;
  29. int foo;
  30. for (foo=0; foo<9; foo++) {
  31. mvwhline(win, lig+foo, col, '~', 6);
  32. }
  33. #define KC '+'
  34. #define KS "++++"
  35. wstandout(win);
  36. for (numbit=0; numbit<8; numbit++) {
  37. mask = 1 << numbit;
  38. switch(mask & bits) {
  39. case 0x01:
  40. mvwaddstr(win, lig, col+1, KS);
  41. break;
  42. case 0x02:
  43. mvwaddch(win, lig+1, col+5, KC);
  44. mvwaddch(win, lig+2, col+5, KC);
  45. mvwaddch(win, lig+3, col+5, KC);
  46. break;
  47. case 0x04:
  48. mvwaddch(win, lig+5, col+5, KC);
  49. mvwaddch(win, lig+6, col+5, KC);
  50. mvwaddch(win, lig+7, col+5, KC);
  51. break;
  52. case 0x08:
  53. mvwaddstr(win, lig+8, col+1, KS);
  54. break;
  55. case 0x10:
  56. mvwaddch(win, lig+5, col, KC);
  57. mvwaddch(win, lig+6, col, KC);
  58. mvwaddch(win, lig+7, col, KC);
  59. break;
  60. case 0x20:
  61. mvwaddch(win, lig+1, col , KC);
  62. mvwaddch(win, lig+2, col , KC);
  63. mvwaddch(win, lig+3, col , KC);
  64. break;
  65. case 0x40:
  66. mvwaddstr(win, lig+4, col+1, KS);
  67. break;
  68. case 0x80:
  69. /* decimal point */
  70. mvwaddch(win, lig+7, col+2 , KC);
  71. mvwaddch(win, lig+7, col+3 , KC);
  72. mvwaddch(win, lig+8, col+2 , KC);
  73. mvwaddch(win, lig+8, col+3 , KC);
  74. break;
  75. }
  76. }
  77. wstandend(win);
  78. return 0;
  79. }
  80. /* ---------------------------------------------------------------- */
  81. int aff7segs_digit(WINDOW * win, int lig, int col, char digit)
  82. {
  83. int bits;
  84. #if TRACE > 1
  85. fprintf(stderr, ">>> %s ( %p %d %d '%c' )\n", __func__,
  86. win, lig, col, digit);
  87. #endif
  88. if (isxdigit(digit)) digit = toupper(digit);
  89. switch (digit) {
  90. case '0': bits = 0x3f; break;
  91. case '1': bits = 0x06; break;
  92. case '2': bits = 0x5b; break;
  93. case '3': bits = 0x4f; break;
  94. case '4': bits = 0x66; break;
  95. case '5': bits = 0x6d; break;
  96. case '6': bits = 0x7d; break;
  97. case '7': bits = 0x07; break;
  98. case '8': bits = 0x7f; break;
  99. case '9': bits = 0x6f; break;
  100. /* hexadecimal letters */
  101. case 'A': bits = 0x77; break;
  102. case 'B': bits = 0x7c; break;
  103. case 'C': bits = 0x39; break;
  104. case 'D': bits = 0x5e; break;
  105. case 'E': bits = 0x79; break;
  106. case 'F': bits = 0x71; break;
  107. case ' ': bits = 0; break;
  108. case '.': bits = 0x80; break;
  109. case '-': bits = 0x40; break;
  110. default: bits = 0x49; break;
  111. }
  112. aff7segs_base(win, lig, col, bits, 0);
  113. return 0;
  114. }
  115. /* ---------------------------------------------------------------- */
  116. int aff7segs_short(WINDOW * win, int lig, int col, short value)
  117. {
  118. char buff[10];
  119. int idx;
  120. sprintf(buff, "%6d", value);
  121. // mvwaddstr(win, lig-1, col, buff);
  122. for (idx=0; idx<strlen(buff); idx++) {
  123. aff7segs_digit(win, lig, col+(idx*9), buff[idx]);
  124. }
  125. wrefresh(win);
  126. return 0;
  127. }
  128. /* ---------------------------------------------------------------- */
  129. int aff7segs_float(WINDOW * win, int lig, int col, float value)
  130. {
  131. char buff[10];
  132. int idx;
  133. sprintf(buff, "%6.2f", value);
  134. // mvwaddstr(win, lig-1, col, buff);
  135. for (idx=0; idx<strlen(buff); idx++) {
  136. aff7segs_digit(win, lig, col+(idx*9), buff[idx]);
  137. }
  138. wrefresh(win);
  139. return 0;
  140. }
  141. /* ---------------------------------------------------------------- */