Guillermo Stedile / RA8875

Dependencies:   GPS

Dependents:   SNOCC_V1 SNOCC_V2

Fork of RA8875 by SNOCC

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Sat Oct 04 17:45:50 2014 +0000
Parent:
70:4cb28f9472fc
Child:
72:ecffe56af969
Commit message:
Initial support for the key pad interface where the RA8875 scans the matrix.

Changed in this revision

RA8875.cpp Show annotated file Show diff for this revision Revisions of this file
RA8875.h Show annotated file Show diff for this revision Revisions of this file
--- a/RA8875.cpp	Sun Aug 31 15:58:15 2014 +0000
+++ b/RA8875.cpp	Sat Oct 04 17:45:50 2014 +0000
@@ -238,6 +238,52 @@
 }
 // #### end of touch panel code additions
 
+
+RetCode_t RA8875::KeypadInit(bool scanEnable, bool longDetect, uint8_t sampleTime, uint8_t scanFrequency, 
+    uint8_t longTimeAdjustment, bool interruptEnable, bool wakeupEnable)
+{
+    uint8_t value = 0;
+    
+    if (sampleTime > 3 || scanFrequency > 7 || longTimeAdjustment  > 3)
+        return bad_parameter;
+    value |= (scanEnable) ? 0x80 : 0x00;
+    value |= (longDetect) ? 0x40 : 0x00;
+    value |= (sampleTime & 0x03) << 4;
+    value |= (scanFrequency & 0x07);
+    WriteCommand(0xC0, value);   // Enable Key Scan (and ignore possibility of an error)
+    
+    value = 0;
+    value |= (wakeupEnable) ? 0x80 : 0x00;
+    value |= (longTimeAdjustment & 0x03) << 2;
+    WriteCommand(0xC1, value);  // (and ignore possibility of an error)
+    
+    value = ReadCommand(0xF0);  // (and ignore possibility of an error)
+    value &= ~0x10;
+    value |= (interruptEnable) ? 0x10 : 0x00;
+    return WriteCommand(0xF0, value);
+}
+
+bool RA8875::_kbhit(void)
+{
+    return (ReadCommand(0xF1) & 0x10);  // check KS status - true if kbhit
+}
+
+uint8_t RA8875::_getch(void)
+{
+    while (!_kbhit()) {
+        wait_us(POLLWAITuSec);
+        COUNTIDLETIME(POLLWAITuSec);
+    }
+
+    // read the key press number
+    uint8_t keyNumReg = ReadCommand(0xC1) & 0x03;
+    // read the key code
+    uint8_t keyCode = ReadCommand(0xC2 + keyNumReg);
+    // Clear KS status
+    WriteCommand(0xF1, 0x10);
+    return keyCode;
+}
+
 #ifdef PERF_METRICS
 void RA8875::ClearPerformance()
 {
--- a/RA8875.h	Sun Aug 31 15:58:15 2014 +0000
+++ b/RA8875.h	Sat Oct 04 17:45:50 2014 +0000
@@ -343,7 +343,7 @@
     /// @param[inout] y is the y position where the touch was registered.
     /// @returns true if touch was detected, in which case the x and y values were set.
     ///
-    unsigned char TouchPanelRead(loc_t *x, loc_t *y);
+    uint8_t TouchPanelRead(loc_t *x, loc_t *y);
 
     /// Poll the TouchPanel and on a touch event return the raw x, y coordinates.
     ///
@@ -351,7 +351,7 @@
     /// @param[inout] y is the y position where the touch was registered.
     /// @returns true if touch was detected, in which case the x and y values were set.
     ///
-    unsigned char TouchPanelReadRaw(loc_t *x, loc_t *y);
+    uint8_t TouchPanelReadRaw(loc_t *x, loc_t *y);
     
     /// Append interrupt handler for specific RA8875 interrupt source
     ///
@@ -378,6 +378,64 @@
     ///
     void UnAppendISR(uint8_t bISRType);
 
+
+    /// Initialize the keypad interface on the RA8875 controller.
+    ///
+    /// Enables the keypad subsystem. It will scan the 4 x 5 matrix
+    /// and make available key presses. 
+    ///
+    /// @note See section 5-13 of RAIO RA8875 data sheet for more details.
+    /// @caution When using the display from buy-display.com, be sure that
+    ///     the option for the keypad is configured on the hardware.
+    ///
+    /// All parameters are optional.
+    /// @param[in] scanEnable, when true, enables the key scan function (default: true).
+    /// @param[in] longDetect, when true, additionally enables the long key held detection (default: false).
+    /// @param[in] sampleTime setting (range: 0 - 3, default: 0).
+    /// @param[in] scanFrequency setting (range: 0 - 7, default: 0).
+    /// @param[in] longTimeAdjustment (range: 0 - 3, default: 0).
+    /// @param[in] interruptEnable, when true, enables interrupts from keypress (default: false).
+    /// @param[in] wakeupEnable, when true, activates the wakeup function (default: false).
+    ///
+    /// @returns success/failure code. @see RetCode_t.
+    ///
+    RetCode_t  KeypadInit(bool scanEnable = true, bool longDetect = false, 
+        uint8_t sampleTime = 0, uint8_t scanFrequency = 0, 
+        uint8_t longTimeAdjustment = 0,
+        bool interruptEnable = false, bool wakeupEnable = false);
+
+    /// Determine if a key has been hit
+    ///
+    /// @returns true if a key has been hit
+    ///
+    bool _kbhit();
+
+    /// Blocking read of the keypad.
+    ///
+    /// @caution: This is a blocking read, so it is important to first call _kbhit()
+    ///         to avoid hanging your processes.
+    ///
+    /// A keypad connected to the RA8875 is connected in a matrix of 4 rows and 5 columns.
+    /// The row where a key is pressed is returned in the lower 2 bits of the upper nibble,
+    /// and the column where a key is pressed is returned in the lower 3 bits of the lower nibble.
+    ///
+    /// Additionally, if configured to detect a "long press", bit 7 will be set to indicate
+    /// this. In this situation, first a "normal press" would be detected and signaled and
+    /// soon after that a "long press" of the same key would be detected and communicated.
+    ///
+    /// Return value encoding: LxRR xCCC
+    /// L = Long Press was detected
+    /// RR = Row number 0 to 3
+    ///
+    /// @return 8-bit value coded as L0RR 0CCC:
+    ///     - L = 1 = Long Press detected
+    ///     - x = 0 = not used
+    ///     - R = 0-3 = Row Number
+    ///     - C = 0-4 = Column Number
+    ///
+    uint8_t _getch();
+
+
     /// Write a command to the display with a word of data.
     ///
     /// This is a high level command, and may invoke several primitives.