Files
libpigpio/pigbeeper.cpp
2025-08-05 22:33:23 +02:00

75 lines
1.7 KiB
C++

/*****************************************************************************
source::worx libPiGPio
Copyright © 2022 c.holzheuer
chris@sourceworx.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Uses:
pigpiod_if2 by joan2937, pigpio @ abyz.me.uk
https://abyz.me.uk/rpi/pigpio
sigslot by 2017 Pierre-Antoine Lacaze
https://github.com/palacaze/sigslot
***************************************************************************/
#include <pigbeeper.h>
#include <iostream>
pigBeeper::pigBeeper( bcm_t bcmNo )
: pigNodeOut( bcmNo , pull_off )
{
}
pigBeeper::~pigBeeper()
{
}
void pigBeeper::play_tone( double freq, double duration )
{
double halveWaveTime = 1 / ( freq * 2 );
int ms = (int)( 1000 * halveWaveTime );
double waves = duration * freq;
int wv = (int) waves;
//std::cout << "sleep: " << halveWaveTime << " in micros: " << ms << " waves: " << waves << std::endl;
std::thread thrd(
[=]()
{
for( int i=0; i < wv; ++i )
{
_pinOut.set_level( high );
delay_millis( ms );
_pinOut.set_level( low );
delay_millis( ms );
}
} );
thrd.detach();
}
void pigBeeper::play_tune( std::vector<double> frv, std::vector<double> drv )
{
size_t count = std::max( frv.size(), drv.size() );
for( size_t i=0; i<count; ++i )
{
//std::cout << "tone: " << freq[i] << " duration " << duration[i] << std::endl;
play_tone( frv[i], drv[i] );
}
}