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