1 /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2 /*
3  * Copyright (c) 2005 Henk Vergonet <Henk.Vergonet@gmail.com>
4  */
5 
6 #ifndef MAP_TO_7SEGMENT_H
7 #define MAP_TO_7SEGMENT_H
8 
9 /* This file provides translation primitives and tables for the conversion
10  * of (ASCII) characters to a 7-segments notation.
11  *
12  * The 7 segment's wikipedia notation below is used as standard.
13  * See: https://en.wikipedia.org/wiki/Seven_segment_display
14  *
15  * Notation:	+-a-+
16  *		f   b
17  *		+-g-+
18  *		e   c
19  *		+-d-+
20  *
21  * Usage:
22  *
23  *   Register a map variable, and fill it with a character set:
24  *	static SEG7_DEFAULT_MAP(map_seg7);
25  *
26  *
27  *   Then use for conversion:
28  *	seg7 = map_to_seg7(&map_seg7, some_char);
29  *	...
30  *
31  * In device drivers it is recommended, if required, to make the char map
32  * accessible via the sysfs interface using the following scheme:
33  *
34  * static ssize_t map_seg7_show(struct device *dev,
35  *				struct device_attribute *attr, char *buf)
36  * {
37  *	memcpy(buf, &map_seg7, sizeof(map_seg7));
38  *	return sizeof(map_seg7);
39  * }
40  * static ssize_t map_seg7_store(struct device *dev,
41  *				 struct device_attribute *attr, const char *buf,
42  *				 size_t cnt)
43  * {
44  *	if(cnt != sizeof(map_seg7))
45  *		return -EINVAL;
46  *	memcpy(&map_seg7, buf, cnt);
47  *	return cnt;
48  * }
49  * static DEVICE_ATTR_RW(map_seg7);
50  *
51  * History:
52  * 2005-05-31	RFC linux-kernel@vger.kernel.org
53  */
54 #include <linux/errno.h>
55 
56 
57 #define BIT_SEG7_A		0
58 #define BIT_SEG7_B		1
59 #define BIT_SEG7_C		2
60 #define BIT_SEG7_D		3
61 #define BIT_SEG7_E		4
62 #define BIT_SEG7_F		5
63 #define BIT_SEG7_G		6
64 #define BIT_SEG7_RESERVED	7
65 
66 struct seg7_conversion_map {
67 	unsigned char	table[128];
68 };
69 
map_to_seg7(struct seg7_conversion_map * map,int c)70 static __inline__ int map_to_seg7(struct seg7_conversion_map *map, int c)
71 {
72 	return c >= 0 && c < sizeof(map->table) ? map->table[c] : -EINVAL;
73 }
74 
75 #define SEG7_CONVERSION_MAP(_name, _map)	\
76 	struct seg7_conversion_map _name = { .table = { _map } }
77 
78 /*
79  * It is recommended to use a facility that allows user space to redefine
80  * custom character sets for LCD devices. Please use a sysfs interface
81  * as described above.
82  */
83 #define MAP_TO_SEG7_SYSFS_FILE	"map_seg7"
84 
85 /*******************************************************************************
86  * ASCII conversion table
87  ******************************************************************************/
88 
89 #define _SEG7(l,a,b,c,d,e,f,g)	\
90       (	a<<BIT_SEG7_A |	b<<BIT_SEG7_B |	c<<BIT_SEG7_C |	d<<BIT_SEG7_D |	\
91 	e<<BIT_SEG7_E |	f<<BIT_SEG7_F |	g<<BIT_SEG7_G )
92 
93 #define _MAP_0_32_ASCII_SEG7_NON_PRINTABLE	\
94 	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,
95 
96 #define _MAP_33_47_ASCII_SEG7_SYMBOL		\
97  _SEG7('!',0,0,0,0,1,1,0), _SEG7('"',0,1,0,0,0,1,0), _SEG7('#',0,1,1,0,1,1,0),\
98  _SEG7('$',1,0,1,1,0,1,1), _SEG7('%',0,0,1,0,0,1,0), _SEG7('&',1,0,1,1,1,1,1),\
99  _SEG7('\'',0,0,0,0,0,1,0),_SEG7('(',1,0,0,1,1,1,0), _SEG7(')',1,1,1,1,0,0,0),\
100  _SEG7('*',0,1,1,0,1,1,1), _SEG7('+',0,1,1,0,0,0,1), _SEG7(',',0,0,0,0,1,0,0),\
101  _SEG7('-',0,0,0,0,0,0,1), _SEG7('.',0,0,0,0,1,0,0), _SEG7('/',0,1,0,0,1,0,1),
102 
103 #define _MAP_48_57_ASCII_SEG7_NUMERIC		\
104  _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),\
105  _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),\
106  _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),\
107  _SEG7('9',1,1,1,1,0,1,1),
108 
109 #define _MAP_58_64_ASCII_SEG7_SYMBOL		\
110  _SEG7(':',0,0,0,1,0,0,1), _SEG7(';',0,0,0,1,0,0,1), _SEG7('<',1,0,0,0,0,1,1),\
111  _SEG7('=',0,0,0,1,0,0,1), _SEG7('>',1,1,0,0,0,0,1), _SEG7('?',1,1,1,0,0,1,0),\
112  _SEG7('@',1,1,0,1,1,1,1),
113 
114 #define _MAP_65_90_ASCII_SEG7_ALPHA_UPPR	\
115  _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),\
116  _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),\
117  _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),\
118  _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),\
119  _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),\
120  _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),\
121  _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),\
122  _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),\
123  _SEG7('Y',0,1,1,0,0,1,1), _SEG7('Z',1,1,0,1,1,0,1),
124 
125 #define _MAP_91_96_ASCII_SEG7_SYMBOL		\
126  _SEG7('[',1,0,0,1,1,1,0), _SEG7('\\',0,0,1,0,0,1,1),_SEG7(']',1,1,1,1,0,0,0),\
127  _SEG7('^',1,1,0,0,0,1,0), _SEG7('_',0,0,0,1,0,0,0), _SEG7('`',0,1,0,0,0,0,0),
128 
129 #define _MAP_97_122_ASCII_SEG7_ALPHA_LOWER	\
130  _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),\
131  _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),\
132  _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),\
133  _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),\
134  _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),\
135  _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),\
136  _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),\
137  _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),\
138  _SEG7('y',0,1,1,1,0,1,1), _SEG7('Z',1,1,0,1,1,0,1),
139 
140 #define _MAP_123_126_ASCII_SEG7_SYMBOL		\
141  _SEG7('{',1,0,0,1,1,1,0), _SEG7('|',0,0,0,0,1,1,0), _SEG7('}',1,1,1,1,0,0,0),\
142  _SEG7('~',1,0,0,0,0,0,0),
143 
144 /* Maps */
145 
146 /* This set tries to map as close as possible to the visible characteristics
147  * of the ASCII symbol, lowercase and uppercase letters may differ in
148  * presentation on the display.
149  */
150 #define MAP_ASCII7SEG_ALPHANUM			\
151 	_MAP_0_32_ASCII_SEG7_NON_PRINTABLE	\
152 	_MAP_33_47_ASCII_SEG7_SYMBOL		\
153 	_MAP_48_57_ASCII_SEG7_NUMERIC		\
154 	_MAP_58_64_ASCII_SEG7_SYMBOL		\
155 	_MAP_65_90_ASCII_SEG7_ALPHA_UPPR	\
156 	_MAP_91_96_ASCII_SEG7_SYMBOL		\
157 	_MAP_97_122_ASCII_SEG7_ALPHA_LOWER	\
158 	_MAP_123_126_ASCII_SEG7_SYMBOL
159 
160 /* This set tries to map as close as possible to the symbolic characteristics
161  * of the ASCII character for maximum discrimination.
162  * For now this means all alpha chars are in lower case representations.
163  * (This for example facilitates the use of hex numbers with uppercase input.)
164  */
165 #define MAP_ASCII7SEG_ALPHANUM_LC			\
166 	_MAP_0_32_ASCII_SEG7_NON_PRINTABLE	\
167 	_MAP_33_47_ASCII_SEG7_SYMBOL		\
168 	_MAP_48_57_ASCII_SEG7_NUMERIC		\
169 	_MAP_58_64_ASCII_SEG7_SYMBOL		\
170 	_MAP_97_122_ASCII_SEG7_ALPHA_LOWER	\
171 	_MAP_91_96_ASCII_SEG7_SYMBOL		\
172 	_MAP_97_122_ASCII_SEG7_ALPHA_LOWER	\
173 	_MAP_123_126_ASCII_SEG7_SYMBOL
174 
175 #define SEG7_DEFAULT_MAP(_name)		\
176 	SEG7_CONVERSION_MAP(_name,MAP_ASCII7SEG_ALPHANUM)
177 
178 #endif	/* MAP_TO_7SEGMENT_H */
179 
180