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