Skip to content

Commit

Permalink
Mbed: Add timer and internal event queue to AppTask.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz Gniadzik committed Mar 10, 2021
1 parent ab7b57e commit 9f66801
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
88 changes: 85 additions & 3 deletions examples/lock-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@
*/

#include "AppTask.h"

#include <support/logging/CHIPLogging.h>

#include "mbed.h"

static mbed::Ticker sFunctionTimer;

// TODO: change EventQueue default event size
static EventQueue sAppEventQueue;

AppTask AppTask::sAppTask;

int AppTask::Init()
{
// TODO: Add led, buttons, hardware initialization.
mFunctionTimerActive = false;

// Timer initialization
// TODO: timer period to FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
StartTimer(50);

return 0;
}

Expand All @@ -37,6 +50,75 @@ int AppTask::StartApp()
return ret;
}

// TODO: Add event msg queue handling
while (true)
{
sAppEventQueue.dispatch(100);
}

return 0;
}
}

void AppTask::CancelTimer()
{
if (mFunctionTimerActive)
{
sFunctionTimer.detach();
mFunctionTimerActive = false;
}
}

void AppTask::StartTimer(uint32_t aTimeoutInMs)
{
auto chronoTimeoutMs = std::chrono::duration<uint32_t, std::milli>(aTimeoutInMs);

if (mFunctionTimerActive)
{
ChipLogError(NotSpecified, "App timer already started!");
CancelTimer();
}

sFunctionTimer.attach(&AppTask::TimerEventHandler, chronoTimeoutMs);
mFunctionTimerActive = true;
}

void AppTask::PostEvent(AppEvent * aEvent)
{
auto handle = sAppEventQueue.call([event = *aEvent, this] { DispatchEvent(&event); });
if (!handle)
{
ChipLogError(NotSpecified, "Failed to post event to app task event queue: Not enough memory");
}
}

void AppTask::DispatchEvent(const AppEvent * aEvent)
{
if (aEvent->Handler)
{
aEvent->Handler(const_cast<AppEvent *>(aEvent));
}
else
{
ChipLogError(NotSpecified, "Event received with no handler. Dropping event.");
}
}

// static
void AppTask::TimerEventHandler()
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
event.TimerEvent.Context = nullptr;
event.Handler = FunctionTimerEventHandler;
sAppTask.PostEvent(&event);
}

// static
void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Below code is only for debug purpose!
static DigitalOut led(LED1);
if (aEvent->Type != AppEvent::kEventType_Timer)
return;

led = !led;
}
58 changes: 58 additions & 0 deletions examples/lock-app/mbed/main/include/AppEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* 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.
*/

#pragma once

#include <cstdint>

struct AppEvent;
typedef void (*EventHandler)(AppEvent *);

struct AppEvent
{
enum AppEventTypes
{
kEventType_Button = 0,
kEventType_Timer,
kEventType_Lock,
kEventType_Install,
};

uint16_t Type;

union
{
struct
{
uint8_t ButtonIdx;
uint8_t Action;
} ButtonEvent;
struct
{
void * Context;
} TimerEvent;
struct
{
uint8_t Action;
int32_t Actor;
} LockEvent;
};

EventHandler Handler;
};
17 changes: 17 additions & 0 deletions examples/lock-app/mbed/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,33 @@
* limitations under the License.
*/

#pragma once

#include "AppEvent.h"

class AppTask
{
public:
int StartApp();

void PostEvent(AppEvent * aEvent);

private:
friend AppTask & GetAppTask(void);

int Init();

void CancelTimer(void);

void DispatchEvent(const AppEvent * event);

static void FunctionTimerEventHandler(AppEvent * aEvent);

static void TimerEventHandler();

void StartTimer(uint32_t aTimeoutInMs);

bool mFunctionTimerActive;
static AppTask sAppTask;
};

Expand Down

0 comments on commit 9f66801

Please sign in to comment.