Silicon Labs / EFM32_CapSenseSlider

Dependents:   EFM32 RDA5807M RDS Radio EMF32-Segment-Touch-Demo EFM32_Bugs MFALHIMOHAMMED ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EFM32_CapSenseSlider.cpp Source File

EFM32_CapSenseSlider.cpp

Go to the documentation of this file.
00001 /***************************************************************************//**
00002  * @file EFM32_CapSenseSlider.cpp
00003  * @brief Driver class for the capacitive touch slider on some EFM32 STK's.
00004  *******************************************************************************
00005  * @section License
00006  * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
00007  *******************************************************************************
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  * 1. The origin of this software must not be misrepresented; you must not
00014  *    claim that you wrote the original software.
00015  * 2. Altered source versions must be plainly marked as such, and must not be
00016  *    misrepresented as being the original software.
00017  * 3. This notice may not be removed or altered from any source distribution.
00018  *
00019  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
00020  * obligation to support this Software. Silicon Labs is providing the
00021  * Software "AS IS", with no express or implied warranties of any kind,
00022  * including, but not limited to, any implied warranties of merchantability
00023  * or fitness for any particular purpose or warranties against infringement
00024  * of any proprietary rights of a third party.
00025  *
00026  * Silicon Labs will not be liable for any consequential, incidental, or
00027  * special damages, or any other relief, or for any claim by any third party,
00028  * arising from your use of this Software.
00029  *
00030  ******************************************************************************/
00031 
00032 #include <mbed.h>
00033 #include "platform.h"
00034 
00035 #include "EFM32_CapSenseSlider.h"
00036 #include "em_lesense.h"
00037 
00038 namespace silabs {
00039     /*
00040      * Constructor.
00041      */
00042     EFM32_CapSenseSlider::EFM32_CapSenseSlider() :
00043         _channelCallback(this, &EFM32_CapSenseSlider::channelCallbackHandler),
00044         _scanCallback(this, &EFM32_CapSenseSlider::scanCallbackHandler)
00045     {
00046         _touchCb = NULL;
00047         _untouchCb = NULL;
00048         _slideCb = NULL;
00049         _trippingPoint = 0;
00050         _lastValue = -1;
00051         _position = -1;
00052         _touched = false;
00053         _running = false;
00054     }
00055 
00056     /*
00057      * Start measuring
00058      */
00059     void EFM32_CapSenseSlider::start() {
00060         if(_running == false) {
00061             CAPLESENSE_Init(true);
00062             CAPLESENSE_setupCallbacks((cbptr_t)_scanCallback.entry(), (cbptr_t)_channelCallback.entry());
00063             blockSleepMode(EM2);
00064             _running = true;
00065         }
00066     }
00067 
00068     /*
00069      * Stop measuring
00070      */
00071     void EFM32_CapSenseSlider::stop() {
00072         if(_running == true) {
00073             LESENSE_ScanStop();
00074             unblockSleepMode(EM2);
00075             _running = false;
00076         }
00077     }
00078 
00079     /*
00080      * Attach a callback handler, which gets called once on touch
00081      * callback: pointer to a void (void) function. If null, then the callback gets disabled.
00082      */
00083     void EFM32_CapSenseSlider::attach_touch(cbptr_t callback) {
00084         _touchCb = callback;
00085     }
00086 
00087     /*
00088      * Attach a callback handler, which gets called once on releasing touch
00089      * callback: pointer to a void (void) function. If null, then the callback gets disabled.
00090      */
00091     void EFM32_CapSenseSlider::attach_untouch(cbptr_t callback) {
00092         _untouchCb = callback;
00093     }
00094 
00095     /*
00096      * Attach a callback which will trigger once the slider value passes a certain point.
00097      *
00098      * trip: point accross which the callback gets called.
00099      * callback: pointer to a void (void) function. If null, then the callback gets disabled.
00100      */
00101     void EFM32_CapSenseSlider::attach_slide(int32_t trip, cbptr_t callback) {
00102         _slideCb = callback;
00103         _trippingPoint = trip;
00104     }
00105 
00106     /*
00107      * Check whether the slider is currently being touched.
00108      */
00109     bool EFM32_CapSenseSlider::isTouched() {
00110         return _touched;
00111     }
00112 
00113     /*
00114      * Get the current position
00115      */
00116     int32_t EFM32_CapSenseSlider::get_position() {
00117         return _position;
00118     }
00119 
00120     void EFM32_CapSenseSlider::channelCallbackHandler(void) {
00121         /* When a touch is detected, go to responsive scan mode */
00122         CAPLESENSE_setupLESENSE(false);
00123     }
00124 
00125     void EFM32_CapSenseSlider::scanCallbackHandler(void) {
00126         /* Calculate slider position */
00127         _position = CAPLESENSE_getSliderPosition();
00128         /* Check for touch */
00129         if(_position < 0) {
00130             /* Slider is no longer being touched */
00131             if(_touched == true) {
00132                 _touched = false;
00133                 if(_untouchCb != NULL) _untouchCb();
00134             }
00135             /* When no longer touched, go to sense mode */
00136             CAPLESENSE_setupLESENSE(true);
00137             return;
00138         }
00139 
00140         /* Touched, check if this is the first touch */
00141         if(_touched == false) {
00142             _touched = true;
00143             if(_touchCb != NULL) _touchCb();
00144         }
00145 
00146         /* Check if we tripped the threshold */
00147         if((_lastValue != _position) && (_slideCb != NULL)) {
00148             if((_trippingPoint == -1) || (_position >= _trippingPoint)) _slideCb();
00149         }
00150 
00151         _lastValue = _position;
00152     }
00153 }