-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from robotpy/hal-sim-value-changed
Add registerSimValue callbacks
- Loading branch information
Showing
4 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
#pragma once | ||
|
||
#include <hal/SimDevice.h> | ||
|
||
class SimValueCB { | ||
public: | ||
|
||
using FnType = std::function<void(const char *, HAL_SimValueHandle, HAL_SimValueDirection, HAL_Value)>; | ||
|
||
SimValueCB(FnType fn, std::function<void(int32_t)> cancel) : | ||
m_fn(fn), | ||
m_cancel(cancel) | ||
{} | ||
|
||
void SetUID(int32_t uid) { | ||
m_uid = uid; | ||
} | ||
|
||
~SimValueCB() { | ||
Cancel(); | ||
} | ||
|
||
void Cancel() { | ||
if (m_valid) { | ||
m_cancel(m_uid); | ||
m_valid = false; | ||
} | ||
} | ||
|
||
FnType m_fn; | ||
|
||
private: | ||
bool m_valid = true; | ||
int32_t m_uid; | ||
std::function<void(int32_t)> m_cancel; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,41 @@ | ||
import hal | ||
import hal.simulation | ||
|
||
import typing | ||
|
||
def test_hal_simulation(): | ||
pass | ||
|
||
def test_value_changed_callback(): | ||
|
||
recv: typing.Optional[typing.Tuple[bool, str, int]] = None | ||
|
||
def created_cb( | ||
name: str, handle: int, direction: hal.SimValueDirection, value: hal.Value | ||
): | ||
nonlocal recv | ||
recv = (True, name, value.value) | ||
|
||
def cb(name: str, handle: int, direction: hal.SimValueDirection, value: hal.Value): | ||
nonlocal recv | ||
recv = (False, name, value.value) | ||
|
||
dev = hal.SimDevice("simd") | ||
|
||
# Must keep the returned value alive or the callback will be unregistered | ||
devunused = hal.simulation.registerSimValueCreatedCallback(dev, created_cb, True) | ||
assert recv is None | ||
|
||
val = dev.createInt("answer", 0, 42) | ||
|
||
assert recv == (True, "answer", 42) | ||
recv = None | ||
|
||
# Must keep the returned value alive or the callback will be unregistered | ||
unused = hal.simulation.registerSimValueChangedCallback(val, cb, True) | ||
|
||
assert recv == (False, "answer", 42) | ||
recv = None | ||
|
||
val.set(84) | ||
|
||
assert recv == (False, "answer", 84) | ||
recv = None |