map_to_14segment.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
  2. /*
  3. * Copyright (C) 2021 Glider bv
  4. *
  5. * Based on include/uapi/linux/map_to_7segment.h:
  6. * Copyright (c) 2005 Henk Vergonet <Henk.Vergonet@gmail.com>
  7. */
  8. #ifndef MAP_TO_14SEGMENT_H
  9. #define MAP_TO_14SEGMENT_H
  10. /* This file provides translation primitives and tables for the conversion
  11. * of (ASCII) characters to a 14-segments notation.
  12. *
  13. * The 14 segment's wikipedia notation below is used as standard.
  14. * See: https://en.wikipedia.org/wiki/Fourteen-segment_display
  15. *
  16. * Notation: +---a---+
  17. * |\ | /|
  18. * f h i j b
  19. * | \|/ |
  20. * +-g1+-g2+
  21. * | /|\ |
  22. * e k l m c
  23. * |/ | \|
  24. * +---d---+
  25. *
  26. * Usage:
  27. *
  28. * Register a map variable, and fill it with a character set:
  29. * static SEG14_DEFAULT_MAP(map_seg14);
  30. *
  31. *
  32. * Then use for conversion:
  33. * seg14 = map_to_seg14(&map_seg14, some_char);
  34. * ...
  35. *
  36. * In device drivers it is recommended, if required, to make the char map
  37. * accessible via the sysfs interface using the following scheme:
  38. *
  39. * static ssize_t map_seg14_show(struct device *dev,
  40. * struct device_attribute *attr, char *buf)
  41. * {
  42. * memcpy(buf, &map_seg14, sizeof(map_seg14));
  43. * return sizeof(map_seg14);
  44. * }
  45. * static ssize_t map_seg14_store(struct device *dev,
  46. * struct device_attribute *attr,
  47. * const char *buf, size_t cnt)
  48. * {
  49. * if (cnt != sizeof(map_seg14))
  50. * return -EINVAL;
  51. * memcpy(&map_seg14, buf, cnt);
  52. * return cnt;
  53. * }
  54. * static DEVICE_ATTR_RW(map_seg14);
  55. */
  56. #include <linux/errno.h>
  57. #include <linux/types.h>
  58. #include <asm/byteorder.h>
  59. #define BIT_SEG14_A 0
  60. #define BIT_SEG14_B 1
  61. #define BIT_SEG14_C 2
  62. #define BIT_SEG14_D 3
  63. #define BIT_SEG14_E 4
  64. #define BIT_SEG14_F 5
  65. #define BIT_SEG14_G1 6
  66. #define BIT_SEG14_G2 7
  67. #define BIT_SEG14_H 8
  68. #define BIT_SEG14_I 9
  69. #define BIT_SEG14_J 10
  70. #define BIT_SEG14_K 11
  71. #define BIT_SEG14_L 12
  72. #define BIT_SEG14_M 13
  73. #define BIT_SEG14_RESERVED1 14
  74. #define BIT_SEG14_RESERVED2 15
  75. struct seg14_conversion_map {
  76. __be16 table[128];
  77. };
  78. static __inline__ int map_to_seg14(struct seg14_conversion_map *map, int c)
  79. {
  80. if (c < 0 || c >= sizeof(map->table) / sizeof(map->table[0]))
  81. return -EINVAL;
  82. return __be16_to_cpu(map->table[c]);
  83. }
  84. #define SEG14_CONVERSION_MAP(_name, _map) \
  85. struct seg14_conversion_map _name = { .table = { _map } }
  86. /*
  87. * It is recommended to use a facility that allows user space to redefine
  88. * custom character sets for LCD devices. Please use a sysfs interface
  89. * as described above.
  90. */
  91. #define MAP_TO_SEG14_SYSFS_FILE "map_seg14"
  92. /*******************************************************************************
  93. * ASCII conversion table
  94. ******************************************************************************/
  95. #define _SEG14(sym, a, b, c, d, e, f, g1, g2, h, j, k, l, m, n) \
  96. __cpu_to_be16( a << BIT_SEG14_A | b << BIT_SEG14_B | \
  97. c << BIT_SEG14_C | d << BIT_SEG14_D | \
  98. e << BIT_SEG14_E | f << BIT_SEG14_F | \
  99. g1 << BIT_SEG14_G1 | g2 << BIT_SEG14_G2 | \
  100. h << BIT_SEG14_H | j << BIT_SEG14_I | \
  101. k << BIT_SEG14_J | l << BIT_SEG14_K | \
  102. m << BIT_SEG14_L | n << BIT_SEG14_M )
  103. #define _MAP_0_32_ASCII_SEG14_NON_PRINTABLE \
  104. 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,
  105. #define _MAP_33_47_ASCII_SEG14_SYMBOL \
  106. _SEG14('!', 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), \
  107. _SEG14('"', 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), \
  108. _SEG14('#', 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0), \
  109. _SEG14('$', 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0), \
  110. _SEG14('%', 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0), \
  111. _SEG14('&', 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1), \
  112. _SEG14('\'',0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), \
  113. _SEG14('(', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1), \
  114. _SEG14(')', 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0), \
  115. _SEG14('*', 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1), \
  116. _SEG14('+', 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0), \
  117. _SEG14(',', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0), \
  118. _SEG14('-', 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0), \
  119. _SEG14('.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), \
  120. _SEG14('/', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0),
  121. #define _MAP_48_57_ASCII_SEG14_NUMERIC \
  122. _SEG14('0', 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0), \
  123. _SEG14('1', 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0), \
  124. _SEG14('2', 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0), \
  125. _SEG14('3', 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), \
  126. _SEG14('4', 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0), \
  127. _SEG14('5', 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1), \
  128. _SEG14('6', 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0), \
  129. _SEG14('7', 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0), \
  130. _SEG14('8', 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0), \
  131. _SEG14('9', 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0),
  132. #define _MAP_58_64_ASCII_SEG14_SYMBOL \
  133. _SEG14(':', 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0), \
  134. _SEG14(';', 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0), \
  135. _SEG14('<', 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1), \
  136. _SEG14('=', 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0), \
  137. _SEG14('>', 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0), \
  138. _SEG14('?', 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0), \
  139. _SEG14('@', 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0),
  140. #define _MAP_65_90_ASCII_SEG14_ALPHA_UPPER \
  141. _SEG14('A', 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0), \
  142. _SEG14('B', 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0), \
  143. _SEG14('C', 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0), \
  144. _SEG14('D', 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0), \
  145. _SEG14('E', 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0), \
  146. _SEG14('F', 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0), \
  147. _SEG14('G', 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0), \
  148. _SEG14('H', 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0), \
  149. _SEG14('I', 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0), \
  150. _SEG14('J', 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), \
  151. _SEG14('K', 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1), \
  152. _SEG14('L', 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0), \
  153. _SEG14('M', 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0), \
  154. _SEG14('N', 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1), \
  155. _SEG14('O', 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0), \
  156. _SEG14('P', 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0), \
  157. _SEG14('Q', 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1), \
  158. _SEG14('R', 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1), \
  159. _SEG14('S', 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0), \
  160. _SEG14('T', 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0), \
  161. _SEG14('U', 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0), \
  162. _SEG14('V', 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0), \
  163. _SEG14('W', 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1), \
  164. _SEG14('X', 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1), \
  165. _SEG14('Y', 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0), \
  166. _SEG14('Z', 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0),
  167. #define _MAP_91_96_ASCII_SEG14_SYMBOL \
  168. _SEG14('[', 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0), \
  169. _SEG14('\\',0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), \
  170. _SEG14(']', 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), \
  171. _SEG14('^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1), \
  172. _SEG14('_', 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), \
  173. _SEG14('`', 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0),
  174. #define _MAP_97_122_ASCII_SEG14_ALPHA_LOWER \
  175. _SEG14('a', 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0), \
  176. _SEG14('b', 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1), \
  177. _SEG14('c', 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0), \
  178. _SEG14('d', 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0), \
  179. _SEG14('e', 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0), \
  180. _SEG14('f', 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0), \
  181. _SEG14('g', 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0), \
  182. _SEG14('h', 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0), \
  183. _SEG14('i', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), \
  184. _SEG14('j', 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0), \
  185. _SEG14('k', 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1), \
  186. _SEG14('l', 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0), \
  187. _SEG14('m', 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0), \
  188. _SEG14('n', 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0), \
  189. _SEG14('o', 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0), \
  190. _SEG14('p', 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0), \
  191. _SEG14('q', 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0), \
  192. _SEG14('r', 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0), \
  193. _SEG14('s', 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1), \
  194. _SEG14('t', 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0), \
  195. _SEG14('u', 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), \
  196. _SEG14('v', 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0), \
  197. _SEG14('w', 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1), \
  198. _SEG14('x', 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), \
  199. _SEG14('y', 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0), \
  200. _SEG14('z', 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0),
  201. #define _MAP_123_126_ASCII_SEG14_SYMBOL \
  202. _SEG14('{', 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0), \
  203. _SEG14('|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0), \
  204. _SEG14('}', 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1), \
  205. _SEG14('~', 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0),
  206. /* Maps */
  207. #define MAP_ASCII14SEG_ALPHANUM \
  208. _MAP_0_32_ASCII_SEG14_NON_PRINTABLE \
  209. _MAP_33_47_ASCII_SEG14_SYMBOL \
  210. _MAP_48_57_ASCII_SEG14_NUMERIC \
  211. _MAP_58_64_ASCII_SEG14_SYMBOL \
  212. _MAP_65_90_ASCII_SEG14_ALPHA_UPPER \
  213. _MAP_91_96_ASCII_SEG14_SYMBOL \
  214. _MAP_97_122_ASCII_SEG14_ALPHA_LOWER \
  215. _MAP_123_126_ASCII_SEG14_SYMBOL
  216. #define SEG14_DEFAULT_MAP(_name) \
  217. SEG14_CONVERSION_MAP(_name, MAP_ASCII14SEG_ALPHANUM)
  218. #endif /* MAP_TO_14SEGMENT_H */