yocto_buzzer.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_buzzer.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindBuzzer(), the high-level API for Buzzer functions
6  *
7  * - - - - - - - - - License information: - - - - - - - - -
8  *
9  * Copyright (C) 2011 and beyond by Yoctopuce Sarl, Switzerland.
10  *
11  * Yoctopuce Sarl (hereafter Licensor) grants to you a perpetual
12  * non-exclusive license to use, modify, copy and integrate this
13  * file into your software for the sole purpose of interfacing
14  * with Yoctopuce products.
15  *
16  * You may reproduce and distribute copies of this file in
17  * source or object form, as long as the sole purpose of this
18  * code is to interface with Yoctopuce products. You must retain
19  * this notice in the distributed source file.
20  *
21  * You should refer to Yoctopuce General Terms and Conditions
22  * for additional information regarding your rights and
23  * obligations.
24  *
25  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 'AS IS' WITHOUT
26  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
27  * WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS
28  * FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO
29  * EVENT SHALL LICENSOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
30  * INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
31  * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR
32  * SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT
33  * LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR
34  * CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON THE
35  * BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF
36  * WARRANTY, OR OTHERWISE.
37  *
38  *********************************************************************/
39 
40 
41 #define _CRT_SECURE_NO_DEPRECATE //do not use windows secure crt
42 #include "yocto_buzzer.h"
43 #include "yapi/yjson.h"
44 #include "yapi/yapi.h"
45 #include <string.h>
46 #include <stdio.h>
47 #include <math.h>
48 #include <stdlib.h>
49 #define __FILE_ID__ "buzzer"
50 
51 YBuzzer::YBuzzer(const string& func): YFunction(func)
52 //--- (YBuzzer initialization)
53  ,_frequency(FREQUENCY_INVALID)
54  ,_volume(VOLUME_INVALID)
55  ,_playSeqSize(PLAYSEQSIZE_INVALID)
56  ,_playSeqMaxSize(PLAYSEQMAXSIZE_INVALID)
57  ,_playSeqSignature(PLAYSEQSIGNATURE_INVALID)
58  ,_command(COMMAND_INVALID)
59  ,_valueCallbackBuzzer(NULL)
60 //--- (end of YBuzzer initialization)
61 {
62  _className="Buzzer";
63 }
64 
66 {
67 //--- (YBuzzer cleanup)
68 //--- (end of YBuzzer cleanup)
69 }
70 //--- (YBuzzer implementation)
71 // static attributes
74 
76 {
77  if(json_val->has("frequency")) {
78  _frequency = floor(json_val->getDouble("frequency") * 1000.0 / 65536.0 + 0.5) / 1000.0;
79  }
80  if(json_val->has("volume")) {
81  _volume = json_val->getInt("volume");
82  }
83  if(json_val->has("playSeqSize")) {
84  _playSeqSize = json_val->getInt("playSeqSize");
85  }
86  if(json_val->has("playSeqMaxSize")) {
87  _playSeqMaxSize = json_val->getInt("playSeqMaxSize");
88  }
89  if(json_val->has("playSeqSignature")) {
90  _playSeqSignature = json_val->getInt("playSeqSignature");
91  }
92  if(json_val->has("command")) {
93  _command = json_val->getString("command");
94  }
95  return YFunction::_parseAttr(json_val);
96 }
97 
98 
108 int YBuzzer::set_frequency(double newval)
109 {
110  string rest_val;
111  int res;
113  try {
114  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
115  res = _setAttr("frequency", rest_val);
116  } catch (std::exception) {
118  throw;
119  }
121  return res;
122 }
123 
132 {
133  double res = 0.0;
135  try {
138  {
141  }
142  }
143  }
144  res = _frequency;
145  } catch (std::exception) {
147  throw;
148  }
150  return res;
151 }
152 
161 {
162  int res = 0;
164  try {
167  {
170  }
171  }
172  }
173  res = _volume;
174  } catch (std::exception) {
176  throw;
177  }
179  return res;
180 }
181 
191 int YBuzzer::set_volume(int newval)
192 {
193  string rest_val;
194  int res;
196  try {
197  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
198  res = _setAttr("volume", rest_val);
199  } catch (std::exception) {
201  throw;
202  }
204  return res;
205 }
206 
215 {
216  int res = 0;
218  try {
221  {
224  }
225  }
226  }
227  res = _playSeqSize;
228  } catch (std::exception) {
230  throw;
231  }
233  return res;
234 }
235 
244 {
245  int res = 0;
247  try {
248  if (_cacheExpiration == 0) {
250  {
253  }
254  }
255  }
256  res = _playSeqMaxSize;
257  } catch (std::exception) {
259  throw;
260  }
262  return res;
263 }
264 
276 {
277  int res = 0;
279  try {
282  {
285  }
286  }
287  }
288  res = _playSeqSignature;
289  } catch (std::exception) {
291  throw;
292  }
294  return res;
295 }
296 
298 {
299  string res;
301  try {
304  {
307  }
308  }
309  }
310  res = _command;
311  } catch (std::exception) {
313  throw;
314  }
316  return res;
317 }
318 
319 int YBuzzer::set_command(const string& newval)
320 {
321  string rest_val;
322  int res;
324  try {
325  rest_val = newval;
326  res = _setAttr("command", rest_val);
327  } catch (std::exception) {
329  throw;
330  }
332  return res;
333 }
334 
363 {
364  YBuzzer* obj = NULL;
365  int taken = 0;
366  if (YAPI::_apiInitialized) {
368  taken = 1;
369  }try {
370  obj = (YBuzzer*) YFunction::_FindFromCache("Buzzer", func);
371  if (obj == NULL) {
372  obj = new YBuzzer(func);
373  YFunction::_AddToCache("Buzzer", func, obj);
374  }
375  } catch (std::exception) {
377  throw;
378  }
380  return obj;
381 }
382 
395 {
396  string val;
397  if (callback != NULL) {
399  } else {
401  }
402  _valueCallbackBuzzer = callback;
403  // Immediately invoke value callback with current value
404  if (callback != NULL && this->isOnline()) {
405  val = _advertisedValue;
406  if (!(val == "")) {
407  this->_invokeValueCallback(val);
408  }
409  }
410  return 0;
411 }
412 
414 {
415  if (_valueCallbackBuzzer != NULL) {
416  _valueCallbackBuzzer(this, value);
417  } else {
419  }
420  return 0;
421 }
422 
424 {
425  return this->set_command(command);
426 }
427 
437 int YBuzzer::addFreqMoveToPlaySeq(int freq,int msDelay)
438 {
439  return this->sendCommand(YapiWrapper::ysprintf("A%d,%d",freq,msDelay));
440 }
441 
451 int YBuzzer::addPulseToPlaySeq(int freq,int msDuration)
452 {
453  return this->sendCommand(YapiWrapper::ysprintf("B%d,%d",freq,msDuration));
454 }
455 
466 int YBuzzer::addVolMoveToPlaySeq(int volume,int msDuration)
467 {
468  return this->sendCommand(YapiWrapper::ysprintf("C%d,%d",volume,msDuration));
469 }
470 
484 int YBuzzer::addNotesToPlaySeq(string notes)
485 {
486  int tempo = 0;
487  int prevPitch = 0;
488  int prevDuration = 0;
489  int prevFreq = 0;
490  int note = 0;
491  int num = 0;
492  int typ = 0;
493  string ascNotes;
494  int notesLen = 0;
495  int i = 0;
496  int ch = 0;
497  int dNote = 0;
498  int pitch = 0;
499  int freq = 0;
500  int ms = 0;
501  int ms16 = 0;
502  int rest = 0;
503  tempo = 100;
504  prevPitch = 3;
505  prevDuration = 4;
506  prevFreq = 110;
507  note = -99;
508  num = 0;
509  typ = 3;
510  ascNotes = notes;
511  notesLen = (int)(ascNotes).size();
512  i = 0;
513  while (i < notesLen) {
514  ch = ((u8)ascNotes[i]);
515  // A (note))
516  if (ch == 65) {
517  note = 0;
518  }
519  // B (note)
520  if (ch == 66) {
521  note = 2;
522  }
523  // C (note)
524  if (ch == 67) {
525  note = 3;
526  }
527  // D (note)
528  if (ch == 68) {
529  note = 5;
530  }
531  // E (note)
532  if (ch == 69) {
533  note = 7;
534  }
535  // F (note)
536  if (ch == 70) {
537  note = 8;
538  }
539  // G (note)
540  if (ch == 71) {
541  note = 10;
542  }
543  // '#' (sharp modifier)
544  if (ch == 35) {
545  note = note + 1;
546  }
547  // 'b' (flat modifier)
548  if (ch == 98) {
549  note = note - 1;
550  }
551  // ' (octave up)
552  if (ch == 39) {
553  prevPitch = prevPitch + 12;
554  }
555  // , (octave down)
556  if (ch == 44) {
557  prevPitch = prevPitch - 12;
558  }
559  // R (rest)
560  if (ch == 82) {
561  typ = 0;
562  }
563  // ! (staccato modifier)
564  if (ch == 33) {
565  typ = 1;
566  }
567  // ^ (short modifier)
568  if (ch == 94) {
569  typ = 2;
570  }
571  // _ (legato modifier)
572  if (ch == 95) {
573  typ = 4;
574  }
575  // - (glissando modifier)
576  if (ch == 45) {
577  typ = 5;
578  }
579  // % (tempo change)
580  if ((ch == 37) && (num > 0)) {
581  tempo = num;
582  num = 0;
583  }
584  if ((ch >= 48) && (ch <= 57)) {
585  // 0-9 (number)
586  num = (num * 10) + (ch - 48);
587  }
588  if (ch == 46) {
589  // . (duration modifier)
590  num = ((num * 2) / (3));
591  }
592  if (((ch == 32) || (i+1 == notesLen)) && ((note > -99) || (typ != 3))) {
593  if (num == 0) {
594  num = prevDuration;
595  } else {
596  prevDuration = num;
597  }
598  ms = (int) floor(320000.0 / (tempo * num)+0.5);
599  if (typ == 0) {
600  this->addPulseToPlaySeq(0, ms);
601  } else {
602  dNote = note - (((prevPitch) % (12)));
603  if (dNote > 6) {
604  dNote = dNote - 12;
605  }
606  if (dNote <= -6) {
607  dNote = dNote + 12;
608  }
609  pitch = prevPitch + dNote;
610  freq = (int) floor(440 * exp(pitch * 0.05776226504666)+0.5);
611  ms16 = ((ms) >> (4));
612  rest = 0;
613  if (typ == 3) {
614  rest = 2 * ms16;
615  }
616  if (typ == 2) {
617  rest = 8 * ms16;
618  }
619  if (typ == 1) {
620  rest = 12 * ms16;
621  }
622  if (typ == 5) {
623  this->addPulseToPlaySeq(prevFreq, ms16);
624  this->addFreqMoveToPlaySeq(freq, 8 * ms16);
625  this->addPulseToPlaySeq(freq, ms - 9 * ms16);
626  } else {
627  this->addPulseToPlaySeq(freq, ms - rest);
628  if (rest > 0) {
629  this->addPulseToPlaySeq(0, rest);
630  }
631  }
632  prevFreq = freq;
633  prevPitch = pitch;
634  }
635  note = -99;
636  num = 0;
637  typ = 3;
638  }
639  i = i + 1;
640  }
641  return YAPI_SUCCESS;
642 }
643 
653 {
654  return this->sendCommand("S");
655 }
656 
664 {
665  return this->sendCommand("X");
666 }
667 
675 {
676  return this->sendCommand("Z");
677 }
678 
686 {
687  return this->sendCommand("s");
688 }
689 
700 int YBuzzer::pulse(int frequency,int duration)
701 {
702  return this->set_command(YapiWrapper::ysprintf("P%d,%d",frequency,duration));
703 }
704 
715 int YBuzzer::freqMove(int frequency,int duration)
716 {
717  return this->set_command(YapiWrapper::ysprintf("F%d,%d",frequency,duration));
718 }
719 
730 int YBuzzer::volumeMove(int volume,int duration)
731 {
732  return this->set_command(YapiWrapper::ysprintf("V%d,%d",volume,duration));
733 }
734 
748 int YBuzzer::playNotes(string notes)
749 {
750  this->resetPlaySeq();
751  this->addNotesToPlaySeq(notes);
752  return this->oncePlaySeq();
753 }
754 
756 {
757  string hwid;
758 
759  if(YISERR(_nextFunction(hwid)) || hwid=="") {
760  return NULL;
761  }
762  return YBuzzer::FindBuzzer(hwid);
763 }
764 
766 {
767  vector<YFUN_DESCR> v_fundescr;
768  YDEV_DESCR ydevice;
769  string serial, funcId, funcName, funcVal, errmsg;
770 
771  if(YISERR(YapiWrapper::getFunctionsByClass("Buzzer", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
772  v_fundescr.size() == 0 ||
773  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
774  return NULL;
775  }
776  return YBuzzer::FindBuzzer(serial+"."+funcId);
777 }
778 
779 //--- (end of YBuzzer implementation)
780 
781 //--- (YBuzzer functions)
782 //--- (end of YBuzzer functions)
string _advertisedValue
Definition: yocto_api.h:1569
virtual int freqMove(int frequency, int duration)
double getDouble(const string &key)
Definition: yocto_api.cpp:796
void(* YBuzzerValueCallback)(YBuzzer *func, const string &functionValue)
Definition: yocto_buzzer.h:54
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static const string COMMAND_INVALID
Definition: yocto_buzzer.h:106
virtual int addVolMoveToPlaySeq(int volume, int msDuration)
int get_playSeqSignature(void)
static const int PLAYSEQSIZE_INVALID
Definition: yocto_buzzer.h:103
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static const int PLAYSEQMAXSIZE_INVALID
Definition: yocto_buzzer.h:104
string _command
Definition: yocto_buzzer.h:84
virtual int resetPlaySeq(void)
int volume(void)
Definition: yocto_buzzer.h:142
int get_volume(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
virtual int oncePlaySeq(void)
int set_command(const string &newval)
int get_playSeqSize(void)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
double frequency(void)
Definition: yocto_buzzer.h:130
int _playSeqMaxSize
Definition: yocto_buzzer.h:82
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
static YBuzzer * FindBuzzer(string func)
string get_command(void)
int _volume
Definition: yocto_buzzer.h:80
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
double _frequency
Definition: yocto_buzzer.h:79
virtual int volumeMove(int volume, int duration)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
int set_volume(int newval)
YBuzzerValueCallback _valueCallbackBuzzer
Definition: yocto_buzzer.h:85
int _playSeqSize
Definition: yocto_buzzer.h:81
int get_playSeqMaxSize(void)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
virtual int playNotes(string notes)
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static YBuzzer * FirstBuzzer(void)
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
virtual int pulse(int frequency, int duration)
string command(void)
Definition: yocto_buzzer.h:199
bool isOnline(void)
Definition: yocto_api.cpp:3291
YBuzzer(const string &func)
static const double FREQUENCY_INVALID
Definition: yocto_buzzer.h:101
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
int set_frequency(double newval)
virtual int sendCommand(string command)
string _className
Definition: yocto_api.h:1558
virtual int _invokeValueCallback(string value)
YBuzzer * nextBuzzer(void)
virtual int addNotesToPlaySeq(string notes)
virtual int registerValueCallback(YBuzzerValueCallback callback)
int _playSeqSignature
Definition: yocto_buzzer.h:83
virtual int _parseAttr(YJSONObject *json_val)
virtual int addPulseToPlaySeq(int freq, int msDuration)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static const int VOLUME_INVALID
Definition: yocto_buzzer.h:102
virtual int addFreqMoveToPlaySeq(int freq, int msDelay)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
double get_frequency(void)
bool has(const string &key)
Definition: yocto_api.cpp:735
virtual int stopPlaySeq(void)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int startPlaySeq(void)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563
static const int PLAYSEQSIGNATURE_INVALID
Definition: yocto_buzzer.h:105


yoctopuce_altimeter
Author(s): Anja Sheppard
autogenerated on Mon Jun 10 2019 15:49:11