首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在文件之间共享全局变量的正确方法

在文件之间共享全局变量的正确方法
EN

Stack Overflow用户
提问于 2021-06-23 06:51:26
回答 2查看 80关注 0票数 0

我在奈阿片领导的图书馆有个包装纸。我把文件命名为leds.cpp和分类帐

leds.cpp:

代码语言:javascript
运行
复制
#include "leds.h"

RgbColor nice_purple(153,153,255);
RgbColor orange(255,165,0);
RgbColor bright_purple(204,0,102);
RgbColor cyan(0, colorSaturation, colorSaturation);
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor yellow(colorSaturation,colorSaturation,0);
RgbColor white(colorSaturation);
RgbColor black(0);
RgbColor purple(colorSaturation, 0, colorSaturation);
NeoPixelBus<NeoGrbFeature, NeoEsp32Rmt0800KbpsMethod> strip(PixelCount, PixelPin);

void toggle_led_strip(RgbColor colour,int number){
      for(int i=0;i<=number;i++){
        strip.SetPixelColor(0, colour);
        strip.SetPixelColor(1, colour);
        strip.SetPixelColor(2, colour);
        strip.SetPixelColor(3, colour);
        strip.SetPixelColor(4, colour);
        strip.SetPixelColor(5, colour);
        strip.SetPixelColor(6, colour);
        strip.SetPixelColor(7, colour);
        strip.Show();
        delay(500);
        strip.SetPixelColor(0, black);
        strip.SetPixelColor(1, black);
        strip.SetPixelColor(2, black);
        strip.SetPixelColor(3, black);
        strip.SetPixelColor(4, black);
        strip.SetPixelColor(5, black);
        strip.SetPixelColor(6, black);
        strip.SetPixelColor(7, black);
        strip.Show();
        delay(500);
      }
    }


void toggle_led_strip_on(RgbColor colour){
        strip.SetPixelColor(0, colour);
        strip.SetPixelColor(1, colour);
        strip.SetPixelColor(2, colour);
        strip.SetPixelColor(3, colour);
        strip.SetPixelColor(4, colour);
        strip.SetPixelColor(5, colour);
        strip.SetPixelColor(6, colour);
        strip.SetPixelColor(7, colour);
        strip.Show();   
      }


      void toggle_led_strip_off(){
        strip.SetPixelColor(0, black);
        strip.SetPixelColor(1, black);
        strip.SetPixelColor(2, black);
        strip.SetPixelColor(3, black);
        strip.SetPixelColor(4, black);
        strip.SetPixelColor(5, black);
        strip.SetPixelColor(6, black);
        strip.SetPixelColor(7, black);
        strip.Show();     
      }


void clear_LED_strip(int number){
  for (int i=0;i<number;i++)
         strip.SetPixelColor(i, black);
        //delay(1);
          strip.Show();
}

void LED_strip_ON(RgbColor colour,int number){
    for (int i=0;i<number;i++)
        strip.SetPixelColor(i, colour);
        strip.Show();

}

和账簿:

代码语言:javascript
运行
复制
#ifndef LEDS_H
#define LEDS_H

#include "NeoPixelBus.h"
#include <Adafruit_I2CDevice.h>
#define PixelCount  8 // this example assumes 4 pixels, making it smaller will cause a failure
#define PixelPin  27  // make sure to set this to the correct pin, ignored for Esp8266
#define colorSaturation 250


void toggle_led_strip(RgbColor colour,int number);
void toggle_led_strip_on(RgbColor colour);
void toggle_led_strip_off();
void clear_LED_strip(int number);
void LED_strip_ON(RgbColor colour,int number);


#endif

现在,我想在多个其他文件中使用这些函数,我想澄清的问题不多。

  1. 我有modbus.cpp和modbus.h文件,我用modbus.cpp编写的函数之一涉及切换一个LED条,我需要在这个文件中包含分类帐,以便能够使用这些函数。

