如何解决返回参数较少的函数指针
这可能是一个棘手的问题,我什至不知道如何正确地说出问题的标题...
我正在开发一个应该是多平台的代码库,即:Arduino、PSoC Creator 等。启用多平台支持的解决方法是通过将硬件与处理器中的对象链接起来的函数指针。这种描述可能很幼稚,因此下面的代码是一个用于控制移位寄存器的库的示例。
ShiftRegister.h -> Contains the object related declarations
ShiftRegister.cpp -> Contains the object deFinitions
ShiftRegister.ino -> Actual Arduino Sketch
ShiftRegister.h
#ifndef SHIFT_REGISTER
#define SHIFT_REGISTER
/////////////////////////////////////////////////////////////////////////////////////
// Link DeFinitions
#define ENABLE_PIN_ENABLED
#define CLEAR_PIN_ENABLED
#define LENGTH_SCAN_PIN_ENABLED
#define PIN_SETUP_ENABLED
#define DATA_BUFFER_ENABLED
//
/////////////////////////////////////////////////////////////////////////////////////
// Integrated Ciruits Library Include
#include <IC.h>
//
/////////////////////////////////////////////////////////////////////////////////////
// Arduino Specific Code
#define DATA_PIN 8
#define ENABLE_PIN 9
#define LATCH_PIN 10
#define CLOCK_PIN 11
#define CLEAR_PIN 12
#define LENGTH_SCAN 13
/////////////////////////////////////////////////////////////////////////////////////
// Shift Register Object
extern IC::ShiftRegister::Unidirectional::SIPO::Controller SR;
//
/////////////////////////////////////////////////////////////////////////////////////
// Function used to initialize the Shift Register Object
void SR_Start();
//
/////////////////////////////////////////////////////////////////////////////////////
// Functions for linking the Shift Register Object with the hardware
void SR_Data(bool state); // Function Passed to the Shift Register Object
void SR_Clock(bool state); // Function Passed to the Shift Register Object
void SR_Latch(bool state); // Function Passed to the Shift Register Object
#ifdef ENABLE_PIN_ENABLED
void SR_Enable(bool state); // Function Passed to the Shift Register Object
#endif
#ifdef CLEAR_PIN_ENABLED
void SR_Clear(bool state); // Function Passed to the Shift Register Object
#endif
#ifdef LENGTH_SCAN_PIN_ENABLED
bool SR_LengthScan(); // Function Passed to the Shift Register Object
#endif
#ifdef PIN_SETUP_ENABLED
void SR_PinSetup(); // Function Passed to the Shift Register Object
#endif
//
/////////////////////////////////////////////////////////////////////////////////////
#endif//SHIFT_REGISTER
ShiftRegister.cpp
#include "ShiftRegister.h"
// Adjust SR Properties to fit with your circuit
using namespace IC::ShiftRegister::Unidirectional::SIPO;
Controller SR;
///////////////////////////////////////////////////////////////////////////////////////
// Function used to initialize the Shift Register Object
void SR_Start()
{
///////////////////////////////////////////////////////////////////////////////////
// Attach Hardware Link Functions to the Shift Register Object
#ifdef PIN_SETUP_ENABLED
SR.AttachPinSetupAPI(SR_PinSetup);
#else
pinMode(DATA_PIN,OUTPUT);
pinMode(CLOCK_PIN,OUTPUT);
pinMode(LATCH_PIN,OUTPUT);
#ifdef ENABLE_PIN_ENABLED
pinMode(ENABLE_PIN,OUTPUT);
#endif
#ifdef CLEAR_PIN_ENABLED
pinMode(CLEAR_PIN,OUTPUT);
#endif
#ifdef LENGTH_SCAN_PIN_ENABLED
pinMode(LENGTH_SCAN,INPUT);
#endif
#endif
SR.AttachDataAPI(SR_Data);
SR.AttachClockAPI(SR_Clock);
SR.AttachLatchAPI(SR_Latch);
#ifdef ENABLE_PIN_ENABLED
SR.AttachEnableAPI(SR_Enable);
#endif
#ifdef CLEAR_PIN_ENABLED
SR.AttachClearaPI(SR_Clear);
#endif
#ifdef LENGTH_SCAN_PIN_ENABLED
SR.AttachLengthScanAPI(SR_LengthScan);
#else
SR.Properties().SetBitLength(8);
#endif
//
///////////////////////////////////////////////////////////////////////////////////
// Set Shift Register Object Properties for operation
/////////////////////////////////////////////////////////////////////////////////
// Logic Related
SR.SetDataLogic(Data::Logic::Regular);
SR.SetClockLogic(Clock::Logic::Rising);
SR.SetLatchLogic(Latch::Logic::Rising);
#ifdef ENABLE_PIN_ENABLED
SR.SetEnableLogic(Enable::Logic::Mode1);
#endif
#ifdef CLEAR_PIN_ENABLED
SR.SetClearLogic(Clear::Logic::Mode2);
#endif
//
/////////////////////////////////////////////////////////////////////////////////
// Data Management Properties
SR.SetBitNumberingMode(BitNumbering::MsbFirst);
#ifdef DATA_BUFFER_ENABLED
SR.DataBuffer_Enabled();
#endif
//
/////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////
SR.Start();
SR.Clear();
#ifdef LENGTH_SCAN_PIN_ENABLED
SR.ScanLength(1);
#endif
}
//
///////////////////////////////////////////////////////////////////////////////////////
// Functions for linking the Shift Register Object with the hardware
/////////////////////////////////////////////////////////////////////////////////////
// Data
void SR_Data(bool state)
{
digitalWrite(DATA_PIN,state);
}
//
/////////////////////////////////////////////////////////////////////////////////////
// Clock
void SR_Clock(bool state)
{
digitalWrite(CLOCK_PIN,state);
}
//
/////////////////////////////////////////////////////////////////////////////////////
// Latch
void SR_Latch(bool state)
{
digitalWrite(LATCH_PIN,state);
}
//
/////////////////////////////////////////////////////////////////////////////////////
// Enable
#ifdef ENABLE_PIN_ENABLED
void SR_Enable(bool state)
{
digitalWrite(ENABLE_PIN,state);
}
#endif
//
/////////////////////////////////////////////////////////////////////////////////////
// Clear
#ifdef CLEAR_PIN_ENABLED
void SR_Clear(bool state)
{
digitalWrite(CLEAR_PIN,state);
}
#endif
//
/////////////////////////////////////////////////////////////////////////////////////
// PinSetup
#ifdef PIN_SETUP_ENABLED
void SR_PinSetup()
{
pinMode(DATA_PIN,INPUT);
#endif
}
#endif
//
/////////////////////////////////////////////////////////////////////////////////////
// Length Scan
#ifdef LENGTH_SCAN_PIN_ENABLED
bool SR_LengthScan()
{
return digitalRead(LENGTH_SCAN);
}
#endif
//
/////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////
ShiftRegister.ino
#include "ShiftRegister.h"
void setup()
{
Serial.begin(115200);
//Initialize the SR Object
SR_Start();
}
void loop()
{
SR.SetState(0,1,1);
delay(1000);
SR.SetState(0,0);
SR.SetState(1,0);
SR.SetState(5,1);
delay(1000);
SR.Clear();
delay(1000);
}
展示了我正在使用的结构的一个工作示例后,我想知道是否有一种方法可以实现一些功能来处理将移位寄存器对象与硬件链接的功能,我的意思是声明硬件arduino 上的功能只需要设置硬件引脚,例如:
将 ShiftRegister.h 更改为:
#ifndef SHIFT_REGISTER
#define SHIFT_REGISTER
/////////////////////////////////////////////////////////////////////////////////////
// Link DeFinitions
#define ENABLE_PIN_ENABLED
#define CLEAR_PIN_ENABLED
#define LENGTH_SCAN_PIN_ENABLED
#define PIN_SETUP_ENABLED
#define DATA_BUFFER_ENABLED
//
/////////////////////////////////////////////////////////////////////////////////////
// Integrated Ciruits Library Include
#include <IC.h>
//
/////////////////////////////////////////////////////////////////////////////////////
// Arduino Specific Code
#define DATA_PIN 8
#define ENABLE_PIN 9
#define LATCH_PIN 10
#define CLOCK_PIN 11
#define CLEAR_PIN 12
#define LENGTH_SCAN 13
/////////////////////////////////////////////////////////////////////////////////////
// Shift Register Object
extern IC::ShiftRegister::Unidirectional::SIPO::Controller SR;
//
/////////////////////////////////////////////////////////////////////////////////////
// Function used to initialize the Shift Register Object
void SR_Start();
//
/////////////////////////////////////////////////////////////////////////////////////
#endif//SHIFT_REGISTER
将 ShiftRegister.cpp 更改为:
#include "ShiftRegister.h"
// Adjust SR Properties to fit with your circuit
using namespace IC::ShiftRegister::Unidirectional::SIPO;
Controller SR;
///////////////////////////////////////////////////////////////////////////////////////
// Function used to initialize the Shift Register Object
void SR_Start()
{
///////////////////////////////////////////////////////////////////////////////////
// Attach Hardware Link Functions to the Shift Register Object
#ifdef PIN_SETUP_ENABLED
SR.AttachPinSetupAPI(ArduinopinSetupHelper(DATA_PIN,CLOCK_PIN,LATCH_PIN,ENABLE_PIN,CLEAR_PIN,LENGTH_SCAN_PIN));
#else
pinMode(DATA_PIN,INPUT);
#endif
#endif
SR.AttachDataAPI(ArduinoDataHelper(DATA_PIN));
SR.AttachClockAPI(ArduinoClockHelper(CLOCK_PIN));
SR.AttachLatchAPI(ArduinoLatchHelper(LATCH_PIN));
#ifdef ENABLE_PIN_ENABLED
SR.AttachEnableAPI(ArduinoEnableHelper(ENABLE_PIN));
#endif
#ifdef CLEAR_PIN_ENABLED
SR.AttachClearaPI(ArduinoClearHelper(CLEAR_PIN));
#endif
#ifdef LENGTH_SCAN_PIN_ENABLED
SR.AttachLengthScanAPI(ArduinoLengthScanHelper(LENGHT_SCAN_PIN));
#else
SR.Properties().SetBitLength(8);
#endif
//
///////////////////////////////////////////////////////////////////////////////////
// Set Shift Register Object Properties for operation
/////////////////////////////////////////////////////////////////////////////////
// Logic Related
SR.SetDataLogic(Data::Logic::Regular);
SR.SetClockLogic(Clock::Logic::Rising);
SR.SetLatchLogic(Latch::Logic::Rising);
#ifdef ENABLE_PIN_ENABLED
SR.SetEnableLogic(Enable::Logic::Mode1);
#endif
#ifdef CLEAR_PIN_ENABLED
SR.SetClearLogic(Clear::Logic::Mode2);
#endif
//
/////////////////////////////////////////////////////////////////////////////////
// Data Management Properties
SR.SetBitNumberingMode(BitNumbering::MsbFirst);
#ifdef DATA_BUFFER_ENABLED
SR.DataBuffer_Enabled();
#endif
//
/////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////
SR.Start();
SR.Clear();
#ifdef LENGTH_SCAN_PIN_ENABLED
SR.ScanLength(1);
#endif
}
//
///////////////////////////////////////////////////////////////////////////////////////
有了第二组文件,唯一需要实现的是 Arduino Helper Functions,它应该以 uint8_t 作为参数并返回一个函数指针,例如给 digitalWrite,但 pin 参数预设为一个值
>示例(我确定这是完全错误的,但仅用于说明我要实现的目标)
void(*)(bool) ArduinoDataHelper(uint8_t Pin)
{
return digitalWrite(Pin,state);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。