API Application Example

#include	"Buzzer.h"
...
...
int	main()
{
	uint32_t	Octave;
...
...
	if ( buzzer_open() < 0 )
	{
		printf("Fail to buzzer_open\n");
	}
...
...
		Octave = 4;/* 4 옥타브의 소리*/
...
...
	case	KEY_1:
		printf("Key code : KEY_1 (0x%X)\n", KeyCode);
		if ( IsReleased == 1 )
		{	/* Key Released */
...
...
			buzzer_off();
...
...
		}
		else
		{
...
...
			buzzer_on_code( MUSIC_FREQUENCY_CODE_C, Octave );/*C는 도이다*/
...
...
		}
		break;
...
...
	if ( buzzer_close() < 0 )
	{
		printf("Fail to buzzer_close\n");
	}
...
...
}

카가 눌리는 동안 소리가 난다.

API Header File

#ifndef	__BUZZER_H__
#define	__BUZZER_H__

#include	<stdint.h>		/* uint??_t */

/***************************************************************************/
/*	For Buzzer
*/
/***************************************************************************/
//	#define	DEVICE_BUZZER_DEBUG_MESSAGE

#define	DEVICE_PWM_CHIP			"/sys/class/pwm/pwmchip0"
#define	DEVICE_BUZZER			"/sys/class/pwm/pwmchip0/pwm0"


#define	MUSIC_FREQUENCY_OCTAVE_MIN	1
#define	MUSIC_FREQUENCY_OCTAVE_MAX	9

typedef	enum
{
	MUSIC_FREQUENCY_CODE_C = 0,	/* C(도)  */
	MUSIC_FREQUENCY_CODE_CS,	/* C#     */
	MUSIC_FREQUENCY_CODE_DF,	/* D flat */
	MUSIC_FREQUENCY_CODE_D,		/* D(레)  */
	MUSIC_FREQUENCY_CODE_DS,	/* D#     */
	MUSIC_FREQUENCY_CODE_EF,	/* E flat */
	MUSIC_FREQUENCY_CODE_E,		/* E(미)  */
	MUSIC_FREQUENCY_CODE_F,		/* F(파)  */
	MUSIC_FREQUENCY_CODE_FS,	/* F#     */
	MUSIC_FREQUENCY_CODE_GF,	/* G flat */
	MUSIC_FREQUENCY_CODE_G,		/* G(솔)  */
	MUSIC_FREQUENCY_CODE_GS,	/* G#     */
	MUSIC_FREQUENCY_CODE_AF,	/* A flat */
	MUSIC_FREQUENCY_CODE_A,		/* A(라)  */
	MUSIC_FREQUENCY_CODE_AS,	/* A#     */
	MUSIC_FREQUENCY_CODE_BF,	/* B flat */
	MUSIC_FREQUENCY_CODE_B		/* B(시)  */
} MusicFrequencyCode_t;


/*	Buzzer Device Open
Return
	0	= Opened
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
extern	int	buzzer_open(void);

/*	Buzzer Device Close
Return
	0	= Closed
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
extern	int	buzzer_close(void);

/*	Buzzer Device Sound On
Return
	0	= OK to On
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
extern	int	buzzer_on(uint32_t Hz);

/*	Buzzer Device Sound On with Music Code
Return
	0	= Ok to On
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
extern	int	buzzer_on_code(MusicFrequencyCode_t MusicFrequencyCode, uint32_t Octave);

/*	Buzzer Device Sound Off
Return
	0	= Ok to Off
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
extern	int	buzzer_off(void);

#endif	/* __BUZZER_H__ */

API Source File

#include	<stdio.h>
#include	<stdint.h>		/* uint??_t */
#include	<string.h>		/* for memset */
#include	<unistd.h>		/* for open/close .. */
#include	<fcntl.h>		/* for O_RDWR */
#include	<sys/ioctl.h>	/* for ioctl */
#include	<sys/fcntl.h>	/* for O_RDWR */

#include	"Buzzer.h"