我是在modbus.h还是modbus.cpp中包括了"leds.h“?有什么不同吗?

  1. 所有的led函数都以颜色为参数。颜色变量类型为RgbColor。所有的颜色都是用leds.cpp定义的。然后我必须声明我将在modbus中使用的颜色作为外部颜色,如extern RgbColor blue;。同样,我担心是否应该在modbus.h或modbus.cpp.

中声明

我很感激任何建议。提前谢谢。

我的modbus.cpp涉及以下功能:

代码语言:javascript
运行
复制
#include "modbus_custom.h"



enum MODBUS_REGISTERS{
  MAIN_REG = 10,
  QUANTITY_REG = 11,
  SLAVE_ID_REG = 12,
  SERIAL_LEN_REG = 13,
  SERIAL1_REG = 14,
  SERIAL2_REG = 15,
  SERIAL3_REG = 16,
  SERIAL4_REG = 17,
  SERIAL5_REG = 18,
  SERIAL6_REG = 19,
  SERIAL7_REG = 20,
  SERIAL8_REG = 21,
  SERIAL9_REG = 22,
  SERIAL10_REG = 23,
  NUMBER_TO_PICK_REG = 24,
};



ModbusRTU mb; // class object 

void Read_modbus_Pildymas(){
    switch(mb.Hreg(MAIN_REG)){
      case 1:
        Serial.println("Received start of pildymas code");
        mb.Hreg(MAIN_REG, 0);   //ALWAYS REPLY WITH 0 IN THE REGISTER
        break;
      case 2:
        Serial.println("Received 2 code");
        toggle_led_strip(blue, 2);
        
    }
}

我的modbus.h看起来:

代码语言:javascript
运行
复制
#ifndef MODBUS_CUSTOM_H
#define MODBUS_CUSTOM_H

#include <ModbusRTU.h>
#include "definitions.h"
#include "leds.h"



#define RXD2 16
#define TXD2 17
#define DIR 15
#define REG_Pildymas 10
#define REG_Picking 100


extern uint16_t SLAVE_ID;
extern item_inside_struct item_inside;
extern int pildymas_flag;
extern char current_status[20];
extern int clear_data_flag;
extern int number_to_pick;
extern int screen_colour;
extern char DEVICE_ID[10];

extern RgbColor blue;
extern Adafruit_ST7735 tft;

void Read_modbus_Pildymas();
void modbus_respond_pildymas();
void modbus_respond_clear_data();


#endif

尝试Lundin方法

在我的账簿里,我现在创造了一个有各种颜色的明灯:

代码语言:javascript
运行
复制
typedef enum
{
  nice_purple,
  orange,
  bright_purple,
  cyan,
  red,
  green,
  blue,
  yellow,
  white,
  black,
  purple,
} color_t;

在我的leds.cpp:

我已经创建了这个静态的const RgbColor colors11

代码语言:javascript
运行
复制
static const RgbColor colors[11] =
{
  [nice_purple] = {153,153,255},
  [orange]  = {255,165,0},
  [bright_purple]  = {204,0,102},
  [cyan]  = {0, colorSaturation, colorSaturation},
  [red]  = {colorSaturation, 0, 0},
  [green]  = {0, colorSaturation, 0},
  [blue]  = {0, colorSaturation, 0},
  [yellow]  = {colorSaturation,colorSaturation,0},
  [white]  = {colorSaturation},
  [black]  = {0},
  [purple]  = {colorSaturation, 0, colorSaturation},
};



void toggle_led_strip(color_t colour,int number){
      for(int i=0;i<=number;i++){
        strip.SetPixelColor(0, colour);
        strip.SetPixelColor(1, colour);
        strip.SetPixelColor(2, colour);
        strip.SetPixelColor(3, colour);
        strip.SetPixelColor(4, colour);
        strip.SetPixelColor(5, colour);
        strip.SetPixelColor(6, colour);
        strip.SetPixelColor(7, colour);
        strip.Show();
        delay(100);
        strip.SetPixelColor(0, black);
        strip.SetPixelColor(1, black);
        strip.SetPixelColor(2, black);
        strip.SetPixelColor(3, black);
        strip.SetPixelColor(4, black);
        strip.SetPixelColor(5, black);
        strip.SetPixelColor(6, black);
        strip.SetPixelColor(7, black);
        strip.Show();
        delay(100);
      }
    }

