Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a linux contact sensor #25161

Merged
merged 12 commits into from
Feb 21, 2023
30 changes: 30 additions & 0 deletions examples/common/imgui_ui/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,33 @@ static_library("light") {

public_configs = [ "${chip_root}/src:includes" ]
}

static_library("boolean_state") {
sources = [
"boolean_state.cpp",
"boolean_state.h",
]

deps = [
":windows",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/third_party/imgui",
]

public_configs = [ "${chip_root}/src:includes" ]
}

static_library("occupancy_sensing") {
sources = [
"occupancy_sensing.cpp",
"occupancy_sensing.h",
]

deps = [
":windows",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/third_party/imgui",
]

public_configs = [ "${chip_root}/src:includes" ]
}
60 changes: 60 additions & 0 deletions examples/common/imgui_ui/windows/boolean_state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "boolean_state.h"

#include <imgui.h>

#include <math.h>

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-enums.h>

namespace example {
namespace Ui {
namespace Windows {

void BooleanState::UpdateState()
{
if (mTargetState.HasValue())
{
chip::app::Clusters::BooleanState::Attributes::StateValue::Set(mEndpointId, mTargetState.Value());
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
mTargetState.ClearValue();
}

chip::app::Clusters::BooleanState::Attributes::StateValue::Get(mEndpointId, &mState);
}

void BooleanState::Render()
{
ImGui::Begin(mTitle.c_str());
ImGui::Text("On Endpoint %d", mEndpointId);

bool uiState = mState;
ImGui::Checkbox("State Value", &uiState);

if (uiState != mState)
{
// toggle value on the next 'UpdateState' call
mTargetState.SetValue(uiState);
}

ImGui::End();
}

} // namespace Windows
} // namespace Ui
} // namespace example
52 changes: 52 additions & 0 deletions examples/common/imgui_ui/windows/boolean_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include "window.h"

#include <stdint.h>
#include <string>

#include <app/data-model/Nullable.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/Optional.h>

#include <app-common/zap-generated/cluster-enums.h>

namespace example {
namespace Ui {
namespace Windows {

class BooleanState : public Window
{
public:
BooleanState(chip::EndpointId endpointId, const char * title) : mEndpointId(endpointId), mTitle(title) {}

void UpdateState() override;
void Render() override;

private:
const chip::EndpointId mEndpointId;
const std::string mTitle;

bool mState = false;
chip::Optional<bool> mTargetState;
};

} // namespace Windows
} // namespace Ui
} // namespace example
64 changes: 64 additions & 0 deletions examples/common/imgui_ui/windows/occupancy_sensing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "occupancy_sensing.h"

#include <imgui.h>

#include <math.h>

#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/cluster-enums.h>

namespace example {
namespace Ui {
namespace Windows {

using chip::app::Clusters::OccupancySensing::OccupancyBitmap;

void OccupancySensing::UpdateState()
{
if (mTargetOccupancy.HasValue())
{
chip::app::Clusters::OccupancySensing::Attributes::Occupancy::Set(mEndpointId, mTargetOccupancy.Value());
mTargetOccupancy.ClearValue();
}

chip::app::Clusters::OccupancySensing::Attributes::Occupancy::Get(mEndpointId, &mOccupancy);
}

void OccupancySensing::Render()
{
ImGui::Begin(mTitle.c_str());
ImGui::Text("On Endpoint %d", mEndpointId);

bool occupied = mOccupancy.Has(OccupancyBitmap::kOccupied);
bool uiState = occupied;
ImGui::Checkbox("Occupancy Value", &uiState);

if (uiState != occupied)
{
// toggle value on the next 'UpdateState' call
// Occupancy is just a single bit, update it as such
mTargetOccupancy.SetValue(uiState ? BitMask(OccupancyBitmap::kOccupied) : BitMask());
}

ImGui::End();
}

} // namespace Windows
} // namespace Ui
} // namespace example
54 changes: 54 additions & 0 deletions examples/common/imgui_ui/windows/occupancy_sensing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include "window.h"

#include <stdint.h>
#include <string>

#include <app/data-model/Nullable.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/Optional.h>

#include <app-common/zap-generated/cluster-enums.h>

namespace example {
namespace Ui {
namespace Windows {

class OccupancySensing : public Window
{
public:
OccupancySensing(chip::EndpointId endpointId, const char * title) : mEndpointId(endpointId), mTitle(title) {}

void UpdateState() override;
void Render() override;

private:
const chip::EndpointId mEndpointId;
const std::string mTitle;

using BitMask = chip::BitMask<chip::app::Clusters::OccupancySensing::OccupancyBitmap>;

BitMask mOccupancy;
chip::Optional<BitMask> mTargetOccupancy;
};

} // namespace Windows
} // namespace Ui
} // namespace example
25 changes: 25 additions & 0 deletions examples/contact-sensor-app/linux/.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")

# The location of the build configuration file.
buildconfig = "${build_root}/config/BUILDCONFIG.gn"

# CHIP uses angle bracket includes.
check_system_includes = true

default_args = {
import("//args.gni")
}
64 changes: 64 additions & 0 deletions examples/contact-sensor-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/chip.gni")

import("${chip_root}/build/chip/tools.gni")
import("${chip_root}/src/app/common_flags.gni")
import("${chip_root}/third_party/imgui/imgui.gni")

assert(chip_build_tools)

config("includes") {
include_dirs = [
".",
"include",
]
}

executable("contact-sensor-app") {
sources = [
"include/CHIPProjectAppConfig.h",
"main.cpp",
]

deps = [
"${chip_root}/examples/contact-sensor-app/contact-sensor-common",
"${chip_root}/examples/platform/linux:app-main",
"${chip_root}/src/lib",
]

if (chip_examples_enable_imgui_ui) {
deps += [
"${chip_root}/examples/common/imgui_ui",
"${chip_root}/examples/common/imgui_ui/windows:boolean_state",
"${chip_root}/examples/common/imgui_ui/windows:occupancy_sensing",
"${chip_root}/examples/common/imgui_ui/windows:qrcode",
]
}

include_dirs = [ "include" ]

cflags = [ "-Wconversion" ]

output_dir = root_out_dir
}

group("linux") {
deps = [ ":contact-sensor-app" ]
}

group("default") {
deps = [ ":linux" ]
}
27 changes: 27 additions & 0 deletions examples/contact-sensor-app/linux/args.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# CHIPProjectConfig.h

import("//build_overrides/chip.gni")

import("${chip_root}/config/standalone/args.gni")

chip_device_project_config_include = "<CHIPProjectAppConfig.h>"
chip_project_config_include = "<CHIPProjectAppConfig.h>"
chip_system_project_config_include = "<SystemProjectConfig.h>"

chip_project_config_include_dirs =
[ "${chip_root}/examples/contact-sensor-app/linux/include" ]
chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]
1 change: 1 addition & 0 deletions examples/contact-sensor-app/linux/build_overrides
Loading