/***************************************************************************/
/*	Static	Functions
*/
/***************************************************************************/
static	uint32_t	device_buzzer_frequency(MusicFrequencyCode_t MusicFrequencyCode,
											uint32_t Octave)
{
	uint32_t	Frequency;

	Frequency = 0;
	if ( ( Octave >= MUSIC_FREQUENCY_OCTAVE_MIN ) && ( Octave <= MUSIC_FREQUENCY_OCTAVE_MAX ) )
	{
		switch ( MusicFrequencyCode )
		{
		case	MUSIC_FREQUENCY_CODE_C:		/* C(µµ)  */
			Frequency = 3270;				/* 32.70Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_CS:	/* C#     */
		case	MUSIC_FREQUENCY_CODE_DF:	/* D flat */
			Frequency = 3465;				/* 34.65Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_D:		/* D(·¹)  */
			Frequency = 3671;				/* 36.71Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_DS:	/* D#     */
		case	MUSIC_FREQUENCY_CODE_EF:	/* E flat */
			Frequency = 3889;				/* 38.89Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_E:		/* E(¹Ì)  */
			Frequency = 4120;				/* 41.20Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_F:		/* F(ÆÄ)  */
			Frequency = 4365;				/* 43.65Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_FS:	/* F#     */
		case	MUSIC_FREQUENCY_CODE_GF:	/* G flat */
			Frequency = 4625;				/* 46.25Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_G:		/* G(¼Ö)  */
			Frequency = 4900;				/* 49.00Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_GS:	/* G#     */
		case	MUSIC_FREQUENCY_CODE_AF:	/* A flat */
			Frequency = 5191;				/* 51.91Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_A:		/* A(¶ó)  */
			Frequency = 5500;				/* 55.00Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_AS:	/* A#     */
		case	MUSIC_FREQUENCY_CODE_BF:	/* B flat */
			Frequency = 5827;				/* 58.27Hz */
			break;

		case	MUSIC_FREQUENCY_CODE_B:		/* B(½Ã)  */
			Frequency = 6174;				/* 61.74Hz */
			break;
		}
		Frequency <<= (Octave - 1);
	}

	return	(Frequency / 100);
}


