examples: Remove heap allocation from C++ examples

Cleanup of UPM C++ examples.  Switched from heap allocation to
stack allocation when possible.  This simplifies the samples since it
removes the need for explicit memory management.  A script was used to
identify and replace pointer use.  To simplify the replace script, I
re-formatted the C++ examples using the UPM .clang-format file.
Unfortuantely this changes the look of the UPM C++ examples to a large
degree.  However, examples will now have a standard look/feel and
uniform formatting.

    * Ran clang-format w/provided UPM .clang-format file
    * Removed new's/delete's whenever possible (left those in interface
      examples)
    * Added IIO sensor library implementation of callback void* arg
    * Converted all sleeps to upm defined delays (added header when
      necessary)
    * Scrubbed CXX example includes

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2017-08-30 15:00:29 -07:00
committed by Mihai Tudor Panu
parent bd6e4ec786
commit 5cefe7f5f3
290 changed files with 7976 additions and 8520 deletions

View File

@ -22,109 +22,108 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <string>
#include "wt5001.hpp"
using namespace std;
void printUsage(char *progname)
void
printUsage(char* progname)
{
cout << "Usage:" << progname << " <command>" << endl;
cout << "Commands:" << endl;
cout << "0 - stop playing" << endl;
cout << "1 - start playing track 1" << endl;
cout << "2 - pause/un-pause playback" << endl;
cout << "3 - next track" << endl;
cout << "4 - previous track" << endl;
}
int main (int argc, char **argv)
{
//! [Interesting]
// Instantiate a WT5001 serial MP3 player on uart 0.
// This example was tested on the Grove Serial MP3 module.
upm::WT5001* mp3 = new upm::WT5001(0);
int cmd = -1;
if (argc > 1)
cmd = atoi(argv[1]);
// make sure port is initialized properly. 9600 baud is the default.
if (!mp3->setupTty(B9600))
{
cerr << "Failed to setup tty port parameters" << endl;
return 1;
}
switch (cmd)
{
case 0:
mp3->stop();
break;
case 1:
mp3->play(upm::WT5001::SD, 1);
break;
case 2:
mp3->pause();
break;
case 3:
mp3->next();
break;
case 4:
mp3->previous();
break;
default:
// nothing, just output usage, and info below
printUsage(argv[0]);
break;
}
// Example: set the date
// mp3->setDate(2015, 1, 1);
// Example: set the time
// mp3->setTime(12, 30, 30);
// print out some information
uint8_t vol = 0;
if (mp3->getVolume(&vol))
cout << "The current volume is: " << int(vol) << endl;
uint8_t ps = 0;
if (mp3->getPlayState(&ps))
cout << "The current play state is: " << int(ps) << endl;
uint16_t numf = 0;
if (mp3->getNumFiles(upm::WT5001::SD, &numf))
cout << "The number of files on the SD card is: " << int(numf) << endl;
uint16_t curf = 0;
if (mp3->getCurrentFile(&curf))
cout << "The current file is: " << int(curf) << endl;
uint16_t year = 0;
uint8_t month = 0, day = 0;
if (mp3->getDate(&year, &month, &day))
cout << "The device date is: " << int(month) << "/" << int(day)
<< "/" << int(year) << endl;
uint8_t hour = 0, minute = 0, second = 0;
if (mp3->getTime(&hour, &minute, &second))
cout << "The device time is: " << int(hour) << ":" << int(minute)
<< ":" << int(second) << endl;
//! [Interesting]
cout << "Exiting..." << endl;
delete mp3;
return 0;
cout << "Usage:" << progname << " <command>" << endl;
cout << "Commands:" << endl;
cout << "0 - stop playing" << endl;
cout << "1 - start playing track 1" << endl;
cout << "2 - pause/un-pause playback" << endl;
cout << "3 - next track" << endl;
cout << "4 - previous track" << endl;
}
int
main(int argc, char** argv)
{
//! [Interesting]
// Instantiate a WT5001 serial MP3 player on uart 0.
// This example was tested on the Grove Serial MP3 module.
upm::WT5001 mp3(0);
int cmd = -1;
if (argc > 1)
cmd = atoi(argv[1]);
// make sure port is initialized properly. 9600 baud is the default.
if (!mp3.setupTty(B9600)) {
cerr << "Failed to setup tty port parameters" << endl;
return 1;
}
switch (cmd) {
case 0:
mp3.stop();
break;
case 1:
mp3.play(upm::WT5001::SD, 1);
break;
case 2:
mp3.pause();
break;
case 3:
mp3.next();
break;
case 4:
mp3.previous();
break;
default:
// nothing, just output usage, and info below
printUsage(argv[0]);
break;
}
// Example: set the date
// mp3.setDate(2015, 1, 1);
// Example: set the time
// mp3.setTime(12, 30, 30);
// print out some information
uint8_t vol = 0;
if (mp3.getVolume(&vol))
cout << "The current volume is: " << int(vol) << endl;
uint8_t ps = 0;
if (mp3.getPlayState(&ps))
cout << "The current play state is: " << int(ps) << endl;
uint16_t numf = 0;
if (mp3.getNumFiles(upm::WT5001::SD, &numf))
cout << "The number of files on the SD card is: " << int(numf) << endl;
uint16_t curf = 0;
if (mp3.getCurrentFile(&curf))
cout << "The current file is: " << int(curf) << endl;
uint16_t year = 0;
uint8_t month = 0, day = 0;
if (mp3.getDate(&year, &month, &day))
cout << "The device date is: " << int(month) << "/" << int(day) << "/" << int(year) << endl;
uint8_t hour = 0, minute = 0, second = 0;
if (mp3.getTime(&hour, &minute, &second))
cout << "The device time is: " << int(hour) << ":" << int(minute) << ":" << int(second)
<< endl;
//! [Interesting]
cout << "Exiting..." << endl;
return 0;
}