wt5001: Added functions that throw exceptions when failing to read from sensors. Added java exception handling, and modified examples.

Signed-off-by: Stefan Andritoiu <stefan.andritoiu@intel.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Stefan Andritoiu 2015-11-18 14:50:22 +02:00 committed by Mihai Tudor Panu
parent 7f7fdb8441
commit fcb36276b7
6 changed files with 137 additions and 18 deletions

View File

@ -1,3 +1,5 @@
import java.io.IOException;
/* /*
* Author: Stefan Andritoiu <stefan.andritoiu@intel.com> * Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
* Copyright (c) 2015 Intel Corporation. * Copyright (c) 2015 Intel Corporation.
@ -44,7 +46,7 @@ public class WT5001Sample {
System.out.println("4 - previous track"); System.out.println("4 - previous track");
} }
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) {
// ! [Interesting] // ! [Interesting]
// Instantiate a WT5001 serial MP3 player on uart 0 // Instantiate a WT5001 serial MP3 player on uart 0
upm_wt5001.WT5001 mp3 = new upm_wt5001.WT5001(0); upm_wt5001.WT5001 mp3 = new upm_wt5001.WT5001(0);
@ -87,21 +89,37 @@ public class WT5001Sample {
} }
// print out some information // print out some information
short vol[] = new short[1]; try {
if (mp3.getVolume(vol)) short vol;
System.out.println("The current volume is: " + vol[0]); vol = mp3.getVolume();
System.out.println("The current volume is: " + vol);
} catch (IOException e) {
e.printStackTrace();
}
short ps[] = new short[1]; try {
if (mp3.getPlayState(ps)) short ps;
System.out.println("The current play state is: " + ps[0]); ps = mp3.getPlayState();
System.out.println("The current play state is: " + ps);
} catch (IOException e) {
e.printStackTrace();
}
int numf[] = new int[1]; try {
if (mp3.getNumFiles(upm_wt5001.WT5001.WT5001_PLAYSOURCE_T.SD, numf)) int numf;
System.out.println("The number of files on the SD card is: " + numf[0]); numf = mp3.getNumFiles(upm_wt5001.WT5001.WT5001_PLAYSOURCE_T.SD);
System.out.println("The number of files on the SD card is: " + numf);
} catch (IOException e) {
e.printStackTrace();
}
int curf[] = new int[1]; try {
if (mp3.getCurrentFile(curf)) int curf;
System.out.println("The current file is: " + curf[0]); curf = mp3.getCurrentFile();
System.out.println("The current file is: " + curf);
} catch (IOException e) {
e.printStackTrace();
}
int year[] = new int[1]; int year[] = new int[1];
short month[] = new short[1]; short month[] = new short[1];

21
src/java_exceptions.i Normal file
View File

@ -0,0 +1,21 @@
/* -----------------------------------------------------------------------------
* java_exceptions.i
*
* SWIG library file providing java-specific exception handling in the upm library
* ----------------------------------------------------------------------------- */
/*
* Use this macro for functions that read data from a sensor and throw a
* std::runtime_error in case of a read failure
*/
%define READDATA_EXCEPTION(function)
%javaexception("java.io.IOException") function {
try {
$action
} catch (std::runtime_error &e) {
jclass clazz = jenv->FindClass("java/io/IOException");
jenv->ThrowNew(clazz, e.what());
return $null;
}
}
%enddef

View File

@ -2,11 +2,9 @@
%include "stdint.i" %include "stdint.i"
%include "upm_exception.i" %include "upm_exception.i"
/* %include "arrays_java.i"; */ %apply int { speed_t };
/* %apply unsigned char[] {uint8_t *mama}; */ %apply int { mraa_result_t };
%apply int { speed_t }; %apply int { mraa::Result };
%apply int { mraa_result_t };
%apply int { mraa::Result };
#if (SWIG_JAVASCRIPT_V8) #if (SWIG_JAVASCRIPT_V8)
%{ %{

View File

@ -3,6 +3,7 @@
%include "stdint.i" %include "stdint.i"
%include "typemaps.i" %include "typemaps.i"
%include "../java_buffer.i" %include "../java_buffer.i"
%include "../java_exceptions.i"
%apply uint8_t *OUTPUT { uint8_t *vol }; %apply uint8_t *OUTPUT { uint8_t *vol };
%apply uint8_t *OUTPUT { uint8_t *ps }; %apply uint8_t *OUTPUT { uint8_t *ps };
@ -17,5 +18,17 @@
speed_t int_B9600 = B9600; speed_t int_B9600 = B9600;
%} %}
%ignore getVolume(uint8_t *vol);
READDATA_EXCEPTION(getVolume())
%ignore getPlayState(uint8_t *ps);
READDATA_EXCEPTION(getPlayState())
%ignore getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf);
READDATA_EXCEPTION(getNumFiles(WT5001_PLAYSOURCE_T psrc))
%ignore getCurrentFile(uint16_t *curf);
READDATA_EXCEPTION(getCurrentFile())
%include "wt5001.h" %include "wt5001.h"
speed_t int_B9600 = B9600; speed_t int_B9600 = B9600;