static	int			device_buzzer_open(uint8_t *pDevicePWM)
{
	int		pDeviceFile;
	char	pCommand[128] = {0};

	if ( pDevicePWM == (uint8_t *)0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf ( "device_buzzer_open : export pwm 0 : Input Parameter error\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-1;
	}

	sprintf ( pCommand, "%s/export", pDevicePWM );
	pDeviceFile = open ( pCommand, O_WRONLY );
	if ( pDeviceFile < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_open : open export pwm 0 : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-2;
	}

	sprintf ( pCommand, "0" );
	if ( write ( pDeviceFile, pCommand, strlen ( pCommand ) ) < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_open : write export pwm 0 : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		close ( pDeviceFile );
		return	-3;
	}

	close ( pDeviceFile );
	return	0;
}


static	int			device_buzzer_close(uint8_t *pDevicePWM)
{
	int		pDeviceFile;
	char	pCommand[128] = {0};

	if ( pDevicePWM == (uint8_t *)0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf ( "device_buzzer_close : export pwm 0 : Input Parameter error\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-1;
	}

	sprintf ( pCommand, "%s/unexport", pDevicePWM );
	pDeviceFile = open ( pCommand, O_WRONLY );
	if ( pDeviceFile < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_close : open unexport pwm 0 : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-2;
	}

	sprintf ( pCommand, "0" );
	if ( write ( pDeviceFile, pCommand, strlen ( pCommand ) ) < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_close : write unexport pwm 0 : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		close ( pDeviceFile );
		return	-3;
	}

	close ( pDeviceFile );
	return	0;
}


static	int			device_buzzer_on(uint8_t *pDeviceBuzzer, uint32_t Hz)
{
	int		pDeviceFile;
	char	pCommand[128] = {0};
	uint32_t	Value;

	if ( pDeviceBuzzer == (uint8_t *)0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf ( "device_buzzer_on : Input Parameter error\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-1;
	}

	sprintf ( pCommand, "%s/period", pDeviceBuzzer );
	pDeviceFile = open ( pCommand, O_WRONLY );
	if ( pDeviceFile < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_on : open period : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-2;
	}
	Value = 1000000000;
	Value /= Hz;
	sprintf ( pCommand, "%d", Value );
	if ( write ( pDeviceFile, pCommand, strlen ( pCommand ) ) < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_on : write period : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		close ( pDeviceFile );
		return	-3;
	}
	close ( pDeviceFile );

	sprintf ( pCommand, "%s/duty_cycle", pDeviceBuzzer );
	pDeviceFile = open ( pCommand, O_WRONLY );
	if ( pDeviceFile < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_on : open duty_cycle : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-2;
	}
	sprintf ( pCommand, "%d", ( Value >> 1 ) );
	if ( write ( pDeviceFile, pCommand, strlen ( pCommand ) ) < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_on : write duty_cycle : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		close ( pDeviceFile );
		return	-3;
	}
	close ( pDeviceFile );

	sprintf ( pCommand, "%s/enable", pDeviceBuzzer );
	pDeviceFile = open ( pCommand, O_WRONLY );
	if ( pDeviceFile < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_on : open enable : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-2;
	}
	sprintf ( pCommand, "1" );
	if ( write ( pDeviceFile, pCommand, strlen ( pCommand ) ) < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_on : write enable : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		close ( pDeviceFile );
		return	-3;
	}
	close ( pDeviceFile );

	return	0;
}


static	int			device_buzzer_off(uint8_t *pDeviceBuzzer)
{
	int		pDeviceFile;
	char	pCommand[128] = {0};
	uint32_t	Value;

	if ( pDeviceBuzzer == (uint8_t *)0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf ( "device_buzzer_off : Input Parameter error\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-1;
	}

	sprintf ( pCommand, "%s/enable", pDeviceBuzzer );
	pDeviceFile = open ( pCommand, O_WRONLY );
	if ( pDeviceFile < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_off : open enable : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		return	-2;
	}
	sprintf ( pCommand, "0" );
	if ( write ( pDeviceFile, pCommand, strlen ( pCommand ) ) < 0 )
	{
#ifdef	DEVICE_BUZZER_DEBUG_MESSAGE
		printf( "device_buzzer_off : write enable : Fail\n" );
#endif	/* DEVICE_BUZZER_DEBUG_MESSAGE */
		close ( pDeviceFile );
		return	-3;
	}
	close ( pDeviceFile );

	return	0;
}

/***************************************************************************/
/*	Functions
*/
/***************************************************************************/
/*	Buzzer Device Open
Return
	0	= Opened
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
int					buzzer_open(void)
{
	if ( device_buzzer_open(DEVICE_PWM_CHIP) < 0 )
	{
		return	-1;
	}
	return	0;
}

/*	Buzzer Device Close
Return
	0	= Opened
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
int					buzzer_close(void)
{
	if ( device_buzzer_close(DEVICE_PWM_CHIP) < 0 )
	{
		return	-1;
	}
	return	0;
}

/*	Buzzer Device Sound On
Return
	0	= Opened
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
int					buzzer_on(uint32_t Hz)
{
	return	device_buzzer_on( DEVICE_BUZZER, Hz );
}

/*	Buzzer Device Sound On with Music Code
Return
	0	= Opened
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
int					buzzer_on_code(MusicFrequencyCode_t MusicFrequencyCode, uint32_t Octave)
{
	uint32_t	Hz;

	Hz = device_buzzer_frequency( MusicFrequencyCode, Octave);

	if ( Hz != 0 )
	{
		return	device_buzzer_on( DEVICE_BUZZER, Hz );
	}
	else
	{
		return	-1;
	}
}

/*	Buzzer Device Sound Off
Return
	0	= Opened
	-1	= Input Parameter Error
	-2	= Fail to Access Device
	-3	= Fail to Operation
*/
int					buzzer_off(void)
{
	return	device_buzzer_off( DEVICE_BUZZER );
}