我不完全了解这些数据的结构,你能帮我澄清一下吗?据我理解,我已经创建了一个由11个RgbColor元素组成的数组。

代码语言:javascript
运行
复制
[nice_purple] = {153,153,255},

为什么需要这些花括号?{ 153,153,255 }和为什么在这里使用括号?[ nice_purple ]

在包括“分类帐”之后,在我的主C中

我将这一职能称为:

代码语言:javascript
运行
复制
toggle_led_strip(red,1);

我不完全理解enum在分类帐中的用法。我假设我的枚举中的每一种颜色都有从0到最后一个颜色编号的编号。

然后在我的RgbColor数组中,我简单地声明第0元素为{153,153,255},第1元素为{255,165,0}等等。这种理解是正确的吗?

EN

回答 2

Stack Overflow用户

发布于 2021-06-23 08:19:21

我是要在modbus.h还是modbus.cpp中包括"leds.h“呢?有什么不同吗?

这是有点主观,有不同的风格。

  • 的一种风格是,只在内部使用的头部应该包含在.C/..cpp文件中。好处是减少了名称空间的杂乱,因为应用程序代码(包括头)不会同时获得附加的标头。
  • 的另一种样式是,模块使用的所有标头都应该包含在.h文件中,这是一种向应用程序程序员记录模块使用的库的方式。这是存在于某个模块中的所有程序依赖项(“耦合”)的自记录代码。头文件被视为公共文件,而.c文件被视为私有文件,因此应用程序程序员不需要挖掘.c文件就可以找到(它甚至可能是不可用的)。这种方法还使您在导入模块时更容易跟踪神秘的链接器错误,但忘记链接所需的一些必要的.c文件。

我订阅了“总是把所有包括在头”阵营个人。

,然后我必须声明我将在modbus中使用的颜色作为extern,例如extern RgbColor蓝色;同样,我担心它是否应该在modbus.h或modbus.cpp中声明。

这是完全错误的设计。.cpp文件中的颜色定义是该文件的专用(应该是static)。打电话的人不需要,也不应该关心他们。问问自己,为什么Modbus模块需要知道颜色是如何定义的.很明显,它不应该知道。它应该知道Modbus的事,什么也不知道。

经验法则:extern在C或C++程序中的存在几乎总是表明设计和意大利面编程都很糟糕。有一些罕见的例外,但一般情况下,如果您的设计是合理的,您就不应该使用它。

您应该做的是在leds头中为调用者提供一个枚举,例如:

代码语言:javascript
运行
复制
typedef enum
{
  GREEN,
  BLUE,
  ...
} color_t;

然后,您的API应该使用以下枚举:

代码语言:javascript
运行
复制
void toggle_led_strip_on (color_t color);

然后在leds.cpp文件中应该有类似的内容(这是伪代码,我不太确定您是在使用C还是C++):

代码语言:javascript
运行
复制
static const RgbColor colors[n] =
{
  [GREEN] = {0, colorSaturation, 0},
  [BLUE]  = {0, 0, colorSaturation},
  ...
};

现在,您可以使用调用方传递的枚举作为表查找。

票数 1
EN

Stack Overflow用户

发布于 2021-06-23 07:37:45

这个答案不会讨论整体的界面设计(即它是否好)。答案是基于OP已经选择的设计。

我是要在modbus.h还是modbus.cpp中包括"leds.h“呢?

如果modbus.h中没有任何东西需要了解分类帐中的内容,则在modbus.cpp中进行包含操作。否则,在modbus.h中

有什么区别吗?

当您在modbus.h中执行包含时,所有使用"modbus“的用户也将了解"leds”。如果他们不需要这些信息,就没有理由提供给他们。

此外,当您在编译单元中包含不必要的文件时,它可能会减慢编译速度。

,然后我必须声明我将在modbus中使用的颜色作为extern,例如extern RgbColor蓝色;同样,我担心它是否应该在modbus.h或modbus.cpp中声明。

都不是。外部解密应记入分类帐。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68094885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档