xref: /linux/include/uapi/linux/map_to_7segment.h (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2607ca46eSDavid Howells /*
3607ca46eSDavid Howells  * Copyright (c) 2005 Henk Vergonet <Henk.Vergonet@gmail.com>
4607ca46eSDavid Howells  */
5607ca46eSDavid Howells 
6607ca46eSDavid Howells #ifndef MAP_TO_7SEGMENT_H
7607ca46eSDavid Howells #define MAP_TO_7SEGMENT_H
8607ca46eSDavid Howells 
9607ca46eSDavid Howells /* This file provides translation primitives and tables for the conversion
10607ca46eSDavid Howells  * of (ASCII) characters to a 7-segments notation.
11607ca46eSDavid Howells  *
12607ca46eSDavid Howells  * The 7 segment's wikipedia notation below is used as standard.
137f317d34SAlexander A. Klimov  * See: https://en.wikipedia.org/wiki/Seven_segment_display
14607ca46eSDavid Howells  *
15607ca46eSDavid Howells  * Notation:	+-a-+
16607ca46eSDavid Howells  *		f   b
17607ca46eSDavid Howells  *		+-g-+
18607ca46eSDavid Howells  *		e   c
19607ca46eSDavid Howells  *		+-d-+
20607ca46eSDavid Howells  *
21607ca46eSDavid Howells  * Usage:
22607ca46eSDavid Howells  *
23607ca46eSDavid Howells  *   Register a map variable, and fill it with a character set:
24607ca46eSDavid Howells  *	static SEG7_DEFAULT_MAP(map_seg7);
25607ca46eSDavid Howells  *
26607ca46eSDavid Howells  *
27607ca46eSDavid Howells  *   Then use for conversion:
28607ca46eSDavid Howells  *	seg7 = map_to_seg7(&map_seg7, some_char);
29607ca46eSDavid Howells  *	...
30607ca46eSDavid Howells  *
31607ca46eSDavid Howells  * In device drivers it is recommended, if required, to make the char map
32607ca46eSDavid Howells  * accessible via the sysfs interface using the following scheme:
33607ca46eSDavid Howells  *
34*0566752cSGeert Uytterhoeven  * static ssize_t map_seg7_show(struct device *dev,
35*0566752cSGeert Uytterhoeven  *				struct device_attribute *attr, char *buf)
36*0566752cSGeert Uytterhoeven  * {
37607ca46eSDavid Howells  *	memcpy(buf, &map_seg7, sizeof(map_seg7));
38607ca46eSDavid Howells  *	return sizeof(map_seg7);
39607ca46eSDavid Howells  * }
40*0566752cSGeert Uytterhoeven  * static ssize_t map_seg7_store(struct device *dev,
41*0566752cSGeert Uytterhoeven  *				 struct device_attribute *attr, const char *buf,
42*0566752cSGeert Uytterhoeven  *				 size_t cnt)
43*0566752cSGeert Uytterhoeven  * {
44607ca46eSDavid Howells  *	if(cnt != sizeof(map_seg7))
45607ca46eSDavid Howells  *		return -EINVAL;
46607ca46eSDavid Howells  *	memcpy(&map_seg7, buf, cnt);
47607ca46eSDavid Howells  *	return cnt;
48607ca46eSDavid Howells  * }
49*0566752cSGeert Uytterhoeven  * static DEVICE_ATTR_RW(map_seg7);
50607ca46eSDavid Howells  *
51607ca46eSDavid Howells  * History:
52607ca46eSDavid Howells  * 2005-05-31	RFC linux-kernel@vger.kernel.org
53607ca46eSDavid Howells  */
54607ca46eSDavid Howells #include <linux/errno.h>
55607ca46eSDavid Howells 
56607ca46eSDavid Howells 
57607ca46eSDavid Howells #define BIT_SEG7_A		0
58607ca46eSDavid Howells #define BIT_SEG7_B		1
59607ca46eSDavid Howells #define BIT_SEG7_C		2
60607ca46eSDavid Howells #define BIT_SEG7_D		3
61607ca46eSDavid Howells #define BIT_SEG7_E		4
62607ca46eSDavid Howells #define BIT_SEG7_F		5
63607ca46eSDavid Howells #define BIT_SEG7_G		6
64607ca46eSDavid Howells #define BIT_SEG7_RESERVED	7
65607ca46eSDavid Howells 
66607ca46eSDavid Howells struct seg7_conversion_map {
67607ca46eSDavid Howells 	unsigned char	table[128];
68607ca46eSDavid Howells };
69607ca46eSDavid Howells 
map_to_seg7(struct seg7_conversion_map * map,int c)70607ca46eSDavid Howells static __inline__ int map_to_seg7(struct seg7_conversion_map *map, int c)
71607ca46eSDavid Howells {
72607ca46eSDavid Howells 	return c >= 0 && c < sizeof(map->table) ? map->table[c] : -EINVAL;
73607ca46eSDavid Howells }
74607ca46eSDavid Howells 
75607ca46eSDavid Howells #define SEG7_CONVERSION_MAP(_name, _map)	\
76607ca46eSDavid Howells 	struct seg7_conversion_map _name = { .table = { _map } }
77607ca46eSDavid Howells 
78607ca46eSDavid Howells /*
79607ca46eSDavid Howells  * It is recommended to use a facility that allows user space to redefine
80607ca46eSDavid Howells  * custom character sets for LCD devices. Please use a sysfs interface
81607ca46eSDavid Howells  * as described above.
82607ca46eSDavid Howells  */
83607ca46eSDavid Howells #define MAP_TO_SEG7_SYSFS_FILE	"map_seg7"
84607ca46eSDavid Howells 
85607ca46eSDavid Howells /*******************************************************************************
86607ca46eSDavid Howells  * ASCII conversion table
87607ca46eSDavid Howells  ******************************************************************************/
88607ca46eSDavid Howells 
89607ca46eSDavid Howells #define _SEG7(l,a,b,c,d,e,f,g)	\
90607ca46eSDavid Howells       (	a<<BIT_SEG7_A |	b<<BIT_SEG7_B |	c<<BIT_SEG7_C |	d<<BIT_SEG7_D |	\
91607ca46eSDavid Howells 	e<<BIT_SEG7_E |	f<<BIT_SEG7_F |	g<<BIT_SEG7_G )
92607ca46eSDavid Howells 
93607ca46eSDavid Howells #define _MAP_0_32_ASCII_SEG7_NON_PRINTABLE	\
94607ca46eSDavid Howells 	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,
95607ca46eSDavid Howells 
96607ca46eSDavid Howells #define _MAP_33_47_ASCII_SEG7_SYMBOL		\
97607ca46eSDavid Howells  _SEG7('!',0,0,0,0,1,1,0), _SEG7('"',0,1,0,0,0,1,0), _SEG7('#',0,1,1,0,1,1,0),\
98607ca46eSDavid Howells  _SEG7('$',1,0,1,1,0,1,1), _SEG7('%',0,0,1,0,0,1,0), _SEG7('&',1,0,1,1,1,1,1),\
99607ca46eSDavid Howells  _SEG7('\'',0,0,0,0,0,1,0),_SEG7('(',1,0,0,1,1,1,0), _SEG7(')',1,1,1,1,0,0,0),\
100607ca46eSDavid Howells  _SEG7('*',0,1,1,0,1,1,1), _SEG7('+',0,1,1,0,0,0,1), _SEG7(',',0,0,0,0,1,0,0),\
101607ca46eSDavid Howells  _SEG7('-',0,0,0,0,0,0,1), _SEG7('.',0,0,0,0,1,0,0), _SEG7('/',0,1,0,0,1,0,1),
102607ca46eSDavid Howells 
103607ca46eSDavid Howells #define _MAP_48_57_ASCII_SEG7_NUMERIC		\
104607ca46eSDavid Howells  _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),\
105607ca46eSDavid Howells  _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),\
106607ca46eSDavid Howells  _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),\
107607ca46eSDavid Howells  _SEG7('9',1,1,1,1,0,1,1),
108607ca46eSDavid Howells 
109607ca46eSDavid Howells #define _MAP_58_64_ASCII_SEG7_SYMBOL		\
110607ca46eSDavid Howells  _SEG7(':',0,0,0,1,0,0,1), _SEG7(';',0,0,0,1,0,0,1), _SEG7('<',1,0,0,0,0,1,1),\
111607ca46eSDavid Howells  _SEG7('=',0,0,0,1,0,0,1), _SEG7('>',1,1,0,0,0,0,1), _SEG7('?',1,1,1,0,0,1,0),\
112607ca46eSDavid Howells  _SEG7('@',1,1,0,1,1,1,1),
113607ca46eSDavid Howells 
114607ca46eSDavid Howells #define _MAP_65_90_ASCII_SEG7_ALPHA_UPPR	\
115607ca46eSDavid Howells  _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),\
116607ca46eSDavid Howells  _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),\
117607ca46eSDavid Howells  _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),\
118607ca46eSDavid Howells  _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),\
119607ca46eSDavid Howells  _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),\
120607ca46eSDavid Howells  _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),\
121607ca46eSDavid Howells  _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),\
122607ca46eSDavid Howells  _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),\
123607ca46eSDavid Howells  _SEG7('Y',0,1,1,0,0,1,1), _SEG7('Z',1,1,0,1,1,0,1),
124607ca46eSDavid Howells 
125607ca46eSDavid Howells #define _MAP_91_96_ASCII_SEG7_SYMBOL		\
126607ca46eSDavid Howells  _SEG7('[',1,0,0,1,1,1,0), _SEG7('\\',0,0,1,0,0,1,1),_SEG7(']',1,1,1,1,0,0,0),\
127607ca46eSDavid Howells  _SEG7('^',1,1,0,0,0,1,0), _SEG7('_',0,0,0,1,0,0,0), _SEG7('`',0,1,0,0,0,0,0),
128607ca46eSDavid Howells 
129607ca46eSDavid Howells #define _MAP_97_122_ASCII_SEG7_ALPHA_LOWER	\
130607ca46eSDavid Howells  _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),\
131607ca46eSDavid Howells  _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),\
132607ca46eSDavid Howells  _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),\
133607ca46eSDavid Howells  _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),\
134607ca46eSDavid Howells  _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),\
135607ca46eSDavid Howells  _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),\
136607ca46eSDavid Howells  _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),\
137607ca46eSDavid Howells  _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),\
138607ca46eSDavid Howells  _SEG7('y',0,1,1,1,0,1,1), _SEG7('Z',1,1,0,1,1,0,1),
139607ca46eSDavid Howells 
140607ca46eSDavid Howells #define _MAP_123_126_ASCII_SEG7_SYMBOL		\
141607ca46eSDavid Howells  _SEG7('{',1,0,0,1,1,1,0), _SEG7('|',0,0,0,0,1,1,0), _SEG7('}',1,1,1,1,0,0,0),\
142607ca46eSDavid Howells  _SEG7('~',1,0,0,0,0,0,0),
143607ca46eSDavid Howells 
144607ca46eSDavid Howells /* Maps */
145607ca46eSDavid Howells 
146607ca46eSDavid Howells /* This set tries to map as close as possible to the visible characteristics
147607ca46eSDavid Howells  * of the ASCII symbol, lowercase and uppercase letters may differ in
148607ca46eSDavid Howells  * presentation on the display.
149607ca46eSDavid Howells  */
150607ca46eSDavid Howells #define MAP_ASCII7SEG_ALPHANUM			\
151607ca46eSDavid Howells 	_MAP_0_32_ASCII_SEG7_NON_PRINTABLE	\
152607ca46eSDavid Howells 	_MAP_33_47_ASCII_SEG7_SYMBOL		\
153607ca46eSDavid Howells 	_MAP_48_57_ASCII_SEG7_NUMERIC		\
154607ca46eSDavid Howells 	_MAP_58_64_ASCII_SEG7_SYMBOL		\
155607ca46eSDavid Howells 	_MAP_65_90_ASCII_SEG7_ALPHA_UPPR	\
156607ca46eSDavid Howells 	_MAP_91_96_ASCII_SEG7_SYMBOL		\
157607ca46eSDavid Howells 	_MAP_97_122_ASCII_SEG7_ALPHA_LOWER	\
158607ca46eSDavid Howells 	_MAP_123_126_ASCII_SEG7_SYMBOL
159607ca46eSDavid Howells 
160607ca46eSDavid Howells /* This set tries to map as close as possible to the symbolic characteristics
161607ca46eSDavid Howells  * of the ASCII character for maximum discrimination.
162607ca46eSDavid Howells  * For now this means all alpha chars are in lower case representations.
163607ca46eSDavid Howells  * (This for example facilitates the use of hex numbers with uppercase input.)
164607ca46eSDavid Howells  */
165607ca46eSDavid Howells #define MAP_ASCII7SEG_ALPHANUM_LC			\
166607ca46eSDavid Howells 	_MAP_0_32_ASCII_SEG7_NON_PRINTABLE	\
167607ca46eSDavid Howells 	_MAP_33_47_ASCII_SEG7_SYMBOL		\
168607ca46eSDavid Howells 	_MAP_48_57_ASCII_SEG7_NUMERIC		\
169607ca46eSDavid Howells 	_MAP_58_64_ASCII_SEG7_SYMBOL		\
170607ca46eSDavid Howells 	_MAP_97_122_ASCII_SEG7_ALPHA_LOWER	\
171607ca46eSDavid Howells 	_MAP_91_96_ASCII_SEG7_SYMBOL		\
172607ca46eSDavid Howells 	_MAP_97_122_ASCII_SEG7_ALPHA_LOWER	\
173607ca46eSDavid Howells 	_MAP_123_126_ASCII_SEG7_SYMBOL
174607ca46eSDavid Howells 
175607ca46eSDavid Howells #define SEG7_DEFAULT_MAP(_name)		\
176607ca46eSDavid Howells 	SEG7_CONVERSION_MAP(_name,MAP_ASCII7SEG_ALPHANUM)
177607ca46eSDavid Howells 
178607ca46eSDavid Howells #endif	/* MAP_TO_7SEGMENT_H */
179607ca46eSDavid Howells 
180