From 500e14663b35831b49e4b1f9a1a9dd84944a5b64 Mon Sep 17 00:00:00 2001 From: Andrei Vasiliu Date: Thu, 19 Nov 2015 14:29:51 +0200 Subject: [PATCH] lol: Added exception when invalid argument is passed to method Signed-off-by: Stefan Andritoiu Signed-off-by: Mihai Tudor Panu --- examples/java/LoL_exampleSample.java | 4 +--- src/lol/lol.cxx | 16 +++++++++------- src/lol/lol.h | 11 ++++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/examples/java/LoL_exampleSample.java b/examples/java/LoL_exampleSample.java index 73e8e6fc..1b95ff00 100644 --- a/examples/java/LoL_exampleSample.java +++ b/examples/java/LoL_exampleSample.java @@ -41,9 +41,7 @@ public class LoL_exampleSample { int x = 0, y = 0; while (true) { // revert pixel - short pixel = sensor.getPixel(x, y); - pixel = (short) ((pixel == 0) ? 1 : 0); - sensor.setPixel(x, y, pixel); + sensor.setPixel(x, y, sensor.getPixel(x, y)); if (++x == 13) { x = 0; diff --git a/src/lol/lol.cxx b/src/lol/lol.cxx index 65c5f6ad..aa712e64 100644 --- a/src/lol/lol.cxx +++ b/src/lol/lol.cxx @@ -168,20 +168,22 @@ unsigned char* LoL::getFramebuffer() { return framebuffer; } -unsigned char LoL::setPixel(int x, int y, unsigned char pixel) +void LoL::setPixel(int x, int y, bool pixel) { if (x < 0 || y < 0 || x >= LOL_X || y >= LOL_Y) - return -1; + throw std::invalid_argument(std::string(__FUNCTION__) + + ": pixel coordinates out of bounds"); - framebuffer[x + LOL_X*y] = (pixel == 0) ? 0 : 1; - return 0; + framebuffer[x + LOL_X*y] = (pixel) ? 1 : 0; + return; } -unsigned char LoL::getPixel(int x, int y) +bool LoL::getPixel(int x, int y) { if (x < 0 || y < 0 || x >= LOL_X || y >= LOL_Y) - return -1; + throw std::invalid_argument(std::string(__FUNCTION__) + + ": pixel coordinates out of bounds"); - return (framebuffer[x + LOL_X*y] == 0) ? 0 : 1; + return (framebuffer[x + LOL_X*y] == 0) ? false : true; } diff --git a/src/lol/lol.h b/src/lol/lol.h index c44d1cc9..a1ea32c8 100644 --- a/src/lol/lol.h +++ b/src/lol/lol.h @@ -77,18 +77,19 @@ class LoL { * Gets a pixel at specified coordinates * @param x Coordinate x * @param y Coordinate y - * @return 1 if the pixel is on, 0 if off, -1 on error + * @return true if the pixel is on, false if off + * @throws std::invalid_argument if pixel is out of bounds */ - unsigned char getPixel(int x, int y); + bool getPixel(int x, int y); /** * Sets a pixel at specified coordinates * @param x Coordinate x * @param y Coordinate y - * @param pixel 0 is off, 1 is on - * @return 0 if successful, -1 on error + * @param pixel false is off, true is on + * @throws std::invalid_argument if pixel is out of bounds */ - unsigned char setPixel(int x, int y, unsigned char pixel); + void setPixel(int x, int y, bool pixel); private: mraa_gpio_context m_LoLCtx[14];