/*********************************************************************************
|
* Copyright: (C) 2019 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: relay.c
|
* Description: This file
|
*
|
* Version: 1.0.0(30/01/19)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "30/01/19 08:44:30"
|
*
|
********************************************************************************/
|
|
#include <stdio.h>
|
#include <unistd.h>
|
#include <libgen.h>
|
#include <string.h>
|
|
#include <gpiod.h>
|
|
#define ON 1
|
#define OFF 0
|
|
/* Relay #Pin(29) BCM(5) */
|
#define relay_pin 5
|
|
int turn_relay(int cmd);
|
|
int main (int argc, char **argv)
|
{
|
if( argc != 2 )
|
{
|
printf("Usage: %s [on/off]\n", basename(argv[0]));
|
return -1;
|
}
|
|
if( !strstr(argv[1], "on") || !strstr(argv[1], "on") )
|
{
|
turn_relay(ON);
|
}
|
else if( !strstr(argv[1], "off") || !strstr(argv[1], "off") )
|
{
|
turn_relay(OFF);
|
}
|
|
return 0;
|
}
|
|
static void gpiod_ctxless_cb(void *data)
|
{
|
sleep(3);
|
}
|
|
int turn_relay(int cmd)
|
{
|
if( OFF == cmd )
|
gpiod_ctxless_set_value("gpiochip0", relay_pin, 0, false, "relay", gpiod_ctxless_cb, NULL);
|
else
|
gpiod_ctxless_set_value("gpiochip0", relay_pin, 1, false, "relay", gpiod_ctxless_cb, NULL);
|
}
|