Jeroen Hilgers / Rc6
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Rc6.h Source File

Rc6.h

00001 /* mbed RC6 (Philips remote control protocol) library
00002  * Copyright (c) 2011 Jeroen Hilgers
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022  
00023  #ifndef __RC6_H__
00024  #define __RC6_H__
00025  
00026  #include "mbed.h"
00027 
00028 /** RC6 (Philips remote control) receiver / transmitter library.
00029   *
00030   * Example: Code receiver for 'play/pause' on remote for Philips HDD&DVD recorder.
00031   * 
00032   * Leader  |S3|S2|S1|S0|Togl|A7|A6|A5|A4|A3|A2|A1|A0|D7|D6|D5|D4|D3|D2|D1|D0|
00033   *         |1 |0 |0 |0 |1   |0 |0 |1 |1 |0 |0 |1 |0 |0 |0 |1 |0 |1 |1 |0 |0 |
00034   *         |  |  |  |  |    |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
00035   * ------  |- | -| -| -|--  | -| -|- |- | -| -|- | -| -| -|- | -|- |- | -| -|
00036   *       --| -|- |- |- |  --|- |- | -| -|- |- | -|- |- |- | -|- | -| -|- |- |
00037   *         |  |  |  |  |    |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
00038   *
00039   * The toggle bit is in bit 16, A7-A0 in bits 15-8, D7-D0 in bits 7-0.
00040   * Thus for the example, 0x1322C is returned. S3..S0 are not implemented
00041   * and received / transmitted as shown in the example.
00042   */
00043   
00044  class Rc6Transmitter
00045  {
00046     public:
00047         /** Create a Rc6Transmitter and initizalize it.
00048          *
00049          * @param pin irPin pin connected IR Led (led on on high level).
00050          */
00051         Rc6Transmitter(PinName irPin);
00052 
00053         /** Send a code once. 
00054          * The function will return synchronously and send the code once. 
00055          *
00056          * @param code Code to send.
00057          */
00058         void Send(uint32_t code);
00059         
00060         /** Check if the transmitter is idle.
00061          * 
00062          * @return True if transmitter still busy.
00063          */
00064         bool IsBusy();
00065     
00066     private:
00067         void Tick();
00068     
00069         bool mBusy;
00070         uint16_t mPattern[50];  // 1 leader, 4 startbits, 1 toggle bit, 16 data bits ==>
00071                                 // at most 24 mark / pause durations + 0,0 terminator = 50 entries. 
00072         Ticker mTicker;
00073         uint16_t mMark;          // Tick: Remaining half-cycles of mark.
00074         uint16_t mPause;         // Tick: Remaining half-cycles of pause.        
00075         uint16_t *mPatPtr;       // Transitter pointer.
00076         DigitalOut mOut;     // Pin connected to IR.
00077 };
00078  
00079  class Rc6Receiver
00080  {
00081     public:
00082         /** Create a Rc6Receiver and initizalize it.
00083          *
00084          * @param pin irPin pin connected to IR sensor (low level when IR is received, e.g. TSOP1736).
00085          */
00086         Rc6Receiver(PinName irPin);
00087 
00088         /** Check if anything was received since last poll. Returns 0xFFFFFFFF if not.
00089          *
00090          * @return Last code received or 0xFFFFFFFF if nothing was received since last poll.
00091          */
00092         uint32_t Receive();
00093         
00094     private:
00095         InterruptIn irq;
00096         void OnRise();
00097         void OnFall();
00098         void OnTimeout();
00099         Timeout mTimeout;
00100         Timer mTimer;
00101         bool mBusy;
00102         uint32_t mCode;  
00103         uint32_t mLastCode;
00104  };
00105   
00106  #endif // __RC6_H__