From 8da36d4f82336087de47f5eadcf2373f4843e292 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 24 Jun 2019 20:30:41 +0800
Subject: [PATCH] update mqttd program, add relay and beep code

---
 mqttd/modules/sht20.h   |    0 
 mqttd/modules/beep.h    |   44 +++++++
 mqttd/modules/ds18b20.c |    0 
 mqttd/modules/led.h     |    0 
 mqttd/modules/led.c     |    4 
 mqttd/modules/relay.h   |   29 ++++
 mqttd/modules/beep.c    |  202 +++++++++++++++++++++++++++++++++
 mqttd/modules/ds18b20.h |    0 
 mqttd/modules/sht20.c   |    0 
 mqttd/modules/relay.c   |   36 ++++++
 10 files changed, 313 insertions(+), 2 deletions(-)

diff --git a/mqttd/modules/beep.c b/mqttd/modules/beep.c
new file mode 100644
index 0000000..6f4816d
--- /dev/null
+++ b/mqttd/modules/beep.c
@@ -0,0 +1,202 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2018 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  led.c
+ *    Description:  This file is used to control Passive buzzer or Active buzzer
+ *
+ *     pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap  
+ *
+ *                  VCC ---- 5V/3.3V
+ *                  GND ---- GND
+ *                  I/O ---- GPIO.18  ---- GPIO.1    
+ *                           BCM           wPi 
+ *                 
+ *        Version:  1.0.0(2018/10/14)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "2018/10/14 12:13:26"
+ *                 
+ ********************************************************************************/
+
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <string.h>
+#include <time.h>
+#include <errno.h>
+
+#include <wiringPi.h>
+#include "beep.h"
+
+/*+-----------------------------+
+ *|  Turn buzzer on or off API  |
+ *+-----------------------------+*/
+
+int turn_passive_beep(int cmd, int freq)
+{
+    int range;
+
+	if(OFF == cmd)
+	{
+        pwmWrite(PWM_PIN, 0);
+        pinMode(PWM_PIN, INPUT) ;
+	}
+	else
+	{
+
+		if(freq<2000 || freq>5000)
+        {
+            printf("Beep set invalid PWM frequency[%d]!\n", freq);
+            return -1;
+        }
+
+		/* Set GPIO as PWM output mode */
+        pinMode(PWM_PIN, PWM_OUTPUT) ;
+
+		/* Set PWM mode as ms mode */
+        pwmSetMode(PWM_MODE_MS);
+
+		/* Set PWM clock: 19.2MHz/32=600KHz */
+        pwmSetClock(32);
+
+		/* Set PWM frequency */
+		range=600000/freq;
+		pwmSetRange(range);
+
+		/* Set PWM duty 50% */
+		pwmWrite(PWM_PIN, range/2);
+	}
+}
+
+
+/* Turn ON/OFF buzzer, Active Buzzer can not set frequency */
+int turn_active_beep(int cmd)
+{ 
+    /* Active buzzer VCC connect to:
+       5V: Both high and low level will be on 
+       3.3V: Low level be on and High level be off 
+	   So we use INPUT or OUTPUT to control the Beeper
+	*/
+	if(OFF == cmd)
+	{
+        pinMode(PWM_PIN, INPUT);
+	}
+	else
+	{
+        pinMode(PWM_PIN, OUTPUT);
+		digitalWrite(PWM_PIN, LOW);
+	}
+
+	return 0;
+}
+
+
+/*+---------------------------------+
+ *|  Play Tone OD,RE,MI....XI,DO1   |
+ *+---------------------------------+*/
+
+enum
+{
+	UNUSED=0,
+	DO,
+	RE,
+	MI,
+	FA,
+	SO,
+	LA,
+	XI,
+	DO1,
+	RI1,
+	TONE_MAX,
+};
+
+#define msleep(x) usleep( 1000*(x) )
+
+static int tone_freq[TONE_MAX]={0, 2093, 2349, 2637, 2794, 3136, 3520, 3952, 4186, 4698 };
+//static int tone_freq[TONE_MAX]={0, 2000, 2130, 2250, 2360, 2450, 2530, 2620, 2700, 2780};
+
+static inline void play_tone(int tone, int delay)
+{
+	if(tone<DO || tone>RI1)
+		return ;
+
+       	turn_passive_beep(ON, tone_freq[tone]);
+	msleep(delay);
+       	turn_passive_beep(OFF, 0);
+}
+
+void play_tone_freq(void)
+{
+	int            i;
+
+	for(i=DO; i<TONE_MAX; i++)
+	{
+		play_tone( i, 500 );
+		msleep(500);
+	}
+}
+
+
+/*+------------------------------+
+ *|  Play song "Little Start"    |
+ *+------------------------------+*/
+
+typedef struct tone_s
+{
+	int     tone;
+	int     delay_ms;   
+
+} tone_t;
+
+#define DEF_DELAY      350 
+static tone_t little_start_notation[]=
+{
+	{DO, DEF_DELAY},
+	{DO, DEF_DELAY},
+	{SO, DEF_DELAY},
+	{SO, DEF_DELAY},
+	{LA, DEF_DELAY},
+	{LA, DEF_DELAY},
+	{SO, DEF_DELAY*2},
+
+	{FA, DEF_DELAY},
+	{FA, DEF_DELAY},
+	{MI, DEF_DELAY},
+	{MI, DEF_DELAY},
+	{RE, DEF_DELAY},
+	{RE, DEF_DELAY},
+	{DO, DEF_DELAY*2},
+
+
+	{SO, DEF_DELAY},
+	{SO, DEF_DELAY},
+	{FA, DEF_DELAY},
+	{FA, DEF_DELAY},
+	{MI, DEF_DELAY},
+	{MI, DEF_DELAY},
+	{RE, DEF_DELAY*2},
+
+
+	{SO, DEF_DELAY},
+	{SO, DEF_DELAY},
+	{FA, DEF_DELAY},
+	{FA, DEF_DELAY},
+	{MI, DEF_DELAY},
+	{MI, DEF_DELAY},
+	{RE, DEF_DELAY*2},
+};
+
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
+void play_little_star(void)
+{
+	int        i;
+
+	for(i=0; i<ARRAY_SIZE(little_start_notation); i++)
+	{
+		play_tone(little_start_notation[i].tone, little_start_notation[i].delay_ms);
+		msleep(30);
+	}
+}
+
+
diff --git a/mqttd/modules/beep.h b/mqttd/modules/beep.h
new file mode 100644
index 0000000..3704a8c
--- /dev/null
+++ b/mqttd/modules/beep.h
@@ -0,0 +1,44 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2018 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  beep.h
+ *    Description:  This file is used to control Passive buzzer or Active buzzer
+ *
+ *     pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap  
+ *
+ *                VCC ---- 5V/3.3V
+ *                GND ---- GND
+ *                I/O ---- GPIO.18  ---- GPIO.1 
+ *                         BCM           wPi
+ *                 
+ *        Version:  1.0.0(2018/10/14)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "2018/10/14 12:13:26"
+ *                 
+ ********************************************************************************/
+
+#ifndef __BEEP_H
+#define __BEEP_H
+
+#define OFF           0
+#define ON            1
+
+//#define CONFIG_ACTV_BEEP
+
+#define BEEP_FREQ     2400
+
+/* Use Pin12 == GPIO18(BCM) == GPIO.1(wPi) */
+#define PWM_PIN       1
+
+int turn_passive_beep(int cmd, int freq);
+
+int turn_active_beep(int cmd);
+
+/*  Only passive buzzer can play tone */
+#ifndef CONFIG_ACTV_BEEP 
+void play_little_star(void);
+#endif
+
+#endif
+
diff --git a/mqttd/ds18b20.c b/mqttd/modules/ds18b20.c
similarity index 100%
rename from mqttd/ds18b20.c
rename to mqttd/modules/ds18b20.c
diff --git a/mqttd/ds18b20.h b/mqttd/modules/ds18b20.h
similarity index 100%
rename from mqttd/ds18b20.h
rename to mqttd/modules/ds18b20.h
diff --git a/mqttd/led.c b/mqttd/modules/led.c
similarity index 98%
rename from mqttd/led.c
rename to mqttd/modules/led.c
index 23a4181..c51b637 100644
--- a/mqttd/led.c
+++ b/mqttd/modules/led.c
@@ -33,8 +33,8 @@
 
 void init_led(void)
 {
-	int         i;
-
+	int         i; 
+    
     wiringPiSetup();
 
 	for(i=0; i<LED_MAX; i++)
diff --git a/mqttd/led.h b/mqttd/modules/led.h
similarity index 100%
rename from mqttd/led.h
rename to mqttd/modules/led.h
diff --git a/mqttd/modules/relay.c b/mqttd/modules/relay.c
new file mode 100644
index 0000000..8676837
--- /dev/null
+++ b/mqttd/modules/relay.c
@@ -0,0 +1,36 @@
+/*********************************************************************************
+ *      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 <wiringPi.h>
+#include "relay.h"
+
+void relay_init(void)
+{
+    wiringPiSetup();
+    pinMode(RELAY_PIN, OUTPUT);
+}
+
+
+void turn_relay(int cmd)
+{
+    if( OFF == cmd )
+    {
+        digitalWrite ( RELAY_PIN, HIGH );
+    }
+    else
+    {
+        digitalWrite ( RELAY_PIN, LOW );
+    }
+}
+
diff --git a/mqttd/modules/relay.h b/mqttd/modules/relay.h
new file mode 100644
index 0000000..af47779
--- /dev/null
+++ b/mqttd/modules/relay.h
@@ -0,0 +1,29 @@
+/********************************************************************************
+ *      Copyright:  (C) 2019 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  relay.h
+ *    Description:  This head file 
+ *
+ *        Version:  1.0.0(30/01/19)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "30/01/19 08:49:31"
+ *                 
+ ********************************************************************************/
+
+#ifndef  _RELAY_H_
+#define  _RELAY_H_
+
+#ifndef ON
+#define ON                  1
+#define OFF                 0
+#endif
+
+/*   I/O Pin connected to PIN#18, BCM code pin number is 24 and wPi pin number is 5 */
+#define RELAY_PIN           5
+
+void relay_init(void);
+void turn_relay(int cmd);
+
+#endif   /* ----- #ifndef _RELAY_H_  ----- */
+
diff --git a/mqttd/sht20.c b/mqttd/modules/sht20.c
similarity index 100%
rename from mqttd/sht20.c
rename to mqttd/modules/sht20.c
diff --git a/mqttd/sht20.h b/mqttd/modules/sht20.h
similarity index 100%
rename from mqttd/sht20.h
rename to mqttd/modules/sht20.h

--
Gitblit v1.9.1