对嵌入式系统编写常量参数最佳实践 [英] Best practice on writing constant parameters for embedded systems

查看:92
本文介绍了对嵌入式系统编写常量参数最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是静态常量的VS在C#定义,案件中的嵌入式系统。

在大/中项目通过了下来code和模块,什么是对你的包含文件,模块等写常参数的最佳实践?

在一个code通过向下的,你不知道,如果你选择的名称在其他一些包含文件中定义或可能与EXTERN或在其他一些文件宏可能包括被称为您的文件。

有了这3个选项:


  1. 静态const int的char_height = 12;

  2. 的#define CHAR_HEIGHT 12

  3. 枚举{char_height = 12};

哪一个更好呢?

(未知内存限制的嵌入式系统上)

原来的code主要使用的这一点,但这类常量在几个方面都实现随意(并且甚至在不同的位置的#define 相同的文件),因为它似乎是几个人开发了这个演示软件有一定的设备。

具体而言,这是一个演示code,表现出一定设备的每个硬件和SDK功能关闭。


  大多数我正在考虑数据的

是用来配置环境中的一种:屏幕尺寸,字符集的特点,一些改善code的可读性。不上自动配置一个编译器和pre-处理器可以做。但是,由于有很多code在那里,我怕全局名称冲突的,我不愿意用#定义的


目前,我正在考虑,这将是更好地从头开始重写该项目,并重新实现大部分已写入功能,只从一个C文件中得到他们的常量或重组常量实施只是一个风格。

不过:<​​/ P>


  1. 这是一个人的项目(因此它需要占用大量的时间来重新实现一切)

  2. 已实施的code ++工程,并已多次修订。 (如果它不破...)


解决方案

始终考虑可读性和内存的限制。此外,宏简单地复制编译之前发生/粘贴操作。有了这样说我喜欢做如下:


  • 我定义是为一定的所有变量静态常量如果他们是在一个C文件(如不能跨多个文件全局访问)使用。定义为 const的任何应在文件范围内放置在ROM中时。显然,他们正在初始化后,你不能改变这些变量。

  • 我定义使用的#define 所有常量的值。

  • 我用枚举操作它增加了可读性。在这里你有价值观我preFER枚举明确声明的意图的固定范围内的任何地方。

试着用面向对象的角度(即使c不OO)接近该项目。隐藏私人活动(不创建在标题中的原型),不要使用全局变量,如果你能避免它,标志着变量只应静态

This is a case of "static const" vs "#define" in C" for embedded systems.

On large/mid projects with "passed-down" code and modules, what is the best practice on writing constant parameters for your include files, modules, etc?

In a code "passed-down" where you don't know if the names you're choosing are defined in some other included file or might be called with extern or as macros in some other file that might include your file.

Having these 3 options:

  1. static const int char_height = 12;
  2. #define CHAR_HEIGHT 12
  3. enum { char_height = 12 };

which one would be better (on an embedded system with unknown memory constraints)?

The original code uses mainly #define's for this, but these kind of constants are haphazardly implemented in several ways (and at different locations even in the same files) since it seems several people developed this demo software for a certain device.

Specifically, this is a demo code, showing off every hardware and SDK feature of a certain device.

Most of the data I'm thinking about is the kind used to configure the environment: screen dimensions, charset characteristics, something to improve the readability of the code. Not on the automatic configuration a compiler and pre-processor could do. But since there's a lot of code in there and I'm afraid of global name conflicts, I'm reluctant to use #define's

Currently, I'm considering that it would be better to rewrite the project from scratch and re-implement most of the already written functions to get their constants from just one c file or reorganize the constants' implementation to just one style.

But:

  1. This is a one person project (so it would take a lot of time to re-implement everything)
  2. The already implemented code works and it has been revised several times. (If it's not broken...)

解决方案

Always consider readability and memory constraints. Also, macros are simply copy/paste operations that occur before compilation. With that being said I like to do the following:

  • I define all variables that are constant as being static const if they are to be used in one c file (e.g. not globally accessible across multiple files). Anything defined as const shall be placed in ROM when at file scope. Obviously you cannot change these variables after they're initialized.
  • I define all constant values using #define.
  • I use enumerations where it adds to readability. Any place where you have a fixed range of values I prefer enumerations to explicitly state the intent.

Try to approach the project with an object oriented perspective (even though c isn't OO). Hide private functions (don't create a prototype in the header), do not use globals if you can avoid it, mark variables that should only reside in one c module (file) as static, etc.

这篇关于对嵌入式系统编写常量参数最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