diff --git a/examples/java/WT5001Sample.java b/examples/java/WT5001Sample.java index a99b6382..2a02d8d1 100644 --- a/examples/java/WT5001Sample.java +++ b/examples/java/WT5001Sample.java @@ -1,3 +1,5 @@ +import java.io.IOException; + /* * Author: Stefan Andritoiu * Copyright (c) 2015 Intel Corporation. @@ -44,7 +46,7 @@ public class WT5001Sample { System.out.println("4 - previous track"); } - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) { // ! [Interesting] // Instantiate a WT5001 serial MP3 player on uart 0 upm_wt5001.WT5001 mp3 = new upm_wt5001.WT5001(0); @@ -87,21 +89,37 @@ public class WT5001Sample { } // print out some information - short vol[] = new short[1]; - if (mp3.getVolume(vol)) - System.out.println("The current volume is: " + vol[0]); + try { + short vol; + vol = mp3.getVolume(); + System.out.println("The current volume is: " + vol); + } catch (IOException e) { + e.printStackTrace(); + } - short ps[] = new short[1]; - if (mp3.getPlayState(ps)) - System.out.println("The current play state is: " + ps[0]); + try { + short ps; + ps = mp3.getPlayState(); + System.out.println("The current play state is: " + ps); + } catch (IOException e) { + e.printStackTrace(); + } - int numf[] = new int[1]; - if (mp3.getNumFiles(upm_wt5001.WT5001.WT5001_PLAYSOURCE_T.SD, numf)) - System.out.println("The number of files on the SD card is: " + numf[0]); + try { + int numf; + 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]; - if (mp3.getCurrentFile(curf)) - System.out.println("The current file is: " + curf[0]); + try { + int curf; + curf = mp3.getCurrentFile(); + System.out.println("The current file is: " + curf); + } catch (IOException e) { + e.printStackTrace(); + } int year[] = new int[1]; short month[] = new short[1]; diff --git a/src/java_exceptions.i b/src/java_exceptions.i new file mode 100644 index 00000000..235dbe93 --- /dev/null +++ b/src/java_exceptions.i @@ -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 diff --git a/src/upm.i b/src/upm.i index dcb6d252..eed1e0f8 100644 --- a/src/upm.i +++ b/src/upm.i @@ -2,11 +2,9 @@ %include "stdint.i" %include "upm_exception.i" - /* %include "arrays_java.i"; */ - /* %apply unsigned char[] {uint8_t *mama}; */ - %apply int { speed_t }; - %apply int { mraa_result_t }; - %apply int { mraa::Result }; +%apply int { speed_t }; +%apply int { mraa_result_t }; +%apply int { mraa::Result }; #if (SWIG_JAVASCRIPT_V8) %{ diff --git a/src/wt5001/javaupm_wt5001.i b/src/wt5001/javaupm_wt5001.i index 34753f95..c95edbd0 100644 --- a/src/wt5001/javaupm_wt5001.i +++ b/src/wt5001/javaupm_wt5001.i @@ -3,6 +3,7 @@ %include "stdint.i" %include "typemaps.i" %include "../java_buffer.i" +%include "../java_exceptions.i" %apply uint8_t *OUTPUT { uint8_t *vol }; %apply uint8_t *OUTPUT { uint8_t *ps }; @@ -17,5 +18,17 @@ 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" speed_t int_B9600 = B9600; diff --git a/src/wt5001/wt5001.cxx b/src/wt5001/wt5001.cxx index d60e4cd6..e4d18ba4 100644 --- a/src/wt5001/wt5001.cxx +++ b/src/wt5001/wt5001.cxx @@ -450,6 +450,15 @@ bool WT5001::getVolume(uint8_t *vol) 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) { char pkt[4]; @@ -473,6 +482,15 @@ bool WT5001::getPlayState(uint8_t *ps) 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) { char pkt[4]; @@ -515,6 +533,15 @@ bool WT5001::getNumFiles(WT5001_PLAYSOURCE_T psrc, uint16_t *numf) 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) { char pkt[4]; @@ -541,6 +568,15 @@ bool WT5001::getCurrentFile(uint16_t *curf) 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) { char pkt[4]; diff --git a/src/wt5001/wt5001.h b/src/wt5001/wt5001.h index 11ad213e..1603cef8 100644 --- a/src/wt5001/wt5001.h +++ b/src/wt5001/wt5001.h @@ -292,6 +292,14 @@ namespace upm { */ 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 * @@ -300,6 +308,14 @@ namespace upm { */ 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 * @@ -309,6 +325,15 @@ namespace upm { */ 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 * @@ -317,6 +342,14 @@ namespace upm { */ 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 *