Program for testing a PIR module on the Intel Edison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Author: Clark Scheff <clark@scheffsblend.com> | |
* Copyright (c) 2015 Scheff's Blend | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining | |
* a copy of this software and associated documentation files (the | |
* "Software"), to deal in the Software without restriction, including | |
* without limitation the rights to use, copy, modify, merge, publish, | |
* distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to | |
* the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#include "jhd1313m1.h" | |
#include "buzzer.h" | |
#include "mraa.hpp" | |
#include <pthread.h> | |
#include <signal.h> | |
#include <unistd.h> | |
upm::Buzzer *g_buzzer = NULL; | |
upm::Jhd1313m1 *g_lcd = NULL; | |
mraa::Gpio *g_motionSensor = NULL; | |
bool g_motionDetected = false; | |
void* alarm(void* args) { | |
while (g_motionDetected) { | |
g_buzzer->playSound(LA, 125000); | |
g_buzzer->playSound(SI, 125000); | |
} | |
g_buzzer->playSound(FA, 0); | |
return NULL; | |
} | |
void motionInterrupt(void* args) { | |
pthread_t tid; | |
int sensorValue = g_motionSensor->read(); | |
if (sensorValue && !g_motionDetected) { | |
g_motionDetected = true; | |
g_lcd->setColor(255, 0, 0); | |
pthread_create(&tid, NULL, &alarm, NULL); | |
} else if (!sensorValue) { | |
g_motionDetected = false; | |
g_lcd->setColor(0, 0, 255); | |
} | |
} | |
void shutdown(int signo) { | |
delete g_buzzer; | |
delete g_motionSensor; | |
delete g_lcd; | |
exit(0); | |
} | |
int main(int argc, char **argv) { | |
// Setup motion sensor on GPIO 4 and configure the pin to interrupt | |
// on both the rising and falling edge | |
g_motionSensor = new mraa::Gpio(4, true, false); | |
g_motionSensor->dir(mraa::DIR_IN); | |
g_motionSensor->isr(mraa::EDGE_BOTH, &motionInterrupt, NULL); | |
// Buzzer uses PWM so make sure this is on a pin that suports PWM | |
// and don't forget to check that damn PWM swizzler shiznit. | |
g_buzzer = new upm::Buzzer(3); | |
// 0x62 RGB_ADDRESS, 0x3E LCD_ADDRESS | |
g_lcd = new upm::Jhd1313m1(0, 0x3E, 0x62); | |
g_lcd->setColor(0, 0, 255); | |
g_lcd->setCursor(0, 0); | |
g_lcd->write("Motion detector"); | |
// clean things up nicely if the process is terminated or interrupted | |
signal(SIGINT, shutdown); | |
signal(SIGTERM, shutdown); | |
while (true) { | |
sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment