map_to_7segment.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
  2. /*
  3. * Copyright (c) 2005 Henk Vergonet <Henk.Vergonet@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef MAP_TO_7SEGMENT_H
  20. #define MAP_TO_7SEGMENT_H
  21. /* This file provides translation primitives and tables for the conversion
  22. * of (ASCII) characters to a 7-segments notation.
  23. *
  24. * The 7 segment's wikipedia notation below is used as standard.
  25. * See: http://en.wikipedia.org/wiki/Seven_segment_display
  26. *
  27. * Notation: +-a-+
  28. * f b
  29. * +-g-+
  30. * e c
  31. * +-d-+
  32. *
  33. * Usage:
  34. *
  35. * Register a map variable, and fill it with a character set:
  36. * static SEG7_DEFAULT_MAP(map_seg7);
  37. *
  38. *
  39. * Then use for conversion:
  40. * seg7 = map_to_seg7(&map_seg7, some_char);
  41. * ...
  42. *
  43. * In device drivers it is recommended, if required, to make the char map
  44. * accessible via the sysfs interface using the following scheme:
  45. *
  46. * static ssize_t show_map(struct device *dev, char *buf) {
  47. * memcpy(buf, &map_seg7, sizeof(map_seg7));
  48. * return sizeof(map_seg7);
  49. * }
  50. * static ssize_t store_map(struct device *dev, const char *buf, size_t cnt) {
  51. * if(cnt != sizeof(map_seg7))
  52. * return -EINVAL;
  53. * memcpy(&map_seg7, buf, cnt);
  54. * return cnt;
  55. * }
  56. * static DEVICE_ATTR(map_seg7, PERMS_RW, show_map, store_map);
  57. *
  58. * History:
  59. * 2005-05-31 RFC linux-kernel@vger.kernel.org
  60. */
  61. #include <linux/errno.h>
  62. #define BIT_SEG7_A 0
  63. #define BIT_SEG7_B 1
  64. #define BIT_SEG7_C 2
  65. #define BIT_SEG7_D 3
  66. #define BIT_SEG7_E 4
  67. #define BIT_SEG7_F 5
  68. #define BIT_SEG7_G 6
  69. #define BIT_SEG7_RESERVED 7
  70. struct seg7_conversion_map {
  71. unsigned char table[128];
  72. };
  73. static __inline__ int map_to_seg7(struct seg7_conversion_map *map, int c)
  74. {
  75. return c >= 0 && c < sizeof(map->table) ? map->table[c] : -EINVAL;
  76. }
  77. #define SEG7_CONVERSION_MAP(_name, _map) \
  78. struct seg7_conversion_map _name = { .table = { _map } }
  79. /*
  80. * It is recommended to use a facility that allows user space to redefine
  81. * custom character sets for LCD devices. Please use a sysfs interface
  82. * as described above.
  83. */
  84. #define MAP_TO_SEG7_SYSFS_FILE "map_seg7"
  85. /*******************************************************************************
  86. * ASCII conversion table
  87. ******************************************************************************/
  88. #define _SEG7(l,a,b,c,d,e,f,g) \
  89. ( a<<BIT_SEG7_A | b<<BIT_SEG7_B | c<<BIT_SEG7_C | d<<BIT_SEG7_D | \
  90. e<<BIT_SEG7_E | f<<BIT_SEG7_F | g<<BIT_SEG7_G )
  91. #define _MAP_0_32_ASCII_SEG7_NON_PRINTABLE \
  92. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  93. #define _MAP_33_47_ASCII_SEG7_SYMBOL \
  94. _SEG7('!',0,0,0,0,1,1,0), _SEG7('"',0,1,0,0,0,1,0), _SEG7('#',0,1,1,0,1,1,0),\
  95. _SEG7('$',1,0,1,1,0,1,1), _SEG7('%',0,0,1,0,0,1,0), _SEG7('&',1,0,1,1,1,1,1),\
  96. _SEG7('\'',0,0,0,0,0,1,0),_SEG7('(',1,0,0,1,1,1,0), _SEG7(')',1,1,1,1,0,0,0),\
  97. _SEG7('*',0,1,1,0,1,1,1), _SEG7('+',0,1,1,0,0,0,1), _SEG7(',',0,0,0,0,1,0,0),\
  98. _SEG7('-',0,0,0,0,0,0,1), _SEG7('.',0,0,0,0,1,0,0), _SEG7('/',0,1,0,0,1,0,1),
  99. #define _MAP_48_57_ASCII_SEG7_NUMERIC \
  100. _SEG7('0',1,1,1,1,1,1,0), _SEG7('1',0,1,1,0,0,0,0), _SEG7('2',1,1,0,1,1,0,1),\
  101. _SEG7('3',1,1,1,1,0,0,1), _SEG7('4',0,1,1,0,0,1,1), _SEG7('5',1,0,1,1,0,1,1),\
  102. _SEG7('6',1,0,1,1,1,1,1), _SEG7('7',1,1,1,0,0,0,0), _SEG7('8',1,1,1,1,1,1,1),\
  103. _SEG7('9',1,1,1,1,0,1,1),
  104. #define _MAP_58_64_ASCII_SEG7_SYMBOL \
  105. _SEG7(':',0,0,0,1,0,0,1), _SEG7(';',0,0,0,1,0,0,1), _SEG7('<',1,0,0,0,0,1,1),\
  106. _SEG7('=',0,0,0,1,0,0,1), _SEG7('>',1,1,0,0,0,0,1), _SEG7('?',1,1,1,0,0,1,0),\
  107. _SEG7('@',1,1,0,1,1,1,1),
  108. #define _MAP_65_90_ASCII_SEG7_ALPHA_UPPR \
  109. _SEG7('A',1,1,1,0,1,1,1), _SEG7('B',1,1,1,1,1,1,1), _SEG7('C',1,0,0,1,1,1,0),\
  110. _SEG7('D',1,1,1,1,1,1,0), _SEG7('E',1,0,0,1,1,1,1), _SEG7('F',1,0,0,0,1,1,1),\
  111. _SEG7('G',1,1,1,1,0,1,1), _SEG7('H',0,1,1,0,1,1,1), _SEG7('I',0,1,1,0,0,0,0),\
  112. _SEG7('J',0,1,1,1,0,0,0), _SEG7('K',0,1,1,0,1,1,1), _SEG7('L',0,0,0,1,1,1,0),\
  113. _SEG7('M',1,1,1,0,1,1,0), _SEG7('N',1,1,1,0,1,1,0), _SEG7('O',1,1,1,1,1,1,0),\
  114. _SEG7('P',1,1,0,0,1,1,1), _SEG7('Q',1,1,1,1,1,1,0), _SEG7('R',1,1,1,0,1,1,1),\
  115. _SEG7('S',1,0,1,1,0,1,1), _SEG7('T',0,0,0,1,1,1,1), _SEG7('U',0,1,1,1,1,1,0),\
  116. _SEG7('V',0,1,1,1,1,1,0), _SEG7('W',0,1,1,1,1,1,1), _SEG7('X',0,1,1,0,1,1,1),\
  117. _SEG7('Y',0,1,1,0,0,1,1), _SEG7('Z',1,1,0,1,1,0,1),
  118. #define _MAP_91_96_ASCII_SEG7_SYMBOL \
  119. _SEG7('[',1,0,0,1,1,1,0), _SEG7('\\',0,0,1,0,0,1,1),_SEG7(']',1,1,1,1,0,0,0),\
  120. _SEG7('^',1,1,0,0,0,1,0), _SEG7('_',0,0,0,1,0,0,0), _SEG7('`',0,1,0,0,0,0,0),
  121. #define _MAP_97_122_ASCII_SEG7_ALPHA_LOWER \
  122. _SEG7('A',1,1,1,0,1,1,1), _SEG7('b',0,0,1,1,1,1,1), _SEG7('c',0,0,0,1,1,0,1),\
  123. _SEG7('d',0,1,1,1,1,0,1), _SEG7('E',1,0,0,1,1,1,1), _SEG7('F',1,0,0,0,1,1,1),\
  124. _SEG7('G',1,1,1,1,0,1,1), _SEG7('h',0,0,1,0,1,1,1), _SEG7('i',0,0,1,0,0,0,0),\
  125. _SEG7('j',0,0,1,1,0,0,0), _SEG7('k',0,0,1,0,1,1,1), _SEG7('L',0,0,0,1,1,1,0),\
  126. _SEG7('M',1,1,1,0,1,1,0), _SEG7('n',0,0,1,0,1,0,1), _SEG7('o',0,0,1,1,1,0,1),\
  127. _SEG7('P',1,1,0,0,1,1,1), _SEG7('q',1,1,1,0,0,1,1), _SEG7('r',0,0,0,0,1,0,1),\
  128. _SEG7('S',1,0,1,1,0,1,1), _SEG7('T',0,0,0,1,1,1,1), _SEG7('u',0,0,1,1,1,0,0),\
  129. _SEG7('v',0,0,1,1,1,0,0), _SEG7('W',0,1,1,1,1,1,1), _SEG7('X',0,1,1,0,1,1,1),\
  130. _SEG7('y',0,1,1,1,0,1,1), _SEG7('Z',1,1,0,1,1,0,1),
  131. #define _MAP_123_126_ASCII_SEG7_SYMBOL \
  132. _SEG7('{',1,0,0,1,1,1,0), _SEG7('|',0,0,0,0,1,1,0), _SEG7('}',1,1,1,1,0,0,0),\
  133. _SEG7('~',1,0,0,0,0,0,0),
  134. /* Maps */
  135. /* This set tries to map as close as possible to the visible characteristics
  136. * of the ASCII symbol, lowercase and uppercase letters may differ in
  137. * presentation on the display.
  138. */
  139. #define MAP_ASCII7SEG_ALPHANUM \
  140. _MAP_0_32_ASCII_SEG7_NON_PRINTABLE \
  141. _MAP_33_47_ASCII_SEG7_SYMBOL \
  142. _MAP_48_57_ASCII_SEG7_NUMERIC \
  143. _MAP_58_64_ASCII_SEG7_SYMBOL \
  144. _MAP_65_90_ASCII_SEG7_ALPHA_UPPR \
  145. _MAP_91_96_ASCII_SEG7_SYMBOL \
  146. _MAP_97_122_ASCII_SEG7_ALPHA_LOWER \
  147. _MAP_123_126_ASCII_SEG7_SYMBOL
  148. /* This set tries to map as close as possible to the symbolic characteristics
  149. * of the ASCII character for maximum discrimination.
  150. * For now this means all alpha chars are in lower case representations.
  151. * (This for example facilitates the use of hex numbers with uppercase input.)
  152. */
  153. #define MAP_ASCII7SEG_ALPHANUM_LC \
  154. _MAP_0_32_ASCII_SEG7_NON_PRINTABLE \
  155. _MAP_33_47_ASCII_SEG7_SYMBOL \
  156. _MAP_48_57_ASCII_SEG7_NUMERIC \
  157. _MAP_58_64_ASCII_SEG7_SYMBOL \
  158. _MAP_97_122_ASCII_SEG7_ALPHA_LOWER \
  159. _MAP_91_96_ASCII_SEG7_SYMBOL \
  160. _MAP_97_122_ASCII_SEG7_ALPHA_LOWER \
  161. _MAP_123_126_ASCII_SEG7_SYMBOL
  162. #define SEG7_DEFAULT_MAP(_name) \
  163. SEG7_CONVERSION_MAP(_name,MAP_ASCII7SEG_ALPHANUM)
  164. #endif /* MAP_TO_7SEGMENT_H */