aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrew Hannam <andrewh@inmarket.com.au>2013-04-07 16:02:10 +1000
committerAndrew Hannam <andrewh@inmarket.com.au>2013-04-07 16:02:10 +1000
commit9ee7c284e63fc419f70ffc3c53eb23590767ee08 (patch)
tree57a45c0ca6e5222292bebb8445dffaff5757275b /src
parent90f5e851467e5ea14b710ada937409e506820d58 (diff)
downloaduGFX-9ee7c284e63fc419f70ffc3c53eb23590767ee08.tar.gz
uGFX-9ee7c284e63fc419f70ffc3c53eb23590767ee08.tar.bz2
uGFX-9ee7c284e63fc419f70ffc3c53eb23590767ee08.zip
Add GINPUT Dial, simplify GWIN input assignment
Added GINPUT Dial support and a driver that uses GADC to read the dial. Added support for Dial inputs to the GWIN slider. Updated the slider demo for Dial Inputs. Simplified the assigning of inputs to GWIN "widgets" button and slider. Updated the demo's to match the new input to button assignment.
Diffstat (limited to 'src')
-rw-r--r--src/ginput/dial.c202
-rw-r--r--src/gwin/button.c20
-rw-r--r--src/gwin/slider.c67
3 files changed, 226 insertions, 63 deletions
diff --git a/src/ginput/dial.c b/src/ginput/dial.c
index 60bc9ae8..2f572cf8 100644
--- a/src/ginput/dial.c
+++ b/src/ginput/dial.c
@@ -1,36 +1,166 @@
-/*
- ChibiOS/GFX - Copyright (C) 2012, 2013
- Joel Bodenmann aka Tectu <joel@unormal.org>
-
- This file is part of ChibiOS/GFX.
-
- ChibiOS/GFX is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
-
- ChibiOS/GFX is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-/**
- * @file src/ginput/dial.c
- * @brief GINPUT dial code.
- *
- * @defgroup Dial Dial
- * @ingroup GINPUT
- * @{
- */
-#include "ch.h"
-#include "hal.h"
-#include "gfx.h"
-
-#if (GFX_USE_GINPUT && GINPUT_NEED_DIAL) || defined(__DOXYGEN__)
- #error "GINPUT: GINPUT_NEED_DIAL - Not Implemented Yet"
-#endif /* GFX_USE_GINPUT && GINPUT_NEED_DIAL */
-/** @} */
+/*
+ ChibiOS/GFX - Copyright (C) 2012, 2013
+ Joel Bodenmann aka Tectu <joel@unormal.org>
+
+ This file is part of ChibiOS/GFX.
+
+ ChibiOS/GFX is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/GFX is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file src/ginput/dial.c
+ * @brief GINPUT dial code.
+ *
+ * @defgroup Dial Dial
+ * @ingroup GINPUT
+ * @{
+ */
+#include "ch.h"
+#include "hal.h"
+#include "gfx.h"
+
+#if GFX_USE_GINPUT && GINPUT_NEED_DIAL
+
+#include "ginput/lld/dial.h"
+
+static GTIMER_DECL(DialTimer);
+static struct DialStatus_t {
+ uint16_t sensitivity;
+ uint16_t lastvalue;
+ uint16_t max;
+} DialStatus[GINPUT_DIAL_NUM_PORTS];
+
+// The reading callback function
+static void DialCallback(uint16_t instance, uint16_t rawvalue) {
+ struct DialStatus_t *pds;
+ GSourceListener *psl;
+ GEventDial *pe;
+
+ /* Get the information we need */
+ pds = DialStatus+instance;
+
+ /* Range scale - if needed */
+ if (pds->max != GINPUT_DIAL_MAX_VALUE)
+ rawvalue = (uint16_t)((uint32_t)rawvalue * pds->max / GINPUT_DIAL_MAX_VALUE);
+
+ /* Forget about changes below our sensitivity threshold */
+ if (rawvalue >= pds->lastvalue) {
+ if (rawvalue - pds->lastvalue < pds->sensitivity) return;
+ } else {
+ if (pds->lastvalue - rawvalue < pds->sensitivity) return;
+ }
+
+ /* Save the value */
+ pds->lastvalue = rawvalue;
+
+ // Send the event to the listeners that are interested.
+ psl = 0;
+ while ((psl = geventGetSourceListener((GSourceHandle)(DialStatus+instance), psl))) {
+ if (!(pe = (GEventDial *)geventGetEventBuffer(psl)))
+ continue;
+ pe->type = GEVENT_DIAL;
+ pe->instance = instance;
+ pe->value = pds->lastvalue;
+ geventSendEvent(psl);
+ }
+}
+
+GSourceHandle ginputGetDial(uint16_t instance) {
+ struct DialStatus_t *pds;
+
+ if (instance >= GINPUT_DIAL_NUM_PORTS)
+ return 0;
+
+ // Do we need to initialise the dial subsystem?
+ if (!gtimerIsActive(&DialTimer)) {
+ for(pds = DialStatus; pds < DialStatus+GINPUT_DIAL_NUM_PORTS; pds++) {
+ pds->max = GINPUT_DIAL_MAX_VALUE;
+#if GINPUT_DIAL_MAX_VALUE < 100
+ pds->sensitivity = 1;
+#else
+ pds->sensitivity = GINPUT_DIAL_MAX_VALUE/100;
+#endif
+ pds->lastvalue = 0;
+ }
+ ginput_lld_dial_init();
+ gtimerStart(&DialTimer, (GTimerFunction)ginput_lld_dial_poll, DialCallback, TRUE, GINPUT_DIAL_POLL_PERIOD);
+ }
+
+ // OK - return this input
+ return (GSourceHandle)(DialStatus+instance);
+}
+
+void ginputResetDialRange(uint16_t instance) {
+ if (instance >= GINPUT_DIAL_NUM_PORTS)
+ return;
+
+ ginputSetDialRange(instance, GINPUT_DIAL_MAX_VALUE);
+}
+
+uint16_t ginputGetDialRange(uint16_t instance) {
+ if (instance >= GINPUT_DIAL_NUM_PORTS)
+ return 0;
+
+ return DialStatus[instance].max;
+}
+
+void ginputSetDialRange(uint16_t instance, uint16_t max) {
+ struct DialStatus_t *pds;
+
+ if (instance >= GINPUT_DIAL_NUM_PORTS)
+ return;
+
+ pds = DialStatus+instance;
+
+ // Rescale the last value and the sensitivity
+ if (max != pds->max) {
+ pds->lastvalue = (uint16_t)((uint32_t)pds->lastvalue * max / pds->max);
+ pds->sensitivity = (uint16_t)((uint32_t)pds->sensitivity * max / pds->max);
+ pds->max = max;
+ }
+}
+
+/**
+ * @brief Set the level change required before a dial even is generated (threshold)
+ * @note This is done after range scaling
+ *
+ * @param[in] instance The ID of the dial input instance
+ * @param[in] diff The amount of level changes
+ */
+void ginputSetDialSensitivity(uint16_t instance, uint16_t diff) {
+ if (instance >= GINPUT_DIAL_NUM_PORTS)
+ return;
+
+ DialStatus[instance].sensitivity = diff;
+}
+
+/**
+ * @brief Get the current dial status
+ *
+ * @param[in] instance The ID of the dial input instance
+ * @param[in] pdial The dial event struct
+ *
+ * @return Returns FALSE on an error (eg invalid instance)
+ */
+bool_t ginputGetDialStatus(uint16_t instance, GEventDial *pdial) {
+ if (instance >= GINPUT_DIAL_NUM_PORTS)
+ return FALSE;
+ pdial->type = GEVENT_DIAL;
+ pdial->instance = instance;
+ pdial->value = DialStatus[instance].lastvalue;
+ return TRUE;
+}
+
+#endif /* GFX_USE_GINPUT && GINPUT_NEED_DIAL */
+/** @} */
diff --git a/src/gwin/button.c b/src/gwin/button.c
index bec738b4..74fe3080 100644
--- a/src/gwin/button.c
+++ b/src/gwin/button.c
@@ -68,7 +68,7 @@ static void gwinButtonCallback(void *param, GEvent *pe) {
#define pbe ((GEventGWinButton *)pe)
switch (pe->type) {
- #if defined(GINPUT_NEED_MOUSE) && GINPUT_NEED_MOUSE
+ #if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
case GEVENT_MOUSE:
case GEVENT_TOUCH:
// Ignore anything other than the primary mouse button going up or down
@@ -108,7 +108,7 @@ static void gwinButtonCallback(void *param, GEvent *pe) {
#endif
#endif
- #if defined(GINPUT_NEED_TOGGLE) && GINPUT_NEED_TOGGLE
+ #if GFX_USE_GINPUT && GINPUT_NEED_TOGGLE
case GEVENT_TOGGLE:
// State has changed - update the button
gbw->state = pxe->on ? GBTN_DOWN : GBTN_UP;
@@ -375,18 +375,22 @@ void gwinButtonDraw_Square(GHandle gh, bool_t isdown, const char *txt, const GBu
}
#endif
-#if defined(GINPUT_NEED_MOUSE) && GINPUT_NEED_MOUSE
- bool_t gwinAttachButtonMouseSource(GHandle gh, GSourceHandle gsh) {
- if (gh->type != GW_BUTTON)
+#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
+ bool_t gwinAttachButtonMouse(GHandle gh, uint16_t instance) {
+ GSourceHandle gsh;
+
+ if (gh->type != GW_BUTTON || !(gsh = ginputGetMouse(instance)))
return FALSE;
return geventAttachSource(&((GButtonObject *)gh)->listener, gsh, GLISTEN_MOUSEMETA);
}
#endif
-#if defined(GINPUT_NEED_TOGGLE) && GINPUT_NEED_TOGGLE
- bool_t gwinAttachButtonToggleSource(GHandle gh, GSourceHandle gsh) {
- if (gh->type != GW_BUTTON)
+#if GFX_USE_GINPUT && GINPUT_NEED_TOGGLE
+ bool_t gwinAttachButtonToggle(GHandle gh, uint16_t instance) {
+ GSourceHandle gsh;
+
+ if (gh->type != GW_BUTTON || !(gsh = ginputGetToggle(instance)))
return FALSE;
return geventAttachSource(&((GButtonObject *)gh)->listener, gsh, GLISTEN_TOGGLE_OFF|GLISTEN_TOGGLE_ON);
diff --git a/src/gwin/slider.c b/src/gwin/slider.c
index 034ba6b2..f535b35c 100644
--- a/src/gwin/slider.c
+++ b/src/gwin/slider.c
@@ -39,7 +39,9 @@
#define GWIN_SLIDER_DEAD_BAND 5
#endif
-static void trackSliderDraw(GHandle gh, coord_t x, coord_t y);
+#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
+ static void trackSliderDraw(GHandle gh, coord_t x, coord_t y);
+#endif
static const GSliderDrawStyle GSliderDefaultStyle = {
HTML2COLOR(0x404040), // color_edge;
@@ -55,10 +57,11 @@ static void gwinSliderCallback(void *param, GEvent *pe) {
#define gsw ((GSliderObject *)param)
#define gsh ((GSourceHandle)param)
#define pme ((GEventMouse *)pe)
+ #define pde ((GEventDial *)pe)
#define pse ((GEventGWinSlider *)pe)
switch (pe->type) {
- #if defined(GINPUT_NEED_MOUSE) && GINPUT_NEED_MOUSE
+ #if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
case GEVENT_MOUSE:
case GEVENT_TOUCH:
// If not tracking we only only interested in a mouse down over the slider
@@ -91,10 +94,10 @@ static void gwinSliderCallback(void *param, GEvent *pe) {
// Set the new position
if (gh->width < gh->height)
gwinSetSliderPosition(gh,
- (gh->height-1-pme->y+gh->y-GWIN_SLIDER_DEAD_BAND)*(gsw->max-gsw->min)/(gh->height-2*GWIN_SLIDER_DEAD_BAND) + gsw->min);
+ (uint16_t)((uint32_t)(gh->height-1-pme->y+gh->y-GWIN_SLIDER_DEAD_BAND)*(gsw->max-gsw->min)/(gh->height-2*GWIN_SLIDER_DEAD_BAND) + gsw->min));
else
gwinSetSliderPosition(gh,
- (pme->x-gh->x-GWIN_SLIDER_DEAD_BAND)*(gsw->max-gsw->min)/(gh->width-2*GWIN_SLIDER_DEAD_BAND) + gsw->min);
+ (uint16_t)((uint32_t)(pme->x-gh->x-GWIN_SLIDER_DEAD_BAND)*(gsw->max-gsw->min)/(gh->width-2*GWIN_SLIDER_DEAD_BAND) + gsw->min));
// Update the display
gwinSliderDraw(gh);
@@ -109,6 +112,17 @@ static void gwinSliderCallback(void *param, GEvent *pe) {
return;
#endif
+ #if GFX_USE_GINPUT && GINPUT_NEED_DIAL
+ case GEVENT_DIAL:
+ // Set the new position
+ gwinSetSliderPosition(gh, (uint16_t)((uint32_t)pde->value*(gsw->max-gsw->min)/ginputGetDialRange(pde->instance) + gsw->min));
+
+ // Update the display
+ gwinSliderDraw(gh);
+
+ // Generate the event
+ break;
+ #endif
default:
return;
@@ -194,20 +208,22 @@ void gwinSetSliderStyle(GHandle gh, const GSliderDrawStyle *pStyle) {
#undef gsw
}
-static void trackSliderDraw(GHandle gh, coord_t x, coord_t y) {
- #define gsw ((GSliderObject *)gh)
+#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
+ static void trackSliderDraw(GHandle gh, coord_t x, coord_t y) {
+ #define gsw ((GSliderObject *)gh)
- #if GDISP_NEED_CLIP
- gdispSetClip(gh->x, gh->y, gh->width, gh->height);
- #endif
-
- if (gh->height <= gh->width)
- gsw->fn(gh, FALSE, x, &gsw->style, gsw->param);
- else
- gsw->fn(gh, TRUE, y, &gsw->style, gsw->param);
+ #if GDISP_NEED_CLIP
+ gdispSetClip(gh->x, gh->y, gh->width, gh->height);
+ #endif
- #undef gbw
-}
+ if (gh->height <= gh->width)
+ gsw->fn(gh, FALSE, x, &gsw->style, gsw->param);
+ else
+ gsw->fn(gh, TRUE, y, &gsw->style, gsw->param);
+
+ #undef gbw
+ }
+#endif
void gwinSliderDraw(GHandle gh) {
#define gsw ((GSliderObject *)gh)
@@ -267,15 +283,28 @@ void gwinSliderDraw_Std(GHandle gh, bool_t isVertical, coord_t thumbpos, const G
}
}
-#if defined(GINPUT_NEED_MOUSE) && GINPUT_NEED_MOUSE
- bool_t gwinAttachSliderMouseSource(GHandle gh, GSourceHandle gsh) {
- if (gh->type != GW_SLIDER)
+#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
+ bool_t gwinAttachSliderMouse(GHandle gh, uint16_t instance) {
+ GSourceHandle gsh;
+
+ if (gh->type != GW_SLIDER || !(gsh = ginputGetMouse(instance)))
return FALSE;
return geventAttachSource(&((GSliderObject *)gh)->listener, gsh, GLISTEN_MOUSEMETA|GLISTEN_MOUSEDOWNMOVES);
}
#endif
+#if GFX_USE_GINPUT && GINPUT_NEED_DIAL
+ bool_t gwinAttachSliderDial(GHandle gh, uint16_t instance) {
+ GSourceHandle gsh;
+
+ if (gh->type != GW_SLIDER || !(gsh = ginputGetDial(instance)))
+ return FALSE;
+
+ return geventAttachSource(&((GSliderObject *)gh)->listener, gsh, 0);
+ }
+#endif
+
#endif /* GFX_USE_GWIN && GWIN_NEED_BUTTON */
/** @} */