Combine a variety of coding in one Arduino code
SHS/doorbell/Buzzer.cpp
SHS/doorbell/Buzzer.cpp
#include
"Buzzer.h"
#include
<
avr
/
wdt
.
h
>
uint8_t buzzer_pin
;
#ifdef
ME_PORT_DEFINED
/**
* Alternate Constructor which can call your own function to map the Buzzer to arduino port,
* Buzzer pins are used and initialized here.
* \param[in]
* None
*/
Buzzer
::
Buzzer
()
{
buzzer_pin
=
9
;
}
/**
* Alternate Constructor which can call your own function to map the Buzzer to arduino port,
* If the hardware serial was selected, we will used the hardware serial.
* \param[in]
* port - RJ25 port from PORT_1 to M2
*/
Buzzer
::
Buzzer
(
uint8_t port
)
:
MePort
(
port
)
{
buzzer_pin
=
port
;
}
/**
* Alternate Constructor which can call your own function to map the Buzzer to arduino port,
* you can set any slot for the buzzer device.
* \param[in]
* port - RJ25 port from PORT_1 to M2
* \param[in]
* slot - SLOT1 or SLOT2
*/
Buzzer
::
Buzzer
(
uint8_t port
,
uint8_t slot
)
:
MePort
(
port
)
{
buzzer_pin
=
s2
;
if
(
slot
==
SLOT2
)
{
buzzer_pin
=
s2
;
}
else
{
buzzer_pin
=
s1
;
}
}
#else
// ME_PORT_DEFINED
/**
* Alternate Constructor which can call your own function to map the Buzzer to arduino port,
* \param[in]
* switchPin - arduino port for buzzer detect pin.
*/
Buzzer
::
Buzzer
(
int
pin
)
{
buzzer_pin
=
pin
;
}
#endif
// ME_PORT_DEFINED
/**
* \par Function
* setpin
* \par Description
* Reset the buzzer available pin by its arduino port.
* \param[in]
* pin - arduino port for buzzer detect pin.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void
Buzzer
::
setpin
(
int
pin
)
{
buzzer_pin
=
pin
;
}
/**
* \par Function
* tone
* \par Description
* Playing the tones.
* \param[in]
* pin - Which pin on board that buzzer is connecting to.
* \param[in]
* frequency - The speed of buzzer's tone play.
* \param[in]
* duration - Time of a tone play.
* \par Output
* None
* \Return
* None.
* \par Others
* Frequency (in hertz) and duration (in milliseconds).
*/
void
Buzzer
::
tone
(
int
pin
,
uint16_t frequency
,
uint32_t duration
)
{
buzzer_pin
=
pin
;
int
period
=
1000000L
/
frequency
;
int
pulse
=
period
/
2
;
pinMode
(
buzzer_pin
,
OUTPUT
);
for
(
long
i
=
0
;
i
<
duration
*
1000L
;
i
+=
period
)
{
digitalWrite
(
buzzer_pin
,
HIGH
);
delayMicroseconds
(
pulse
);
digitalWrite
(
buzzer_pin
,
LOW
);
delayMicroseconds
(
pulse
);
wdt_reset
();
}
}
/**
* \par Function
* tone
* \par Description
* Playing the tones.
* \param[in]
* frequency - The speed of buzzer's tone play.
* \param[in]
* duration - Time of a tone play.
* \par Output
* None
* \Return
* None.
* \par Others
* Frequency (in hertz) and duration (in milliseconds).
*/
void
Buzzer
::
tone
(
uint16_t frequency
,
uint32_t duration
)
{
int
period
=
1000000L
/
frequency
;
int
pulse
=
period
/
2
;
pinMode
(
buzzer_pin
,
OUTPUT
);
for
(
long
i
=
0
;
i
<
duration
*
1000L
;
i
+=
period
)
{
digitalWrite
(
buzzer_pin
,
HIGH
);
delayMicroseconds
(
pulse
);
digitalWrite
(
buzzer_pin
,
LOW
);
delayMicroseconds
(
pulse
);
wdt_reset
();
}
}
void
Buzzer
::
_tone
(
float
noteFrequency
,
long
noteDuration
,
int
silentDuration
)
{
// tone(10,261,500);
// delay(500);
if
(
silentDuration
==
0
){
silentDuration
=
1
;}
tone
(
buzzer_pin
,
noteFrequency
,
noteDuration
);
delay
(
noteDuration
);
//milliseconds to microseconds
//noTone(PIN_Buzzer);
delay
(
silentDuration
);
}
void
Buzzer
::
bendTones
(
float
initFrequency
,
float
finalFrequency
,
float
prop
,
long
noteDuration
,
int
silentDuration
){
//Examples:
// bendTones (880, 2093, 1.02, 18, 1);
// bendTones (note_A5, note_C7, 1.02, 18, 0);
if
(
silentDuration
==
0
){
silentDuration
=
1
;}
if
(
initFrequency
<
finalFrequency
)
{
for
(
int
i
=
initFrequency
;
i
<
finalFrequency
;
i
=
i
*
prop
)
{
_tone
(
i
,
noteDuration
,
silentDuration
);
}
}
else
{
for
(
int
i
=
initFrequency
;
i
>
finalFrequency
;
i
=
i
/
prop
)
{
_tone
(
i
,
noteDuration
,
silentDuration
);
}
}
}
/**
* \par Function
* noTone
* \par Description
* Do not playing the tones.
* \param[in]
* pin - Which pin on board that buzzer is connecting to.
* \par Output
* None
* \Return
* None.
* \par Others
* None
*/
void
Buzzer
::
noTone
(
int
pin
)
{
buzzer_pin
=
pin
;
pinMode
(
buzzer_pin
,
OUTPUT
);
digitalWrite
(
buzzer_pin
,
LOW
);
}
/**
* \par Function
* noTone
* \par Description
* Do not playing the tones.
* \param[in]
* None
* \par Output
* None
* \Return
* None.
* \par Others
* None
*/
void
Buzzer
::
noTone
()
{
pinMode
(
buzzer_pin
,
OUTPUT
);
digitalWrite
(
buzzer_pin
,
LOW
);
}
SHS/doorbell/Buzzer.h
/** * \par Copyright (C), 2012-2016, MakeBlock * \class Buzzer * \brief Driver for Me Buzzer module. * @file Buzzer.h * @author MakeBlock * @version V1.0.0 * @date 2015/11/09 * @brief Header for Buzzer.cpp module * * \par Copyright * This software is Copyright (C), 2012-2016, MakeBlock. Use is subject to license \n * conditions. The main licensing options available are GPL V2 or Commercial: \n * * \par Open Source Licensing GPL V2 * This is the appropriate option if you want to share the source code of your \n * application with everyone you distribute it to, and you also want to give them \n * the right to share who uses it. If you wish to use this software under Open \n * Source Licensing, you must contribute all your source code to the open source \n * community in accordance with the GPL Version 2 when your application is \n * distributed. See http://www.gnu.org/copyleft/gpl.html * * \par Description * This file is a drive for Me Buzzer device, The Me Buzzer inherited the * MeSerial class from SoftwareSerial. * * \par Method List: * * 1. void Buzzer::setpin(int pin); * 2. void Buzzer::tone(int pin, uint16_t frequency, uint32_t duration); * 3. void Buzzer::tone(uint16_t frequency, uint32_t duration) * 4. void Buzzer::noTone(int pin); * 5. void Buzzer::noTone(); * * \par History: * <pre> * `<Author>` `<Time>` `<Version>` `<Descr>` * forfish 2015/11/09 1.0.0 Add description * </pre> * * @example BuzzerTest.ino */ #ifndef Buzzer_H #define Buzzer_H #include <stdint.h> #include <stdbool.h> #include <Arduino.h> #ifdef ME_PORT_DEFINED #include "MePort.h" #endif // ME_PORT_DEFINED /** * Class: Buzzer * \par Description * Declaration of Class Buzzer. */ #ifdef ME_PORT_DEFINED class Buzzer : public MePort #else class Buzzer #endif { public: #ifdef ME_PORT_DEFINED /** * Alternate Constructor which can call your own function to map the buzzer to arduino port, * no pins are used or initialized here. * \param[in] * None */ Buzzer(); /** * Alternate Constructor which can call your own function to map the Buzzer to arduino port, * If the hardware serial was selected, we will used the hardware serial. * \param[in] * port - RJ25 port from PORT_1 to M2 */ Buzzer(uint8_t port); /** * Alternate Constructor which can call your own function to map the Buzzer to arduino port, * you can set any slot for the buzzer device. * \param[in] * port - RJ25 port from PORT_1 to M2 * \param[in] * slot - SLOT1 or SLOT2 */ Buzzer(uint8_t port, uint8_t slot); #else // ME_PORT_DEFINED /** * Alternate Constructor which can call your own function to map the Buzzer to arduino port, * \param[in] * switchPin - arduino port for buzzer detect pin. */ Buzzer(int pin); #endif // ME_PORT_DEFINED /** * \par Function * setpin * \par Description * Reset the buzzer available pin by its arduino port. * \param[in] * pin - arduino port for buzzer detect pin. * \par Output * None * \par Return * None * \par Others * None */ void setpin(int pin); /** * \par Function * tone * \par Description * Playing the tones. * \param[in] * pin - Which pin on board that buzzer is connecting to. * \param[in] * frequency - The speed of buzzer's tone play. * \param[in] * duration - Time of a tone play. * \par Output * None * \Return * None. * \par Others * Frequency (in hertz) and duration (in milliseconds). */ void tone(int pin, uint16_t frequency, uint32_t duration); /** * \par Function * tone * \par Description * Playing the tones. * \param[in] * frequency - The speed of buzzer's tone play. * \param[in] * duration - Time of a tone play. * \par Output * None * \Return * None. * \par Others * Frequency (in hertz) and duration (in milliseconds). */ void tone(uint16_t frequency, uint32_t duration = 0); /** * \par Function * noTone * \par Description * Do not playing the tones. * \param[in] * pin - Which pin on board that buzzer is connecting to. * \par Output * None * \Return * None. * \par Others * None */ void _tone(float noteFrequency, long noteDuration, int silentDuration); void bendTones(float initFrequency, float finalFrequency, float prop, long noteDuration, int silentDuration); void noTone(int pin); /** * \par Function * noTone * \par Description * Do not playing the tones. * \param[in] * None * \par Output * None * \Return * None. * \par Others * None */ void noTone(); }; #endif
SHS/doorbell/doorbell.ino
#include "Buzzer.h" #include "Sounds.h" #include "PH20Port.h" PH20Port buzzerplay(P9); #include "Buzzer.h" int touch_PIN2 = 2; Buzzer mBuzzer = Buzzer(buzzerplay.pin1()); Buzzer buzzer(buzzerplay.pin1()); void setup() { pinMode(touch_PIN2,INPUT); } void loop() { if (digitalRead(touch_PIN2)==HIGH) { mBuzzer.bendTones(1500, 2500, 1.05, 20, 8); mBuzzer.bendTones(2499, 1500, 1.05, 25, 8); } else { buzzer.noTone(); } }
SHS/doorbell/PH20Port.cpp
#include "PH20Port.h" PH20Port_Sig PH20_Port[RJ25_MAX] = { { A3, NC, NC, NC, NC, NC }, //1 { A2, NC, NC, NC, NC, NC }, //2 { A1, NC, NC, NC, NC, NC }, //3 { A0, NC, NC, NC, NC, NC }, //4 { 7, A0, NC, NC, NC, NC }, //5 { 8, A1, NC, NC, NC, NC }, //6 { A5, A4, NC, NC, NC, NC }, //7 { 6, 5, NC, NC, NC, NC }, //8 { 3, NC, NC, NC, NC, NC }, //9 { 4, NC, NC, NC, NC, NC }, //10 { 5, NC, NC, NC, NC, NC }, //11 { 6, NC, NC, NC, NC, NC }, //12 { 4, 7, 8, A3, NC, NC }, //13 { 2, 7, A5, A4, NC, NC }, //14 { NC, NC, 3, 5, 6, NC }, //15 { NC, NC, A4, A5, 2, NC }, //16 }; /***********************Port*********************/ /** * Alternate Constructor which can call your own function to map the PH20Port to arduino port, * no pins are used or initialized here */ PH20Port::PH20Port(void) { s1 = PH20_Port[0].s1; s2 = PH20_Port[0].s2; s3 = PH20_Port[0].s3; s4 = PH20_Port[0].s4; s5 = PH20_Port[0].s5; s6 = PH20_Port[0].s6; _port = 0; } /** * Alternate Constructor which can call your own function to map the PH20Port to arduino port, * no pins are used or initialized here, but PWM frequency set to 976 Hz * \param[in] * port - RJ25 port from PORT_1 to M2 */ PH20Port::PH20Port(uint8_t port) { if (port < 1) return; s1 = PH20_Port[port-1].s1; s2 = PH20_Port[port-1].s2; s3 = PH20_Port[port-1].s3; s4 = PH20_Port[port-1].s4; s5 = PH20_Port[port-1].s5; s6 = PH20_Port[port-1].s6; _port = port; } /** * Alternate Constructor which can call your own function to map the PH20Port to arduino port, * no pins are used or initialized here, but PWM frequency set to 976 Hz * \param[in] * port - RJ25 port from PORT_1 to M2 * \param[in] * slot - SLOT1 or SLOT2 */ PH20Port::PH20Port(uint8_t port, uint8_t slot) { if (port < 1) return; s1 = PH20_Port[port-1].s1; s2 = PH20_Port[port-1].s2; _port = port; _slot = slot; } /** * \par Function * getPort * \par Description * Get current valid port of current RJ25 object * \par Output * None * \return * Port bumber from PORT_1 to M2 * \par Others * None */ uint8_t PH20Port::getPort() { return(_port); } /** * \par Function * getSlot * \par Description * Get current valid slot of current RJ25 object's port * \par Output * None * \return * Slot bumber SLOT1 or SLOT2 * \par Others * None */ uint8_t PH20Port::getSlot(void) { return(_slot); } /** * \par Function * dRead1 * \par Description * Read the digital input value on slot1 of current RJ25 object's port * \param[in] * mode - digital input mode INPUT or INPUT_PULLUP * \par Output * None * \return * Digital input value * \par Others * None */ bool PH20Port::dRead1(uint8_t mode) { bool val; pinMode(s1, mode); val = digitalRead(s1); return(val); } /** * \par Function * dRead2 * \par Description * Read the digital input value on slot2 of current RJ25 object's port * \param[in] * mode - digital input mode INPUT or INPUT_PULLUP * \par Output * None * \return * Digital input value * \par Others * None */ bool PH20Port::dRead2(uint8_t mode) { bool val; pinMode(s2, mode); val = digitalRead(s2); return(val); } bool PH20Port::dRead3(uint8_t mode) { bool val; pinMode(s3, mode); val = digitalRead(s3); return(val); } bool PH20Port::dRead4(uint8_t mode) { bool val; pinMode(s4, mode); val = digitalRead(s4); return(val); } bool PH20Port::dRead5(uint8_t mode) { bool val; pinMode(s5, mode); val = digitalRead(s5); return(val); } /** * \par Function * dpRead1 * \par Description * Read the digital input value on slot1 of current RJ25 object's port, the input * mode set as INPUT_PULLUP. * \par Output * None * \return * Digital input value * \par Others * None */ bool PH20Port::dpRead1(void) { bool val; pinMode(s1, INPUT_PULLUP); val = digitalRead(s1); return(val); } /** * \par Function * dpRead2 * \par Description * Read the digital input value on slot2 of current RJ25 object's port, the input * mode set as INPUT_PULLUP. * \par Output * None * \return * Digital input value * \par Others * None */ bool PH20Port::dpRead2(void) { bool val; pinMode(s2, INPUT_PULLUP); val = digitalRead(s2); return(val); } /** * \par Function * dWrite1 * \par Description * Set the digital output value on slot1 of current RJ25 object's port * \param[in] * value - digital output value HIGH or LOW * \par Output * None * \return * None * \par Others * None */ void PH20Port::dWrite1(bool value) { pinMode(s1, OUTPUT); digitalWrite(s1, value); } /** * \par Function * dWrite2 * \par Description * Set the digital output value on slot2 of current RJ25 object's port * \param[in] * value - digital output value HIGH or LOW * \par Output * None * \return * None * \par Others * None */ void PH20Port::dWrite2(bool value) { pinMode(s2, OUTPUT); digitalWrite(s2, value); } void PH20Port::dWrite3(bool value) { pinMode(s3, OUTPUT); digitalWrite(s3, value); } void PH20Port::dWrite4(bool value) { pinMode(s4, OUTPUT); digitalWrite(s4, value); } void PH20Port::dWrite5(bool value) { pinMode(s5, OUTPUT); digitalWrite(s5, value); } /** * \par Function * aRead1 * \par Description * Read the analog value on slot1 of current RJ25 object's port * \par Output * None * \return * Analog value from 0-1023 * \par Others * None */ int16_t PH20Port::aRead1(void) { int16_t val; pinMode(s1, INPUT); val = analogRead(s1); return(val); } /** * \par Function * aRead2 * \par Description * Read the analog value on slot2 of current RJ25 object's port * \par Output * None * \return * Analog value from 0-1023 * \par Others * None */ int16_t PH20Port::aRead2(void) { int16_t val; pinMode(s2, INPUT); val = analogRead(s2); return(val); } int16_t PH20Port::aRead3(void) { int16_t val; pinMode(s3, INPUT); val = analogRead(s3); return(val); } int16_t PH20Port::aRead4(void) { int16_t val; pinMode(s4, INPUT); val = analogRead(s4); return(val); } int16_t PH20Port::aRead5(void) { int16_t val; pinMode(s5, INPUT); val = analogRead(s5); return(val); } /** * \par Function * aWrite1 * \par Description * Set the PWM output value on slot1 of current RJ25 object's port * \param[in] * value - Analog value between 0 to 255 * \par Output * None * \return * None * \par Others * None */ void PH20Port::aWrite1(int16_t value) { analogWrite(s1, value); } /** * \par Function * aWrite2 * \par Description * Set the PWM output value on slot2 of current RJ25 object's port * \param[in] * value - Analog value between 0 to 255 * \par Output * None * \return * None * \par Others * None */ void PH20Port::aWrite2(int16_t value) { analogWrite(s2, value); } void PH20Port::aWrite3(int16_t value) { analogWrite(s3, value); } void PH20Port::aWrite4(int16_t value) { analogWrite(s4, value); } void PH20Port::aWrite5(int16_t value) { analogWrite(s5, value); } /** * \par Function * reset * \par Description * Reset the RJ25 available PIN by its port * \param[in] * port - RJ25 port from PORT_1 to M2 * \par Output * None * \return * None * \par Others * None */ void PH20Port::reset(uint8_t port) { if ( port < 1) return; s1 = PH20_Port[port-1].s1; s2 = PH20_Port[port-1].s2; _port = port; } /** * \par Function * reset * \par Description * Reset the RJ25 available PIN by its port and slot * \param[in] * port - RJ25 port from PORT_1 to M2 * \param[in] * slot - SLOT1 or SLOT2 * \par Output * None * \return * None * \par Others * None */ void PH20Port::reset(uint8_t port, uint8_t slot) { if ( port < 1) return; s1 = PH20_Port[port-1].s1; s2 = PH20_Port[port-1].s2; _port = port; _slot = slot; } /** * \par Function * pin1 * \par Description * Return the arduino pin number of current RJ25 object's slot1 * \par Output * None * \return * The PIN number of arduino * \par Others * None */ uint8_t PH20Port::pin1(void) { return(s1); } /** * \par Function * pin2 * \par Description * Return the arduino pin number of current RJ25 object's slot2 * \par Output * None * \return * The PIN number of arduino * \par Others * None */ uint8_t PH20Port::pin2(void) { return(s2); } uint8_t PH20Port::pin3(void) { return(s3); } uint8_t PH20Port::pin4(void) { return(s4); } uint8_t PH20Port::pin5(void) { return(s5); } /** * \par Function * pin * \par Description * Return the arduino pin number of current RJ25 object's port, if the RJ25 module * have one available PIN. * \par Output * None * \return * The PIN number of arduino * \par Others * None */ uint8_t PH20Port::pin(void) { return(_slot == SLOT_1 ? s1 : s2); } /** * \par Function * pin * \par Description * Return the arduino pin number of current RJ25 object's port * \param[in] * port - RJ25 port from PORT_1 to M2 * \param[in] * slot - SLOT1 or SLOT2 * \par Output * None * \return * The PIN number of arduino * \par Others * None */ uint8_t PH20Port::pin(uint8_t port, uint8_t slot) { if ( port < 1) return; return(slot == SLOT_1 ? PH20_Port[port-1].s1 : PH20_Port[port-1].s2); }
SHS/doorbell/PH20Port.h
#ifndef _PH20Port_H_ #define _PH20Port_H_ #include <Arduino.h> #include <avr/interrupt.h> #include <avr/io.h> #include <util/delay.h> #include <stdint.h> #include <stdlib.h> #define RJ25_MAX 16 #define P1 1 #define P2 2 #define P3 3 #define P4 4 #define P5 5 #define P6 6 #define P7 7 #define P8 8 #define P9 9 #define P10 10 #define P11 11 #define P12 12 #define P13 13 #define P14 14 #define P15 15 #define P16 16 /** * A structure to represent PH20Port Signal. */ typedef struct { uint8_t s1; uint8_t s2; uint8_t s3; uint8_t s4; uint8_t s5; uint8_t s6; } PH20Port_Sig; extern PH20Port_Sig PH20_Port[RJ25_MAX]; // PH20Port[0] is nonsense #define NC (0) //use UART RX for NULL port #define SLOT1 (1) #define SLOT2 (2) #define SLOT3 (3) #define SLOT4 (4) #define SLOT5 (5) #define SLOT6 (6) #define SLOT_1 SLOT1 #define SLOT_2 SLOT2 #define SLOT_3 SLOT3 #define SLOT_4 SLOT4 #define SLOT_5 SLOT3 #define SLOT_6 SLOT4 #ifndef FALSE #define FALSE (0) #endif #ifndef TRUE #define TRUE (1) #endif /** * Class: PH20Port * * \par Description * Declaration of Class PH20Port */ class PH20Port { public: /** * Alternate Constructor which can call your own function to map the PH20Port to arduino port, * no pins are used or initialized here */ PH20Port(void); /** * Alternate Constructor which can call your own function to map the PH20Port to arduino port, * no pins are used or initialized here, but PWM frequency set to 976 Hz * \param[in] * port - RJ25 port from PORT_1 to M2 */ PH20Port(uint8_t port); /** * Alternate Constructor which can call your own function to map the PH20Port to arduino port, * no pins are used or initialized here, but PWM frequency set to 976 Hz * \param[in] * port - RJ25 port from PORT_1 to M2 * \param[in] * slot - SLOT1 or SLOT2 */ PH20Port(uint8_t port, uint8_t slot); /** * \par Function * getPort * \par Description * Get current valid port of current RJ25 object * \par Output * None * \return * Port bumber from PORT_1 to M2 * \par Others * None */ uint8_t getPort(void); /** * \par Function * getSlot * \par Description * Get current valid slot of current RJ25 object's port * \par Output * None * \return * Slot bumber SLOT1 or SLOT2 * \par Others * None */ uint8_t getSlot(void); /** * \par Function * dRead1 * \par Description * Read the digital input value on slot1 of current RJ25 object's port * \param[in] * mode - digital input mode INPUT or INPUT_PULLUP * \par Output * None * \return * Digital input value * \par Others * None */ bool dRead1(uint8_t mode = INPUT); /** * \par Function * dRead2 * \par Description * Read the digital input value on slot2 of current RJ25 object's port * \param[in] * mode - digital input mode INPUT or INPUT_PULLUP * \par Output * None * \return * Digital input value * \par Others * None */ bool dRead2(uint8_t mode = INPUT); bool dRead3(uint8_t mode = INPUT); bool dRead4(uint8_t mode = INPUT); bool dRead5(uint8_t mode = INPUT); /** * \par Function * dpRead1 * \par Description * Read the digital input value on slot1 of current RJ25 object's port, the input * mode set as INPUT_PULLUP. * \par Output * None * \return * Digital input value * \par Others * None */ bool dpRead1(void); /** * \par Function * dpRead2 * \par Description * Read the digital input value on slot2 of current RJ25 object's port, the input * mode set as INPUT_PULLUP. * \par Output * None * \return * Digital input value * \par Others * None */ bool dpRead2(void); /** * \par Function * dWrite1 * \par Description * Set the digital output value on slot1 of current RJ25 object's port * \param[in] * value - digital output value HIGH or LOW * \par Output * None * \return * None * \par Others * None */ void dWrite1(bool value); /** * \par Function * dWrite2 * \par Description * Set the digital output value on slot2 of current RJ25 object's port * \param[in] * value - digital output value HIGH or LOW * \par Output * None * \return * None * \par Others * None */ void dWrite2(bool value); void dWrite3(bool value); void dWrite4(bool value); void dWrite5(bool value); /** * \par Function * aRead1 * \par Description * Read the analog value on slot1 of current RJ25 object's port * \par Output * None * \return * Analog value from 0-1023 * \par Others * None */ int16_t aRead1(void); /** * \par Function * aRead2 * \par Description * Read the analog value on slot2 of current RJ25 object's port * \par Output * None * \return * Analog value from 0-1023 * \par Others * None */ int16_t aRead2(void); int16_t aRead3(void); int16_t aRead4(void); int16_t aRead5(void); /** * \par Function * aWrite1 * \par Description * Set the PWM output value on slot1 of current RJ25 object's port * \param[in] * value - Analog value between 0 to 255 * \par Output * None * \return * None * \par Others * None */ void aWrite1(int16_t value); /** * \par Function * aWrite2 * \par Description * Set the PWM output value on slot2 of current RJ25 object's port * \param[in] * value - Analog value between 0 to 255 * \par Output * None * \return * None * \par Others * None */ void aWrite2(int16_t value); void aWrite3(int16_t value); void aWrite4(int16_t value); void aWrite5(int16_t value); /** * \par Function * reset * \par Description * Reset the RJ25 available PIN by its port * \param[in] * port - RJ25 port from PORT_1 to M2 * \par Output * None * \return * None * \par Others * None */ void reset(uint8_t port); /** * \par Function * reset * \par Description * Reset the RJ25 available PIN by its port and slot * \param[in] * port - RJ25 port from PORT_1 to M2 * \param[in] * slot - SLOT1 or SLOT2 * \par Output * None * \return * None * \par Others * None */ void reset(uint8_t port, uint8_t slot); /** * \par Function * pin1 * \par Description * Return the arduino pin number of current RJ25 object's slot1 * \par Output * None * \return * The PIN number of arduino * \par Others * None */ uint8_t pin1(void); /** * \par Function * pin2 * \par Description * Return the arduino pin number of current RJ25 object's slot2 * \par Output * None * \return * The PIN number of arduino * \par Others * None */ uint8_t pin2(void); uint8_t pin3(void); uint8_t pin4(void); uint8_t pin5(void); /** * \par Function * pin * \par Description * Return the arduino pin number of current RJ25 object's port, if the RJ25 module * have one available PIN. * \par Output * None * \return * The PIN number of arduino * \par Others * None */ uint8_t pin(void); /** * \par Function * pin * \par Description * Return the arduino pin number of current RJ25 object's port * \param[in] * port - RJ25 port from PORT_1 to M2 * \param[in] * slot - SLOT1 or SLOT2 * \par Output * None * \return * The PIN number of arduino * \par Others * None */ uint8_t pin(uint8_t port, uint8_t slot); protected: /** * \par Description * Variables used to store the slot1 gpio number */ uint8_t s1, s2, s3, s4, s5, s6; /** * \par Description * Variables used to store the port */ uint8_t _port; /** * \par Description * Variables used to store the slot */ uint8_t _slot; }; #endif // _RH20Port_H_
SHS/doorbell/Sounds.h
#ifndef _SOUNDS_H_ #define _SOUNDS_H_ typedef enum { E_NOTE, E_SOUND, E_MUSIC, E_BUZZER_TYPE_MAX, }E_BUZZER_TYPE; typedef enum { E_BEAT_8_1, E_BEAT_4_1, E_BEAT_2_1, E_BEAT_1, E_BEAT_2, E_BEAT_4 }E_MUSIC_BEAT; typedef struct { uint16_t note; E_MUSIC_BEAT beat; }ST_MUSIC_TYPE; //*********************************************************************************** //*********************************SOUNDS DEFINES************************************ //*********************************************************************************** // Reference: This list was adapted from the table located here: // http://www.phy.mtu.edu/~suits/notefreqs.html #define note_C0 16.35 //C0 #define note_Db0 17.32 //C#0/Db0 #define note_D0 18.35 //D0 #define note_Eb0 19.45 //D#0/Eb0 #define note_E0 20.6 //E0 #define note_F0 21.83 //F0 #define note_Gb0 23.12 //F#0/Gb0 #define note_G0 24.5 //G0 #define note_Ab0 25.96 //G#0/Ab0 #define note_A0 27.5 //A0 #define note_Bb0 29.14 //A#0/Bb0 #define note_B0 30.87 //B0 #define note_C1 32.7 //C1 #define note_Db1 34.65 //C#1/Db1 #define note_D1 36.71 //D1 #define note_Eb1 38.89 //D#1/Eb1 #define note_E1 41.2 //E1 #define note_F1 43.65 //F1 #define note_Gb1 46.25 //F#1/Gb1 #define note_G1 49 //G1 #define note_Ab1 51.91 //G#1/Ab1 #define note_A1 55 //A1 #define note_Bb1 58.27 //A#1/Bb1 #define note_B1 61.74 //B1 #define note_C2 65.41 //C2 (Middle C) #define note_Db2 69.3 //C#2/Db2 #define note_D2 73.42 //D2 #define note_Eb2 77.78 //D#2/Eb2 #define note_E2 82.41 //E2 #define note_F2 87.31 //F2 #define note_Gb2 92.5 //F#2/Gb2 #define note_G2 98 //G2 #define note_Ab2 103.83 //G#2/Ab2 #define note_A2 110 //A2 #define note_Bb2 116.54 //A#2/Bb2 #define note_B2 123.47 //B2 #define note_C3 130.81 //C3 #define note_Db3 138.59 //C#3/Db3 #define note_D3 146.83 //D3 #define note_Eb3 155.56 //D#3/Eb3 #define note_E3 164.81 //E3 #define note_F3 174.61 //F3 #define note_Gb3 185 //F#3/Gb3 #define note_G3 196 //G3 #define note_Ab3 207.65 //G#3/Ab3 #define note_A3 220 //A3 #define note_Bb3 233.08 //A#3/Bb3 #define note_B3 246.94 //B3 #define note_C4 261.63 //C4 #define note_Db4 277.18 //C#4/Db4 #define note_D4 293.66 //D4 #define note_Eb4 311.13 //D#4/Eb4 #define note_E4 329.63 //E4 #define note_F4 349.23 //F4 #define note_Gb4 369.99 //F#4/Gb4 #define note_G4 392 //G4 #define note_Ab4 415.3 //G#4/Ab4 #define note_A4 440 //A4 #define note_Bb4 466.16 //A#4/Bb4 #define note_B4 493.88 //B4 #define note_C5 523.25 //C5 #define note_Db5 554.37 //C#5/Db5 #define note_D5 587.33 //D5 #define note_Eb5 622.25 //D#5/Eb5 #define note_E5 659.26 //E5 #define note_F5 698.46 //F5 #define note_Gb5 739.99 //F#5/Gb5 #define note_G5 783.99 //G5 #define note_Ab5 830.61 //G#5/Ab5 #define note_A5 880 //A5 #define note_Bb5 932.33 //A#5/Bb5 #define note_B5 987.77 //B5 #define note_C6 1046.5 //C6 #define note_Db6 1108.73 //C#6/Db6 #define note_D6 1174.66 //D6 #define note_Eb6 1244.51 //D#6/Eb6 #define note_E6 1318.51 //E6 #define note_F6 1396.91 //F6 #define note_Gb6 1479.98 //F#6/Gb6 #define note_G6 1567.98 //G6 #define note_Ab6 1661.22 //G#6/Ab6 #define note_A6 1760 //A6 #define note_Bb6 1864.66 //A#6/Bb6 #define note_B6 1975.53 //B6 #define note_C7 2093 //C7 #define note_Db7 2217.46 //C#7/Db7 #define note_D7 2349.32 //D7 #define note_Eb7 2489.02 //D#7/Eb7 #define note_E7 2637.02 //E7 #define note_F7 2793.83 //F7 #define note_Gb7 2959.96 //F#7/Gb7 #define note_G7 3135.96 //G7 #define note_Ab7 3322.44 //G#7/Ab7 #define note_A7 3520 //A7 #define note_Bb7 3729.31 //A#7/Bb7 #define note_B7 3951.07 //B7 #define note_C8 4186.01 //C8 #define note_Db8 4434.92 //C#8/Db8 #define note_D8 4698.64 //D8 #define note_Eb8 4978.03 //D#8/Eb8 #define S_connection 0 #define S_disconnection 1 #define S_buttonPushed 2 #define S_mode1 3 #define S_mode2 4 #define S_mode3 5 #define S_surprise 6 #define S_OhOoh 7 #define S_OhOoh2 8 #define S_cuddly 9 #define S_sleeping 10 #define S_happy 11 #define S_superHappy 12 #define S_happy_short 13 #define S_sad 14 #define S_confused 15 #define S_fart1 16 #define S_fart2 17 #define S_fart3 18 #define S_didi 19 #endif /* _SOUNDS_H_ */
SHS/Intelligent_access_control/EM_TTP229.cpp
#include "EM_TTP229.h" ST_KEY_MAP em_ttp_keymap[16] = { {"1", 0xFFFE}, {"2", 0xFFFD}, {"3", 0xFFFB}, {"4", 0xFFEF}, {"5", 0xFFDF}, {"6", 0xFFBF}, {"7", 0xFEFF}, {"8", 0xFDFF}, {"9", 0xFBFF}, {"*", 0xEFFF}, {"0", 0xDFFF}, {"#", 0xBFFF}, {"D", 0x7FFF}, {"C", 0xF7FF}, {"B", 0xFF7F}, {"A", 0xFFF7}, }; void EM_TTP229::initTTP229(uint8_t SclPin, uint8_t SdoPin) { SCLPin = SclPin; SDOPin = SdoPin; pinMode(SCLPin , OUTPUT); pinMode(SDOPin , INPUT); } uint16_t EM_TTP229::GetKeyCode(void) { unsigned int DATA = 0; pinMode(SDOPin , OUTPUT); digitalWrite(SDOPin, HIGH); delayMicroseconds(93); digitalWrite(SDOPin, LOW); delayMicroseconds(10); pinMode(SDOPin , INPUT); for (int i = 0; i < 16; i++) { digitalWrite(SCLPin, HIGH); digitalWrite(SCLPin, LOW); DATA |= digitalRead(SDOPin) << i; } delay(4); return DATA & 0xFFFF; } String EM_TTP229::GetKeyMap(void) { byte i; uint16_t keycode = GetKeyCode(); ST_KEY_MAP *irkeymap = em_ttp_keymap; for (i = 0; i < KEY_MAX; i++) { // Serial.println(irkeymap[i].keycode); if (irkeymap[i].keycode == keycode) return irkeymap[i].keyname; } return ""; } uint16_t EM_TTP229::GetKey(void) { byte i; uint16_t keycode = GetKeyCode(); ST_KEY_MAP *irkeymap = em_ttp_keymap; for (i = 0; i < KEY_MAX; i++) { // Serial.println(irkeymap[i].keycode); if (irkeymap[i].keycode == keycode) return i; } return 0xff; }
SHS/Intelligent_access_control/EM_TTP229.h
#ifndef _TTP229_H_ #define _TTP229_H_ #include <Arduino.h> #define KEY_MAX 16 typedef struct { String keyname; uint16_t keycode; } ST_KEY_MAP; typedef enum { EM_KEYCODE_1 = 0, EM_KEYCODE_2, EM_KEYCODE_3, EM_KEYCODE_4, EM_KEYCODE_5, EM_KEYCODE_6, EM_KEYCODE_7, EM_KEYCODE_8, EM_KEYCODE_9, EM_KEYCODE_0, EM_KEYCODE_A, EM_KEYCODE_B, EM_KEYCODE_C, EM_KEYCODE_D, IR_KEYCODE_STAR, IR_KEYCODE_POUND, } E_EM_KEYCODE; extern ST_KEY_MAP em_ttp_keymap[]; class EM_TTP229 { private: uint8_t SCLPin, SDOPin; public: void initTTP229(uint8_t SclPin, uint8_t SdoPin); uint16_t GetKeyCode(void); String GetKeyMap(void); uint16_t GetKey(void); }; #endif /* _KEYMAY_H_ */
SHS/Intelligent_access_control/Intelligent_access_control.ino
#include "EM_TTP229.h" EM_TTP229 mTTP229; int SCLPin = 9, SDOPin = 8; String Read_Key () { String key_name = mTTP229.GetKeyMap(); char * result = (char *)key_name.c_str(); return result; } #include <Servo.h> String item; volatile int number; String password[]={"0", "0", "0"}; Servo servo_12; void setup(){ item = ""; number = 0; mTTP229.initTTP229(SCLPin, SDOPin); Serial.begin(9600); servo_12.attach(12); } void loop(){ item = Read_Key(); if (item == "1" && number == 0) { Serial.println(item); password[(int)(0)] = item; number = 1; delay(100); } if (item == "2" && number == 1) { Serial.println(item); password[(int)(1)] = item; number = 2; delay(100); } if (item == "3" && number == 2) { Serial.println(item); password[(int)(2)] = item; number = 3; delay(100); } if (password[(int)(0)] == "1" && (password[(int)(1)] == "2" && password[(int)(2)] == "3")) { Serial.println("hello"); servo_12.write(0); delay(2000); password[(int)(0)] = "0"; password[(int)(1)] = "0"; password[(int)(2)] = "0"; number = 0; } else { servo_12.write(90); delay(20); } }
SHS/remote_control/IR_remote.cpp
SHS/remote_control/IR_remote.cpp
#include
"IR_remote.h"
#include
"Keymap.h"
// Provides ISR
#ifndef
__AVR_ATmega32U4__
#include
<
avr
/
interrupt
.
h
>
volatile
irparams_t irparams
;
bool
MATCH
(
uint8_t measured_ticks
,
uint8_t desired_us
)
{
// Serial.print(measured_ticks);Serial.print(",");Serial.println(desired_us);
return
(
measured_ticks
>=
desired_us
-
(
desired_us
>>
2
)
-
1
&&
measured_ticks
<=
desired_us
+
(
desired_us
>>
2
)
+
1
);
}
ISR
(
TIMER_INTR_NAME
)
{
// Serial.println("ISR");
//Serial.println(millis());
uint8_t irdata
=
(
uint8_t
)
digitalRead
(
irparams
.
recvpin
);
// uint32_t new_time = micros();
// uint8_t timer = (new_time - irparams.lastTime)>>6;
irparams
.
timer
++
;
// One more 50us tick
if
(
irparams
.
rawlen
>=
RAWBUF
)
{
// Buffer overflow
irparams
.
rcvstate
=
STATE_STOP
;
}
switch
(
irparams
.
rcvstate
)
{
case
STATE_IDLE
:
// In the middle of a gap
if
(
irdata
==
MARK
)
{
irparams
.
rawlen
=
0
;
irparams
.
timer
=
0
;
irparams
.
rcvstate
=
STATE_MARK
;
}
break
;
case
STATE_MARK
:
// timing MARK
if
(
irdata
==
SPACE
)
{
// MARK ended, record time
irparams
.
rawbuf
[
irparams
.
rawlen
++
]
=
irparams
.
timer
;
irparams
.
timer
=
0
;
irparams
.
rcvstate
=
STATE_SPACE
;
}
break
;
case
STATE_SPACE
:
// timing SPACE
if
(
irdata
==
MARK
)
{
// SPACE just ended, record it
irparams
.
rawbuf
[
irparams
.
rawlen
++
]
=
irparams
.
timer
;
irparams
.
timer
=
0
;
irparams
.
rcvstate
=
STATE_MARK
;
}
else
{
// SPACE
if
(
irparams
.
timer
>
GAP_TICKS
)
{
// big SPACE, indicates gap between codes
// Mark current code as ready for processing
// Switch to STOP
// Don't reset timer; keep counting space width
irparams
.
rcvstate
=
STATE_STOP
;
irparams
.
lastTime
=
millis
();
}
}
break
;
case
STATE_STOP
:
// waiting, measuring gap
if
(
millis
()
-
irparams
.
lastTime
>
120
)
{
irparams
.
rawlen
=
0
;
irparams
.
timer
=
0
;
irparams
.
rcvstate
=
STATE_IDLE
;
}
else
if
(
irdata
==
MARK
)
{
// reset gap timer
irparams
.
timer
=
0
;
}
break
;
}
// irparams.lastTime = new_time;
}
/**
* Alternate Constructor which can call your own function to map the IR to arduino port,
* no pins are used or initialized here.
* \param[in]
* None
*/
IRremote
::
IRremote
(
int
pin
)
{
pinMode
(
pin
,
INPUT
);
irparams
.
recvpin
=
pin
;
// attachInterrupt(INT0, irISR, CHANGE);
irDelayTime
=
0
;
irIndex
=
0
;
irRead
=
0
;
irReady
=
false
;
irBuffer
=
""
;
irPressed
=
false
;
begin
();
}
/**
* \par Function
* begin
* \par Description
* Initialize interrupt.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void
IRremote
::
begin
()
{
cli
();
// setup pulse clock timer interrupt
//Prescale /8 (16M/8 = 0.5 microseconds per tick)
// Therefore, the timer interval can range from 0.5 to 128 microseconds
// depending on the reset value (255 to 0)
TIMER_CONFIG_NORMAL
();
//Timer2 Overflow Interrupt Enable
TIMER_ENABLE_INTR
;
// TIMER_RESET;
sei
();
// enable interrupts
// initialize state machine variables
irparams
.
rawlen
=
0
;
irparams
.
rcvstate
=
STATE_IDLE
;
}
/**
* \par Function
* end
* \par Description
* Close the interrupt.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void
IRremote
::
end
()
{
EIMSK
&=
~
(
1
<<
INT0
);
}
/**
* \par Function
* decode
* \par Description
* Decodes the received IR message.
* \param[in]
* None
* \par Output
* None
* \par Return
* Returns 0 if no data ready, 1 if data ready.
* \par Others
* Results of decoding are stored in results.
*/
ErrorStatus
IRremote
::
decode
()
{
rawbuf
=
irparams
.
rawbuf
;
rawlen
=
irparams
.
rawlen
;
if
(
irparams
.
rcvstate
!=
STATE_STOP
)
{
return
ERROR
;
}
if
(
decodeNEC
())
{
begin
();
return
SUCCESS
;
}
begin
();
return
ERROR
;
}
/**
* \par Function
* decodeNEC
* \par Description
* Decodes NEC the received IR message.
* \param[in]
* None
* \par Output
* None
* \par Return
* Returns ERROR if decode NEC no done, SUCCESS if decode NEC done.
* \par Others
* Results of decode NEC.
*/
// NECs have a repeat only 4 items long
ErrorStatus
IRremote
::
decodeNEC
()
{
static
unsigned
long
repeat_value
=
0xFFFFFFFF
;
static
byte repeta_time
=
0
;
uint32_t data
=
0
;
int
offset
=
0
;
// Skip first space
// Initial mark
if
(
!
MATCH
(
rawbuf
[
offset
],
NEC_HDR_MARK
/
50
))
{
return
ERROR
;
}
offset
++
;
// Check for repeat
if
(
rawlen
==
3
&&
MATCH
(
rawbuf
[
offset
],
NEC_RPT_SPACE
/
50
)
&&
MATCH
(
rawbuf
[
offset
+
1
],
NEC_BIT_MARK
/
50
))
{
rawbuf
[
offset
]
=
0
;
rawbuf
[
offset
+
1
]
=
0
;
repeta_time
++
;
// if(repeta_time > 1) {
repeta_time
=
0
;
bits
=
0
;
value
=
repeat_value
;
// Serial.println("REPEAT");
decode_type
=
NEC
;
return
SUCCESS
;
// }
}
if
(
rawlen
<
(
2
*
NEC_BITS
+
3
))
{
return
ERROR
;
}
// Initial space
if
(
!
MATCH
(
rawbuf
[
offset
],
NEC_HDR_SPACE
/
50
))
{
return
ERROR
;
}
rawbuf
[
offset
]
=
0
;
offset
++
;
for
(
int
i
=
0
;
i
<
NEC_BITS
;
i
++
)
{
if
(
!
MATCH
(
rawbuf
[
offset
],
NEC_BIT_MARK
/
50
))
{
return
ERROR
;
}
rawbuf
[
offset
]
=
0
;
offset
++
;
if
(
MATCH
(
rawbuf
[
offset
],
NEC_ONE_SPACE
/
50
))
{
//data = (data << 1) | 1;
data
=
(
data
>>
1
)
|
0x80000000
;
}
else
if
(
MATCH
(
rawbuf
[
offset
],
NEC_ZERO_SPACE
/
50
))
{
//data <<= 1;
data
>>=
1
;
}
else
{
return
ERROR
;
}
offset
++
;
}
// Success
bits
=
NEC_BITS
;
value
=
data
;
repeat_value
=
data
;
decode_type
=
NEC
;
repeta_time
=
0
;
return
SUCCESS
;
}
/**
* \par Function
* mark
* \par Description
* Sends an IR mark for the specified number of microseconds.
* \param[in]
* us - THe time of a PWM.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void
IRremote
::
mark
(
uint16_t us
)
{
// Sends an IR mark for the specified number of microseconds.
// The mark output is modulated at the PWM frequency.
TIMER_ENABLE_PWM
;
// Enable pin 3 PWM output
delayMicroseconds
(
us
);
}
/**
* \par Function
* space
* \par Description
* Sends an IR mark for the specified number of microseconds.
* \param[in]
* us - THe time of a PWM.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
/* Leave pin off for time (given in microseconds) */
void
IRremote
::
space
(
uint16_t us
)
{
// Sends an IR space for the specified number of microseconds.
// A space is no output, so the PWM output is disabled.
TIMER_DISABLE_PWM
;
// Disable pin 3 PWM output
delayMicroseconds
(
us
);
}
/**
* \par Function
* enableIROut
* \par Description
* Enable an IR for the specified number of khz.
* \param[in]
* us - THe time of a INTR.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void
IRremote
::
enableIROut
(
uint8_t khz
)
{
TIMER_DISABLE_INTR
;
//Timer2 disable Interrupt
TIMER_CONFIG_KHZ
(
khz
);
}
/**
* \par Function
* enableIRIn
* \par Description
* Enable an IR to write in.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
// initialization
void
IRremote
::
enableIRIn
()
{
cli
();
// setup pulse clock timer interrupt
//Prescale /8 (16M/8 = 0.5 microseconds per tick)
// Therefore, the timer interval can range from 0.5 to 128 microseconds
// depending on the reset value (255 to 0)
TIMER_CONFIG_NORMAL
();
//Timer2 Overflow Interrupt Enable
TIMER_ENABLE_INTR
;
//TIMER_RESET;
sei
();
// enable interrupts
// initialize state machine variables
irparams
.
rcvstate
=
STATE_IDLE
;
irparams
.
rawlen
=
0
;
// set pin modes
pinMode
(
irparams
.
recvpin
,
INPUT
);
}
/**
* \par Function
* sendRaw
* \par Description
* Send the length of data with hz.
* \param[in]
* buf[] - The data's buffer.
* \param[in]
* len - The data's length.
* \param[in]
* hz - The hz for sending data.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void
IRremote
::
sendRaw
(
unsigned
int
buf
[],
int
len
,
uint8_t hz
)
{
enableIROut
(
hz
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
if
(
i
&
1
)
{
space
(
buf
[
i
]);
}
else
{
mark
(
buf
[
i
]);
}
}
space
(
0
);
// Just to be sure
}
/**
* \par Function
* getString
* \par Description
* Get string in a INTR.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the result in a IRQ.
* \par Others
* None
*/
String
IRremote
::
getString
()
{
if
(
decode
())
{
irRead
=
((
value
>>
8
)
>>
8
)
&
0xff
;
if
(
irRead
==
0xa
||
irRead
==
0xd
)
{
irIndex
=
0
;
irReady
=
true
;
}
else
{
irBuffer
+=
irRead
;
irIndex
++
;
}
irDelayTime
=
millis
();
}
else
{