From 48e47b5212ace2dcb83379d3502b11f64ef4204d Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Sun, 14 Oct 2018 22:26:35 +0800
Subject: [PATCH] Add beep source code and update led.h

---
 modules/c/beep.c |  118 +++++++++++++++++++++++++++++++++++++++
 modules/c/beep.h |   38 ++++++++++++
 modules/c/led.h  |    2 
 3 files changed, 157 insertions(+), 1 deletions(-)

diff --git a/modules/c/beep.c b/modules/c/beep.c
new file mode 100644
index 0000000..4108b1f
--- /dev/null
+++ b/modules/c/beep.c
@@ -0,0 +1,118 @@
+/*********************************************************************************
+ *      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 <stdlib.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"
+
+//#define CONFIG_ACTV_BEEP
+
+int main(int argc, char *argv[])
+{
+       	wiringPiSetup();
+
+	while(1)
+	{
+#ifdef CONFIG_ACTV_BEEP
+		turn_active_beep(ON);
+#else
+		turn_passive_beep(ON, BEEP_FREQ);
+#endif
+		sleep(1);
+
+#ifdef CONFIG_ACTV_BEEP
+		turn_active_beep(OFF);
+#else
+		turn_passive_beep(OFF, BEEP_FREQ);
+#endif
+		sleep(1);
+	}
+
+        return 0;
+}
+
+
+int turn_passive_beep(int cmd, int freq)
+{
+       	int range;
+
+	if(freq<2000 || freq>2500)
+	{
+		printf("Beep set invalid PWM frequency!\n");
+		return -1;
+	}
+
+	if(OFF == cmd)
+	{
+	       	pwmWrite(PWM_PIN, 0);
+	       	pinMode(PWM_PIN, INPUT) ;
+	}
+	else
+	{
+		/* 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;
+}
+
+
diff --git a/modules/c/beep.h b/modules/c/beep.h
new file mode 100644
index 0000000..68c4751
--- /dev/null
+++ b/modules/c/beep.h
@@ -0,0 +1,38 @@
+/*********************************************************************************
+ *      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 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);
+
+#endif
+
diff --git a/modules/c/led.h b/modules/c/led.h
index 582d845..8e3b4fa 100644
--- a/modules/c/led.h
+++ b/modules/c/led.h
@@ -2,7 +2,7 @@
  *      Copyright:  (C) 2018 LingYun IoT System Studio
  *                  All rights reserved.
  *
- *       Filename:  led.c
+ *       Filename:  led.h
  *    Description:  This file is used to control RGB 3-colours LED
  *
  *     pi@raspberrypi:~ $ gpio readall show BCM and wPi pinmap  

--
Gitblit v1.9.1