Skip to content

Commit

Permalink
Add lighting-app
Browse files Browse the repository at this point in the history
  • Loading branch information
hanksuu committed Jul 29, 2021
1 parent 7f40f45 commit 1487442
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/lighting-app/ambd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.vscode

/build/
/sdkconfig
/sdkconfig.old
92 changes: 92 additions & 0 deletions examples/lighting-app/ambd/main/CHIPDeviceManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
*
* 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.
*/

/**
* @file
* This file implements the CHIP Device Interface that is used by
* applications to interact with the CHIP stack
*
*/

#include <stdlib.h>

#include "CHIPDeviceManager.h"
#include <app/util/basic-types.h>
#include <support/CHIPMem.h>
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>
#include "Globals.h"
#include "LEDWidget.h"
#include <app/common/gen/attribute-id.h>
#include <app/common/gen/attribute-type.h>
#include <app/common/gen/cluster-id.h>

using namespace ::chip;

namespace chip {

namespace DeviceManager {

using namespace ::chip::DeviceLayer;

CHIP_ERROR CHIPDeviceManager::Init(void)
{
CHIP_ERROR err;

err = PlatformMgr().InitChipStack();
SuccessOrExit(err);

err = Platform::MemoryInit();
SuccessOrExit(err);

// // Start a task to run the CHIP Device event loop.
err = PlatformMgr().StartEventLoopTask();
if (err != CHIP_NO_ERROR)
{
printf("StartEventLoopTask() - ERROR!\r\n");
}
else
{
printf("StartEventLoopTask() - OK\r\n");
}

exit:
return err;
}
} // namespace DeviceManager
} // namespace chip

void emberAfPostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, uint8_t mask,
uint16_t manufacturerCode, uint8_t type, uint16_t size, uint8_t * value)
{
ChipLogProgress(Zcl, "Cluster callback: %" PRIx32, clusterId);
if (clusterId == ZCL_ON_OFF_CLUSTER_ID)
{
if (attributeId != ZCL_ON_OFF_ATTRIBUTE_ID)
{
ChipLogProgress(Zcl, "Unknown attribute ID: %" PRIx32, attributeId);
return;
}

statusLED1.Set(*value);
}
else
{
ChipLogProgress(Zcl, "Unknown cluster ID: %" PRIx32, clusterId);
return;
}
}
20 changes: 20 additions & 0 deletions examples/lighting-app/ambd/main/Globals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
*
* 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.
*/

#include "Globals.h"

LEDWidget statusLED1;
61 changes: 61 additions & 0 deletions examples/lighting-app/ambd/main/LEDWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*
* Copyright (c) 2018 Nest Labs, Inc.
* All rights reserved.
*
* 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.
*/

/**
* @file LEDWidget.cpp
*
* Implements an LED Widget controller that is usually tied to a GPIO
* It also updates the display widget if it's enabled
*/

#include "LEDWidget.h"

gpio_t gpio_led;

void LEDWidget::Init(PinName gpioNum)
{

mGPIONum = gpioNum;
mState = false;

if (gpioNum != (PinName)NC)
{
// Init LED control pin
gpio_init(&gpio_led, gpioNum);
gpio_dir(&gpio_led, PIN_OUTPUT); // Direction: Output
gpio_mode(&gpio_led, PullNone); // No pull
gpio_write(&gpio_led, mState);
}
}

void LEDWidget::Set(bool state)
{
DoSet(state);
}

void LEDWidget::DoSet(bool state)
{
bool stateChange = (mState != state);
mState = state;

if (stateChange)
{
gpio_write(&gpio_led, state);
}
}

79 changes: 79 additions & 0 deletions examples/lighting-app/ambd/main/chipinterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <platform_stdlib.h>

#include "Globals.h"
#include "LEDWidget.h"
#include "CHIPDeviceManager.h"
//#include "DeviceCallbacks.h"
#include "Server.h"

#include <platform/CHIPDeviceLayer.h>
#include <support/CHIPMem.h>

extern "C"{ void * __dso_handle = 0 ;}

using namespace ::chip;
using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;

namespace
{
void SetupPretendDevices(void)
{
//TODO
}

class AppCallbacks : public AppDelegate
{
public:
void OnReceiveError() override { };
void OnRendezvousStarted() override { };
void OnRendezvousStopped() override {};
void OnPairingWindowOpened() override { };
void OnPairingWindowClosed() override { };
};

} // namespace

#ifdef CONFIG_PLATFORM_8721D
#define STATUS_LED_GPIO_NUM PB_5
#elif defined(CONFIG_PLATFORM_8710C)
#define STATUS_LED_GPIO_NUM PA_20
#else
#define STATUS_LED_GPIO_NUM NC
#endif

extern "C" void ChipTest(void)
{
printf("In ChipTest()\r\n");
CHIP_ERROR err = CHIP_NO_ERROR;


CHIPDeviceManager &deviceMgr = CHIPDeviceManager::GetInstance();


err = deviceMgr.Init();

if (err != CHIP_NO_ERROR)
{
printf("DeviceManagerInit() - ERROR!\r\n");
}
else
{
printf("DeviceManagerInit() - OK\r\n");
}

SetupPretendDevices();

AppCallbacks callbacks;
InitServer(&callbacks);

statusLED1.Init(STATUS_LED_GPIO_NUM);

while(true)
vTaskDelay( pdMS_TO_TICKS(50) );
}

bool lowPowerClusterSleep()
{
return true;
}
73 changes: 73 additions & 0 deletions examples/lighting-app/ambd/main/include/CHIPDeviceManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* 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.
*/

/**
* @file
* This file contains definitions for the CHIP DeviceManager Interface
*
* This object will co-ordinate multiple activities such as
* initialisation, rendezvous, session mgmt and other such
* activities within the CHIP stack. This is a singleton object.
*/

#pragma once

#include <core/CHIPCore.h>
#include <core/CHIPError.h>
#include <platform/CHIPDeviceLayer.h>

#include <support/DLLUtil.h>

#include <stdarg.h>
#include <stdlib.h>

#include "af-types.h"

namespace chip {
namespace DeviceManager {

/**
* @brief
* A common class that drives other components of the CHIP stack
*/
class DLL_EXPORT CHIPDeviceManager
{
public:
CHIPDeviceManager(const CHIPDeviceManager &) = delete;
CHIPDeviceManager(const CHIPDeviceManager &&) = delete;
CHIPDeviceManager & operator=(const CHIPDeviceManager &) = delete;

static CHIPDeviceManager & GetInstance()
{
static CHIPDeviceManager instance;
return instance;
}

/**
* @brief
* Initialise CHIPDeviceManager
*
* @param cb Application's instance of the CHIPDeviceManagerCallbacks for consuming events
*/
CHIP_ERROR Init(void);

private:
CHIPDeviceManager() {}
};

} // namespace DeviceManager
} // namespace chip
22 changes: 22 additions & 0 deletions examples/lighting-app/ambd/main/include/Globals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
*
* 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.
*/

#pragma once

#include "LEDWidget.h"

extern LEDWidget statusLED1;
Loading

0 comments on commit 1487442

Please sign in to comment.