简体   繁体   English

微控制器显示驱动程序需要C或C ++中的异常字符串连接

[英]Need for unusual string concatenation in C or C++ for microcontroller display driver

I'm adding string and text support to Dean Reading's " SevSeg " display driver for 7-segment displays , controlled by Arduino, and I need some help. 我正在为由Arduino控制的7段显示器的 Dean Reading的“ SevSeg ”显示驱动程序添加字符串和文本支持,我需要一些帮助。

Due to limitations on the display, I have to create special cases where two digits on the display are used to display one character. 由于显示器的限制,我必须创建特殊情况,其中显示器上的两位数字用于显示一个字符。 Ex: "M" must be treated as a special case since one display digit cannot create an M by itself. 例如:“ M”必须作为一种特殊情况处理,因为一个显示位不能单独创建M。

What I need essentially is for the user to be able to write something like this: 我基本上需要的是用户能够编写如下内容:

char myString[] = "Help Me Do This!";

Except that I want them to be able to specify whether they want to use the special M (called "PSEUDO_M") instead. 除了我希望他们能够指定是否要使用特殊M(称为“ PSEUDO_M”)之外。 If they leave the above phrase as-is, the M will be replaced with a dash, and I don't want to prohibit this behavior since the user may want the phrase to fit in a set # of display digits more than they want a 2-digit M. Anyway, what I want is something like this: 如果他们将上述词组保持原样,则M将替换为破折号,并且我不想禁止这种行为,因为用户可能希望该词组比一组数字更适合显示的一组显示数字。 2位M。无论如何,我想要的是这样的:

const char PSEUDO_M = -128;

char myString[] = "Help " + PSEUDO_M + "e Do this!";
//OR perhaps:
char myString[] = "Help [PSEUDO_M]e Do this!"; 

I would then process their input string, treating the PSEUDO_M (dec -128) as a special case. 然后,我将处理它们的输入字符串,将PSEUDO_M(dec -128)视为特例。

Obviously, the above additions of strings doesn't work in C. 显然,以上字符串的添加在C中不起作用。

I know that a string is simply a char array, so when a user creates a string, the character decimal representation for the above (WITH the PSEUDO_M [-128] used in place of the M [77]) would be: 我知道一个字符串只是一个char数组,所以当用户创建一个字符串时,上述字符的十进制表示形式(用PSEUDO_M [-128]代替M [77])将是:

char myString[] = {72, 101, 108, 112, 32, -128, 101, 32, 68, 111, 32, 84, 104, 105, 115, 33, 0};

However, the above number array is not human-readable. 但是,以上数字数组不是人类可读的。 *The idea is that the user types in a string literal essentially, using standard characters. *这个想法是用户本质上使用标准字符输入字符串文字。 So, what's the best way to write a macro, function, or something else that will easily allow someone to type a phrase to be used in a string, but where M's (dec 77) will be replaced with PSEUDO_M's (dec -128), if the user desires it?* 因此,编写宏,函数或其他可以轻松地使某人键入要在字符串中使用的短语的最佳方法是什么,但是其中M(dec 77)将被PSEUDO_M(dec -128)代替, 如果用户希望吗?*

Note: ideally, the technique I use will not take up any extra bytes in the char array, since my PSEUDO_M could simply be represented as a single character (dec -128). 注意:理想情况下,我使用的技术不会占用char数组中的任何额外字节,因为我的PSEUDO_M可以简单地表示为单个字符(dec -128)。

Note that in C, multiple character literals in sequence are concatenated by the compiler; 请注意,在C语言中,编译器将按顺序连接多个字符文字; for example, "foo" "bar" is the same as "foobar" . 例如, "foo" "bar""foobar"相同。

Using this and a define, you can get a syntax that's human readable while still having zero runtime overhead. 使用此定义和定义,您可以获得一种人类可读的语法,同时运行时开销仍为零。

#define PSEUDO_M "\xAB" // Replace AB with the appropriate code

char myString[] = "Help " PSEUDO_M "e Do this!";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM