- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我卡住了。我正在尝试形成一个函数,它将吃掉无类函数指针和对象中的指针。这是我当前的代码,希望能解释更多。
(它应该在 Arduino 上运行,所以我不能使用大型库。)
首先,我将这个库用于 Arduino:
/* SimpleTimer - A timer library for Arduino.
* Author: mromani@ottotecnica.com
* Copyright (c) 2010 OTTOTECNICA Italy
*/
它采用在这种类型的设置计时器间隔上调用的函数:
typedef void (*timer_callback)(void);
据我所知,这是一个类函数,网页 Pointers to member functions 让我走得很远,但还不够远。可能是我的术语不足。
现在,我创建了自己的类,我想反过来使用这个 SimpleTimer 库。但是,如果我将 SimpleTimer 提供给我的类函数,它就不喜欢它们(据我所知)。但是如何在不改变 SimpleTimer 库的情况下实现这一点。
所以就有了 Robot 类,它有 Robot::halt()
。我希望机器人在一定时间内向前移动。像这样:
void Robot::forward(int speed, long time) {
reset();
timer.setTimer(time, c_func, 1);
analogWrite(l_a, speed);
analogWrite(r_a, speed);
isMoving(true);
}
void Robot::halt() {
__isMoving = false;
digitalWrite(r_a, LOW);
digitalWrite(r_b, LOW);
digitalWrite(l_b, LOW);
digitalWrite(l_a, LOW);
}
c_func 变量此时是一个无类函数,但我想使用 Robot::halt
函数。我看过,读过,学过,但还没有成功。我似乎无法理解这个,因为我错过了一些角度。
我试过了:
timer.setTimer(time, (this->*halt), 1);
timer.setTimer(time, Robot::*halt, 1);
timer.setTimer(time, &Robot::halt), 1);
但是这都是同一个问题/我只是在这里暗中刺伤......
编辑
之前,我说过不想更改 SimpleTimer 库代码。我想回归这个,我想改变它会有更好的选择。
已经感谢所有当前的答案,我只被允许将一个标记为可行的答案,实际上我在这里阅读的所有内容都非常有帮助。
要继续此操作,请更改 SimpleTimer 代码。这个类需要引用包含我的“暂停”函数的对象,对吧?因此,将 settimer 函数重载为将我的对象和我的函数作为两个单独的指针的东西会起作用......?我想我已经掌握了这个窍门,但是,我还没有头脑清醒。
编辑
我不知道谁又来了这个,但是,任何人都找到了这个线程。如果找到 Member Function Pointers and the Fastest Possible C++ Delegates 对函数指针和成员函数指针进行了很好的介绍。
编辑
得到它的工作,更改 SimpleTimer 库以使用此委托(delegate)系统: http://www.codeproject.com/KB/cpp/FastDelegate.aspx
它集成得非常好,在 Arduino 库中拥有这样的标准 Delegate 系统会很不错。
测试中的代码(工作)
类型定义
typedef FastDelegate0<> FuncDelegate;
机器人类中的代码:
void Robot::test(){
FuncDelegate f_delegate;
f_delegate = MakeDelegate(this, &Robot::halt);
timer.setTimerDelg(1, f_delegate, 1);
}
void Robot::halt() {
Serial.println("TEST");
}
SimpleTimer 类中的代码:
int SimpleTimer::setTimerDelg(long d, FuncDelegate f, int n){
f();
}
Arduino 在控制台中打印 TEST。
下一步将它放入一个数组中,在那里看不到很多问题。谢谢大家,这两天学到的东西我简直不敢相信。
那是什么味道?那是……的味道吗?成功!
对于感兴趣的人来说,使用的Delegate系统并不构成内存容量问题:使用 FastDelegate
AVR Memory Usage
----------------
Device: atmega2560
Program: 17178 bytes (6.6% Full)
(.text + .data + .bootloader)
Data: 1292 bytes (15.8% Full)
(.data + .bss + .noinit)
Finished building: sizedummy
没有 FastDelegate:
AVR Memory Usage
----------------
Device: atmega2560
Program: 17030 bytes (6.5% Full)
(.text + .data + .bootloader)
Data: 1292 bytes (15.8% Full)
(.data + .bss + .noinit)
Finished building: sizedummy
最佳答案
您可以通过创建 functor 来做到这一点对象,它充当计时器代码和您的代码之间的代理。
class MyHaltStruct
{
public:
MyHaltStruct(Robot &robot)
: m_robot(robot)
{ }
void operator()()
{ robot.halt(); }
private:
Robot &m_robot;
}
// ...
timer.setTimer(time, MyHaltStruct(*this), 1);
如果不能通过仿函数对象完成,您可以改为全局变量和函数,也许在命名空间中:
namespace my_robot_halter
{
Robot *robot = 0;
void halt()
{
if (robot)
robot->halt();
}
}
// ...
my_robot_halter::robot = this;
timer.setTimer(time, my_robot_halter::halt, 1);
这仅在您有一个机器人实例时才有效。
关于c++ - 带/不带类的回调函数指针 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819580/
运行 PostgreSQL(7.4 和 8.x),我认为这是可行的,但现在我遇到了错误。 我可以单独运行查询,它工作得很好,但如果我使用 UNION 或 UNION ALL,它会抛出错误。 这个错误:
我试图为我的应用程序创建一个导航,使用抽屉导航我的 fragment 之一(HomeFragment)有一个 ViewPager,可容纳 3 个 fragment (Bundy Clock、Annou
以我目前正在开发的应用为例: - 它有一个包含多个项目的抽屉导航;现在有两个项目让我感兴趣,我将它们称为 X 和 Y。 X 和 Y 都在单击时显示包含 x 元素或 y 元素列表的 fragment 选
我有一个形状为 (370,275,210) 的 NumPy 数组,我想将其重新整形为 (275,210,370)。我将如何在 Python 中实现这一点? 370是波段数,275是行数,210是图像包
我们如何与被子 UIViewController 阻止的父 UIViewController(具有按钮)交互。显然,触摸事件不会通过子 Nib 。 (启用用户交互) 注意:我正在加载默认和自定义 NI
我是 Jpa 新手,我想执行过程 我的代码如下 private static final String PERSISTENCE_UNIT_NAME = "todos"; private static
与安装了 LAMP 的 GCE 相比,选择与 Google Cloud SQL 链接的 GCE 实例有哪些优势? 我确定 GCE 是可扩展的,但是安装在其上的 mysql 数据库的可扩展性如何? 使用
这个问题在这里已经有了答案: Value receiver vs. pointer receiver (3 个答案) 关闭 3 年前。 我刚接触 golang。只是想了解为 Calc 类型声明的两种
我不小心按了一个快捷键,一个非常漂亮的断线出现在日期上。 有点像 # 23 Jun 2010 -------------------- 有人知道有问题的快捷方式吗?? (我在 mac 上工作!) 在
我正在Scala中编写正则表达式 val regex = "^foo.*$".r 这很好,但是如果我想做 var x = "foo" val regex = s"""^$x.*$""".r 现在我们有
以下 XML 文档在技术上是否相同? James Dean 19 和: James Dean 19 最佳答案 这两个文档在语义上是相同的。在 X
我在对数据帧列表运行稳健的线性回归模型(使用 MASS 库中的 rlm)时遇到问题。 可重现的示例: var1 <- c(1:100) var2 <- var1*var1 df1 <- data.f
好的,我有一个自定义数字键盘,可以在标签(numberField)中将数字显示为 0.00,现在我需要它显示 $0.00。 NSString *digit = sender.currentTitle;
在基于文档的应用程序中,使用 XIB 文件,创建新窗口时其行为是: 根据最后一个事件的位置进行定位和调整大小 window 。 如果最后一个事件窗口仍然可见,则新窗口 窗口应该是级联的,这样它就不会直
我想使用参数进行查询,如下所示: SELECT * FROM MATABLE WHERE MT_ID IN (368134, 181956) 所以我考虑一下 SELECT * FROM MATABLE
我遇到一些性能问题。 我有一个大约有 200 万行的表。 CREATE TABLE [dbo].[M8]( [M8_ID] [int] IDENTITY(1,1) NOT NULL,
我在 jquery 中的按键功能遇到问题。我不知道为什么按键功能不起作用。我已经使用了正确的 key 代码。在我的函数中有 2 个代码,其中包含 2 个事件键,按一个键表示 (+) 代码 107 和(
我想显示音频波形,我得到了此代码,它需要.raw音频输入并显示音频波形,但是当我放入.3gp,.mp3音频时,我得到白噪声,有人可以帮助我如何使其按需与.3gp一起使用使用.3gp音频运行它。 Inp
我无法让 stristr 函数返回真值,我相信这是因为我的搜索中有一个 $ 字符。 当我这样做时: var_dump($nopricecart); 完整的 $nopricecart 值是 $0 ,我得
如果我有这样的循环: for(int i=0;i O(n) 次。所以do some执行了O(n)次。如果做某事是线性时间,那么代码片段的复杂度是O(n^2)。 关于algorithm - 带 If 语
我是一名优秀的程序员,十分优秀!