[sim] Various WebSockets fixes and enhancements (#2952)

This is a breaking change to the WebSockets layer to align it with
recent specification documentation work.

To support this, HAL SimValue changed readonly to a direction enum.
This allows specifying bidirectional in addition to input and output.

The SimValue change is specifically designed to avoid API and ABI breakage.
This is completely transparent in C++; in Java a new callback class was added,
and the old readonly functions have been marked deprecated.

A new SimValue creation function for enums allows specifying double values
for each enum value, not just strings.  This allows mapping enum values to
doubles in the WebSockets layer.

A ":" in the SimDevice name now maps it to different WebSocket types (e.g.
"Accel:Name" becomes type "Accel", device "Name").  The type is hidden
in the GUI.

Other WebSockets changes:
* Implemented match_time and game_data
* Added joystick rumble data
* Added builtin accelerometer support
* SimValue enums are mapped to string and double value on WS interface
* Added WebSockets protocol specification
* Added READMEs
This commit is contained in:
Peter Johnson
2020-12-23 15:54:11 -08:00
committed by GitHub
parent 699bbe21a4
commit 10b396b4c2
38 changed files with 1264 additions and 187 deletions

View File

@@ -33,7 +33,7 @@ class SimValueSource : public glass::DataSource {
private:
static void CallbackFunc(const char*, void* param, HAL_SimValueHandle,
HAL_Bool, const HAL_Value* value) {
int32_t, const HAL_Value* value) {
auto source = static_cast<SimValueSource*>(param);
if (value->type == HAL_BOOLEAN) {
source->SetValue(value->data.v_boolean);
@@ -73,7 +73,7 @@ void SimDevicesModel::Update() {
HALSIM_EnumerateSimValues(
handle, &data,
[](const char* name, void* dataV, HAL_SimValueHandle handle,
HAL_Bool readonly, const HAL_Value* value) {
int32_t direction, const HAL_Value* value) {
auto data = static_cast<Data*>(dataV);
auto& source = data->self->m_sources[handle];
if (!source) {
@@ -85,7 +85,7 @@ void SimDevicesModel::Update() {
}
static void DisplaySimValue(const char* name, void* data,
HAL_SimValueHandle handle, HAL_Bool readonly,
HAL_SimValueHandle handle, int32_t direction,
const HAL_Value* value) {
auto model = static_cast<SimDevicesModel*>(data);
@@ -94,14 +94,16 @@ static void DisplaySimValue(const char* name, void* data,
switch (value->type) {
case HAL_BOOLEAN: {
bool v = value->data.v_boolean;
if (glass::DeviceBoolean(name, readonly, &v, model->GetSource(handle))) {
if (glass::DeviceBoolean(name, direction == HAL_SimValueOutput, &v,
model->GetSource(handle))) {
valueCopy.data.v_boolean = v ? 1 : 0;
HAL_SetSimValue(handle, valueCopy);
}
break;
}
case HAL_DOUBLE:
if (glass::DeviceDouble(name, readonly, &valueCopy.data.v_double,
if (glass::DeviceDouble(name, direction == HAL_SimValueOutput,
&valueCopy.data.v_double,
model->GetSource(handle))) {
HAL_SetSimValue(handle, valueCopy);
}
@@ -109,21 +111,22 @@ static void DisplaySimValue(const char* name, void* data,
case HAL_ENUM: {
int32_t numOptions = 0;
const char** options = HALSIM_GetSimValueEnumOptions(handle, &numOptions);
if (glass::DeviceEnum(name, readonly, &valueCopy.data.v_enum, options,
numOptions, model->GetSource(handle))) {
if (glass::DeviceEnum(name, direction == HAL_SimValueOutput,
&valueCopy.data.v_enum, options, numOptions,
model->GetSource(handle))) {
HAL_SetSimValue(handle, valueCopy);
}
break;
}
case HAL_INT:
if (glass::DeviceInt(name, readonly, &valueCopy.data.v_int,
model->GetSource(handle))) {
if (glass::DeviceInt(name, direction == HAL_SimValueOutput,
&valueCopy.data.v_int, model->GetSource(handle))) {
HAL_SetSimValue(handle, valueCopy);
}
break;
case HAL_LONG:
if (glass::DeviceLong(name, readonly, &valueCopy.data.v_long,
model->GetSource(handle))) {
if (glass::DeviceLong(name, direction == HAL_SimValueOutput,
&valueCopy.data.v_long, model->GetSource(handle))) {
HAL_SetSimValue(handle, valueCopy);
}
break;
@@ -134,7 +137,10 @@ static void DisplaySimValue(const char* name, void* data,
static void DisplaySimDevice(const char* name, void* data,
HAL_SimDeviceHandle handle) {
if (glass::BeginDevice(name)) {
// only show "Foo" portion of "Accel:Foo"
auto [type, id] = wpi::StringRef{name}.split(':');
if (id.empty()) id = type;
if (glass::BeginDevice(id.data())) {
HALSIM_EnumerateSimValues(handle, data, DisplaySimValue);
glass::EndDevice();
}