Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
io/DeviceFeedback.cpp
- Committer:
- vwochnik
- Date:
- 2014-11-30
- Revision:
- 67:c360a2b2c948
- Parent:
- 65:a62dbef2f924
- Child:
- 71:063c45e99578
File content as of revision 67:c360a2b2c948:
#include "DeviceFeedback.h"
#include <stdlib.h>
#include <string.h>
#define MSG_BEEP_SUCCESS 1
#define MSG_BEEP_FAILURE 2
#define MSG_CLOSE_RELAY 3
#define MSG_OPEN_RELAY 4
DeviceFeedback::DeviceFeedback(PwmOut speaker) :
_speaker(speaker),
_thread(DeviceFeedback::thread_func, this)
{
}
void DeviceFeedback::beepSuccess()
{
sendMessage(MSG_BEEP_SUCCESS);
}
void DeviceFeedback::beepFailure()
{
sendMessage(MSG_BEEP_FAILURE);
}
void DeviceFeedback::closeRelay()
{
sendMessage(MSG_CLOSE_RELAY);
}
void DeviceFeedback::openRelay()
{
sendMessage(MSG_OPEN_RELAY);
}
void DeviceFeedback::sendMessage(uint8_t msg)
{
uint8_t *msgPtr;
msgPtr = _mail.alloc();
*msgPtr = msg;
_mail.put(msgPtr);
}
void DeviceFeedback::thread()
{
osEvent evt; uint8_t *msg;
bool relayState = false;
while (true) {
if ((evt = _mail.get(1000)).status == osEventMail) {
msg = (uint8_t*)evt.value.p;
switch (*msg) {
case MSG_BEEP_SUCCESS:
for (float i=2000.0; i<10000.0; i+=2000.0) {
_speaker.period(1.0/i);
_speaker = 0.5;
Thread::wait(200);
_speaker = 0.0;
Thread::wait(50);
}
break;
case MSG_BEEP_FAILURE:
for (float i=10000.0; i>2000.0; i-=2000.0) {
_speaker.period(1.0/i);
_speaker = 0.5;
Thread::wait(200);
_speaker = 0.0;
Thread::wait(50);
}
break;
case MSG_CLOSE_RELAY:
if (!relayState) {
relayState = true;
for (float i=2000.0; i<10000.0; i+=100) {
_speaker.period(1.0/i);
_speaker = 0.5;
Thread::wait(20);
}
_speaker = 0.0;
}
break;
case MSG_OPEN_RELAY:
if (relayState) {
relayState = false;
for (float i=10000.0; i>2000.0; i-=100) {
_speaker.period(1.0/i);
_speaker = 0.5;
Thread::wait(20);
}
_speaker = 0.0;
}
break;
}
_mail.free(msg);
}
if (relayState) {
_speaker.period(1.0/10000);
_speaker = 0.5;
Thread::wait(20);
_speaker = 0.0;
}
}
}
void DeviceFeedback::thread_func(void const *arg)
{
DeviceFeedback *that;
that = (DeviceFeedback*)arg;
that->thread();
}
