-
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.
- Fixes #90
- Loading branch information
Showing
4 changed files
with
119 additions
and
4 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,32 @@ | ||
import hal | ||
import hal.simulation | ||
|
||
import typing | ||
|
||
def test_hal_simulation(): | ||
pass | ||
|
||
def test_value_changed_callback(): | ||
|
||
recv_name: typing.Optional[str] = None | ||
recv_value: typing.Optional[hal.Value] = None | ||
|
||
def cb(name: str, handle: int, direction: hal.SimValueDirection, value: hal.Value): | ||
nonlocal recv_name, recv_value | ||
recv_name = name | ||
recv_value = value | ||
print(name, value) | ||
|
||
dev = hal.SimDevice("simd") | ||
val = dev.createInt("answer", 0, 42) | ||
|
||
# Must keep the returned value alive or the callback will be unregistered | ||
unused = hal.simulation.registerSimValueChangedCallback(val, cb, True) | ||
|
||
assert recv_name == "answer" | ||
assert recv_value is not None | ||
assert recv_value.value == 42 | ||
|
||
val.set(84) | ||
|
||
assert recv_name == "answer" | ||
assert recv_value is not None | ||
assert recv_value.value == 84 |