View File

@ -450,6 +450,15 @@ bool WT5001::getVolume(uint8_t *vol)
return true; return true;
} }
uint8_t WT5001::getVolume()
{
uint8_t vol = 0;
if (!getVolume(&vol))
throw std::runtime_error(std::string(__PRETTY_FUNCTION__) +
": readData() failed");
return vol;
}
bool WT5001::getPlayState(uint8_t *ps) bool WT5001::getPlayState(uint8_t *ps)
{ {
char pkt[4]; char pkt[4];
@ -473,6 +482,15 @@ bool WT5001::getPlayState(uint8_t *ps)
return true; return true;
} }
uint8_t WT5001::getPlayState()
{
uint8_t ps = 0;
if (!getPlayState(&ps))
throw std::runtime_error(std::string(__PRETTY_FUNCTION__) +
": readData() failed");
return ps;
}
bool WT5001::getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf) bool WT5001::getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf)
{ {
char pkt[4]; char pkt[4];
@ -515,6 +533,15 @@ bool WT5001::getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf)
return true; return true;
} }
uint16_t WT5001::getNumFiles(WT5001_PLAYSOURCE_T psrc)
{
uint16_t numf = 0;
if (!getNumFiles(psrc, &numf))
throw std::runtime_error(std::string(__PRETTY_FUNCTION__) +
": readData() failed");
return numf;
}
bool WT5001::getCurrentFile(uint16_t *curf) bool WT5001::getCurrentFile(uint16_t *curf)
{ {
char pkt[4]; char pkt[4];
@ -541,6 +568,15 @@ bool WT5001::getCurrentFile(uint16_t *curf)
return true; return true;
} }
uint16_t WT5001::getCurrentFile()
{
uint16_t curf = 0;
if (!getCurrentFile(&curf))
throw std::runtime_error(std::string(__PRETTY_FUNCTION__) +
": readData() failed");
return curf;
}
bool WT5001::getDate(uint16_t *year, uint8_t *month, uint8_t *day) bool WT5001::getDate(uint16_t *year, uint8_t *month, uint8_t *day)
{ {
char pkt[4]; char pkt[4];

View File

@ -292,6 +292,14 @@ namespace upm {
*/ */
bool getVolume(uint8_t *vol); bool getVolume(uint8_t *vol);
/**
* Gets the current volume
*
* @return Volume
* @throws std::runtime_error if reading from the sensor failed
*/
uint8_t getVolume();
/** /**
* Gets the current play state: 1 = playing, 2 = stopped, 3 = paused * Gets the current play state: 1 = playing, 2 = stopped, 3 = paused
* *
@ -300,6 +308,14 @@ namespace upm {
*/ */
bool getPlayState(uint8_t *ps); bool getPlayState(uint8_t *ps);
/**
* Gets the current play state: 1 = playing, 2 = stopped, 3 = paused
*
* @return Play state
* @throws std::runtime_error if reading from the sensor failed
*/
uint8_t getPlayState();
/** /**
* Gets the number of files present on the source device * Gets the number of files present on the source device
* *
@ -309,6 +325,15 @@ namespace upm {
*/ */
bool getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf); bool getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf);
/**
* Gets the number of files present on the source device
*
* @param psrc Storage source
* @return Number of files
* @throws std::runtime_error if reading from the sensor failed
*/
uint16_t getNumFiles(WT5001_PLAYSOURCE_T psrc);
/** /**
* Gets the index of the current file * Gets the index of the current file
* *
@ -317,6 +342,14 @@ namespace upm {
*/ */
bool getCurrentFile(uint16_t *curf); bool getCurrentFile(uint16_t *curf);
/**
* Gets the index of the current file
*
* @return Index of the curretn file
* @throws std::runtime_error if reading from the sensor failed
*/
uint16_t getCurrentFile();
/** /**
* Gets the device date * Gets the device date
* *