Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
0002 /*
0003  * Copyright (c) 2005 Henk Vergonet <Henk.Vergonet@gmail.com>
0004  */
0005 
0006 #ifndef MAP_TO_7SEGMENT_H
0007 #define MAP_TO_7SEGMENT_H
0008 
0009 /* This file provides translation primitives and tables for the conversion
0010  * of (ASCII) characters to a 7-segments notation.
0011  *
0012  * The 7 segment's wikipedia notation below is used as standard.
0013  * See: https://en.wikipedia.org/wiki/Seven_segment_display
0014  *
0015  * Notation:    +-a-+
0016  *      f   b
0017  *      +-g-+
0018  *      e   c
0019  *      +-d-+
0020  *
0021  * Usage:
0022  *
0023  *   Register a map variable, and fill it with a character set:
0024  *  static SEG7_DEFAULT_MAP(map_seg7);
0025  *
0026  *
0027  *   Then use for conversion:
0028  *  seg7 = map_to_seg7(&map_seg7, some_char);
0029  *  ...
0030  *
0031  * In device drivers it is recommended, if required, to make the char map
0032  * accessible via the sysfs interface using the following scheme:
0033  *
0034  * static ssize_t map_seg7_show(struct device *dev,
0035  *              struct device_attribute *attr, char *buf)
0036  * {
0037  *  memcpy(buf, &map_seg7, sizeof(map_seg7));
0038  *  return sizeof(map_seg7);
0039  * }
0040  * static ssize_t map_seg7_store(struct device *dev,
0041  *               struct device_attribute *attr, const char *buf,
0042  *               size_t cnt)
0043  * {
0044  *  if(cnt != sizeof(map_seg7))
0045  *      return -EINVAL;
0046  *  memcpy(&map_seg7, buf, cnt);
0047  *  return cnt;
0048  * }
0049  * static DEVICE_ATTR_RW(map_seg7);
0050  *
0051  * History:
0052  * 2005-05-31   RFC linux-kernel@vger.kernel.org
0053  */
0054 #include <linux/errno.h>
0055 
0056 
0057 #define BIT_SEG7_A      0
0058 #define BIT_SEG7_B      1
0059 #define BIT_SEG7_C      2
0060 #define BIT_SEG7_D      3
0061 #define BIT_SEG7_E      4
0062 #define BIT_SEG7_F      5
0063 #define BIT_SEG7_G      6
0064 #define BIT_SEG7_RESERVED   7
0065 
0066 struct seg7_conversion_map {
0067     unsigned char   table[128];
0068 };
0069 
0070 static __inline__ int map_to_seg7(struct seg7_conversion_map *map, int c)
0071 {
0072     return c >= 0 && c < sizeof(map->table) ? map->table[c] : -EINVAL;
0073 }
0074 
0075 #define SEG7_CONVERSION_MAP(_name, _map)    \
0076     struct seg7_conversion_map _name = { .table = { _map } }
0077 
0078 /*
0079  * It is recommended to use a facility that allows user space to redefine
0080  * custom character sets for LCD devices. Please use a sysfs interface
0081  * as described above.
0082  */
0083 #define MAP_TO_SEG7_SYSFS_FILE  "map_seg7"
0084 
0085 /*******************************************************************************
0086  * ASCII conversion table
0087  ******************************************************************************/
0088 
0089 #define _SEG7(l,a,b,c,d,e,f,g)  \
0090       ( a<<BIT_SEG7_A | b<<BIT_SEG7_B | c<<BIT_SEG7_C | d<<BIT_SEG7_D | \
0091     e<<BIT_SEG7_E | f<<BIT_SEG7_F | g<<BIT_SEG7_G )
0092 
0093 #define _MAP_0_32_ASCII_SEG7_NON_PRINTABLE  \
0094     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,
0095 
0096 #define _MAP_33_47_ASCII_SEG7_SYMBOL        \
0097  _SEG7('!',0,0,0,0,1,1,0), _SEG7('"',0,1,0,0,0,1,0), _SEG7('#',0,1,1,0,1,1,0),\
0098  _SEG7('$',1,0,1,1,0,1,1), _SEG7('%',0,0,1,0,0,1,0), _SEG7('&',1,0,1,1,1,1,1),\
0099  _SEG7('\'',0,0,0,0,0,1,0),_SEG7('(',1,0,0,1,1,1,0), _SEG7(')',1,1,1,1,0,0,0),\
0100  _SEG7('*',0,1,1,0,1,1,1), _SEG7('+',0,1,1,0,0,0,1), _SEG7(',',0,0,0,0,1,0,0),\
0101  _SEG7('-',0,0,0,0,0,0,1), _SEG7('.',0,0,0,0,1,0,0), _SEG7('/',0,1,0,0,1,0,1),
0102 
0103 #define _MAP_48_57_ASCII_SEG7_NUMERIC       \
0104  _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),\
0105  _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),\
0106  _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),\
0107  _SEG7('9',1,1,1,1,0,1,1),
0108 
0109 #define _MAP_58_64_ASCII_SEG7_SYMBOL        \
0110  _SEG7(':',0,0,0,1,0,0,1), _SEG7(';',0,0,0,1,0,0,1), _SEG7('<',1,0,0,0,0,1,1),\
0111  _SEG7('=',0,0,0,1,0,0,1), _SEG7('>',1,1,0,0,0,0,1), _SEG7('?',1,1,1,0,0,1,0),\
0112  _SEG7('@',1,1,0,1,1,1,1),
0113 
0114 #define _MAP_65_90_ASCII_SEG7_ALPHA_UPPR    \
0115  _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),\
0116  _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),\
0117  _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),\
0118  _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),\
0119  _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),\
0120  _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),\
0121  _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),\
0122  _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),\
0123  _SEG7('Y',0,1,1,0,0,1,1), _SEG7('Z',1,1,0,1,1,0,1),
0124 
0125 #define _MAP_91_96_ASCII_SEG7_SYMBOL        \
0126  _SEG7('[',1,0,0,1,1,1,0), _SEG7('\\',0,0,1,0,0,1,1),_SEG7(']',1,1,1,1,0,0,0),\
0127  _SEG7('^',1,1,0,0,0,1,0), _SEG7('_',0,0,0,1,0,0,0), _SEG7('`',0,1,0,0,0,0,0),
0128 
0129 #define _MAP_97_122_ASCII_SEG7_ALPHA_LOWER  \
0130  _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),\
0131  _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),\
0132  _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),\
0133  _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),\
0134  _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),\
0135  _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),\
0136  _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),\
0137  _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),\
0138  _SEG7('y',0,1,1,1,0,1,1), _SEG7('Z',1,1,0,1,1,0,1),
0139 
0140 #define _MAP_123_126_ASCII_SEG7_SYMBOL      \
0141  _SEG7('{',1,0,0,1,1,1,0), _SEG7('|',0,0,0,0,1,1,0), _SEG7('}',1,1,1,1,0,0,0),\
0142  _SEG7('~',1,0,0,0,0,0,0),
0143 
0144 /* Maps */
0145 
0146 /* This set tries to map as close as possible to the visible characteristics
0147  * of the ASCII symbol, lowercase and uppercase letters may differ in
0148  * presentation on the display.
0149  */
0150 #define MAP_ASCII7SEG_ALPHANUM          \
0151     _MAP_0_32_ASCII_SEG7_NON_PRINTABLE  \
0152     _MAP_33_47_ASCII_SEG7_SYMBOL        \
0153     _MAP_48_57_ASCII_SEG7_NUMERIC       \
0154     _MAP_58_64_ASCII_SEG7_SYMBOL        \
0155     _MAP_65_90_ASCII_SEG7_ALPHA_UPPR    \
0156     _MAP_91_96_ASCII_SEG7_SYMBOL        \
0157     _MAP_97_122_ASCII_SEG7_ALPHA_LOWER  \
0158     _MAP_123_126_ASCII_SEG7_SYMBOL
0159 
0160 /* This set tries to map as close as possible to the symbolic characteristics
0161  * of the ASCII character for maximum discrimination.
0162  * For now this means all alpha chars are in lower case representations.
0163  * (This for example facilitates the use of hex numbers with uppercase input.)
0164  */
0165 #define MAP_ASCII7SEG_ALPHANUM_LC           \
0166     _MAP_0_32_ASCII_SEG7_NON_PRINTABLE  \
0167     _MAP_33_47_ASCII_SEG7_SYMBOL        \
0168     _MAP_48_57_ASCII_SEG7_NUMERIC       \
0169     _MAP_58_64_ASCII_SEG7_SYMBOL        \
0170     _MAP_97_122_ASCII_SEG7_ALPHA_LOWER  \
0171     _MAP_91_96_ASCII_SEG7_SYMBOL        \
0172     _MAP_97_122_ASCII_SEG7_ALPHA_LOWER  \
0173     _MAP_123_126_ASCII_SEG7_SYMBOL
0174 
0175 #define SEG7_DEFAULT_MAP(_name)     \
0176     SEG7_CONVERSION_MAP(_name,MAP_ASCII7SEG_ALPHANUM)
0177 
0178 #endif  /* MAP_TO_7SEGMENT_H */
0179