AppleRemote receive library.

Committer:
tosihisa
Date:
Sat Oct 09 09:39:45 2010 +0000
Revision:
1:6d06459fb6db
Parent:
0:a5f26ed3f510
Release for library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tosihisa 0:a5f26ed3f510 1 /**
tosihisa 0:a5f26ed3f510 2 * @section LICENSE
tosihisa 0:a5f26ed3f510 3 *
tosihisa 0:a5f26ed3f510 4 * This source code is "zlib" license.
tosihisa 0:a5f26ed3f510 5 * Of course, this is not a part of the zlib software.
tosihisa 0:a5f26ed3f510 6 * The license is zlib license.
tosihisa 0:a5f26ed3f510 7 *
tosihisa 0:a5f26ed3f510 8 * Copyright (C) 2010 tosihisa
tosihisa 0:a5f26ed3f510 9 *
tosihisa 0:a5f26ed3f510 10 * This software is provided 'as-is', without any express or implied
tosihisa 0:a5f26ed3f510 11 * warranty. In no event will the authors be held liable for any damages
tosihisa 0:a5f26ed3f510 12 * arising from the use of this software.
tosihisa 0:a5f26ed3f510 13 *
tosihisa 0:a5f26ed3f510 14 * Permission is granted to anyone to use this software for any purpose,
tosihisa 0:a5f26ed3f510 15 * including commercial applications, and to alter it and redistribute it
tosihisa 0:a5f26ed3f510 16 * freely, subject to the following restrictions:
tosihisa 0:a5f26ed3f510 17 *
tosihisa 0:a5f26ed3f510 18 * 1. The origin of this software must not be misrepresented; you must not
tosihisa 0:a5f26ed3f510 19 * claim that you wrote the original software. If you use this software
tosihisa 0:a5f26ed3f510 20 * in a product, an acknowledgment in the product documentation would be
tosihisa 0:a5f26ed3f510 21 * appreciated but is not required.
tosihisa 0:a5f26ed3f510 22 * 2. Altered source versions must be plainly marked as such, and must not be
tosihisa 0:a5f26ed3f510 23 * misrepresented as being the original software.
tosihisa 0:a5f26ed3f510 24 * 3. This notice may not be removed or altered from any source distribution.
tosihisa 0:a5f26ed3f510 25 *
tosihisa 0:a5f26ed3f510 26 * @section DESCRIPTION
tosihisa 0:a5f26ed3f510 27 *
tosihisa 0:a5f26ed3f510 28 * Apple Remote Decoder
tosihisa 0:a5f26ed3f510 29 * http://en.wikipedia.org/wiki/Apple_Remote
tosihisa 0:a5f26ed3f510 30 *
tosihisa 0:a5f26ed3f510 31 */
tosihisa 0:a5f26ed3f510 32
tosihisa 0:a5f26ed3f510 33 #include "mbed.h"
tosihisa 0:a5f26ed3f510 34 #include "AppleRemote.h"
tosihisa 0:a5f26ed3f510 35
tosihisa 0:a5f26ed3f510 36 AppleRemote::AppleRemote( PinName _ir ) : IR(_ir) , nowStart(0) , _repeatEnable(0)
tosihisa 0:a5f26ed3f510 37 {
tosihisa 0:a5f26ed3f510 38 reset();
tosihisa 0:a5f26ed3f510 39 stop();
tosihisa 0:a5f26ed3f510 40 }
tosihisa 0:a5f26ed3f510 41
tosihisa 0:a5f26ed3f510 42 void AppleRemote::reset()
tosihisa 0:a5f26ed3f510 43 {
tosihisa 0:a5f26ed3f510 44 IR_Timer.reset();
tosihisa 0:a5f26ed3f510 45 cjobst = WAIT_READER;
tosihisa 0:a5f26ed3f510 46 bit_index = 0;
tosihisa 0:a5f26ed3f510 47 pre_IR_code = IR_code = 0;
tosihisa 0:a5f26ed3f510 48 pre_timer = 0;
tosihisa 0:a5f26ed3f510 49 }
tosihisa 0:a5f26ed3f510 50
tosihisa 0:a5f26ed3f510 51 void AppleRemote::start()
tosihisa 0:a5f26ed3f510 52 {
tosihisa 0:a5f26ed3f510 53 if(nowStart == 0){
tosihisa 0:a5f26ed3f510 54 reset();
tosihisa 0:a5f26ed3f510 55 IR.fall(this,&AppleRemote::IR_fall);
tosihisa 0:a5f26ed3f510 56 IR.rise(this,&AppleRemote::IR_rise);
tosihisa 0:a5f26ed3f510 57 IR_Timer.start();
tosihisa 0:a5f26ed3f510 58 nowStart = 1;
tosihisa 0:a5f26ed3f510 59 }
tosihisa 0:a5f26ed3f510 60 }
tosihisa 0:a5f26ed3f510 61
tosihisa 0:a5f26ed3f510 62 void AppleRemote::stop()
tosihisa 0:a5f26ed3f510 63 {
tosihisa 0:a5f26ed3f510 64 nowStart = 0;
tosihisa 0:a5f26ed3f510 65 IR_Timer.stop();
tosihisa 0:a5f26ed3f510 66 IR.fall(NULL);
tosihisa 0:a5f26ed3f510 67 IR.rise(NULL);
tosihisa 0:a5f26ed3f510 68 }
tosihisa 0:a5f26ed3f510 69
tosihisa 0:a5f26ed3f510 70 int AppleRemote::readable()
tosihisa 0:a5f26ed3f510 71 {
tosihisa 0:a5f26ed3f510 72 return (widx == ridx) ? 0 : 1;
tosihisa 0:a5f26ed3f510 73 }
tosihisa 0:a5f26ed3f510 74
tosihisa 0:a5f26ed3f510 75 int AppleRemote::getc()
tosihisa 0:a5f26ed3f510 76 {
tosihisa 0:a5f26ed3f510 77 int retval = -1;
tosihisa 0:a5f26ed3f510 78 if(readable()){
tosihisa 0:a5f26ed3f510 79 retval = static_cast<unsigned short>(buf[ridx]);
tosihisa 0:a5f26ed3f510 80 ridx = (ridx + 1) & (bufsize-1);
tosihisa 0:a5f26ed3f510 81 }
tosihisa 0:a5f26ed3f510 82 return retval;
tosihisa 0:a5f26ed3f510 83 }
tosihisa 0:a5f26ed3f510 84
tosihisa 0:a5f26ed3f510 85 void AppleRemote::repeat(int enable)
tosihisa 0:a5f26ed3f510 86 {
tosihisa 0:a5f26ed3f510 87 _repeatEnable = enable;
tosihisa 0:a5f26ed3f510 88 }
tosihisa 0:a5f26ed3f510 89
tosihisa 0:a5f26ed3f510 90 void AppleRemote::IR_fall()
tosihisa 0:a5f26ed3f510 91 {
tosihisa 0:a5f26ed3f510 92 int us;
tosihisa 0:a5f26ed3f510 93 if(cjobst == WAIT_READER){
tosihisa 0:a5f26ed3f510 94 IR_Timer.reset();
tosihisa 0:a5f26ed3f510 95 pre_timer = 0;
tosihisa 0:a5f26ed3f510 96 cjobst = CHK_READER_1ST;
tosihisa 0:a5f26ed3f510 97 } else if(cjobst == CHK_READER_2ND){
tosihisa 0:a5f26ed3f510 98 us = IR_Timer.read_us();
tosihisa 0:a5f26ed3f510 99 if(((us - pre_timer) > 4400) && ((us - pre_timer) < 4600)){
tosihisa 0:a5f26ed3f510 100 //printf("Get Reader-code\n");
tosihisa 0:a5f26ed3f510 101 cjobst = CHK_CODE_1ST;
tosihisa 0:a5f26ed3f510 102 pre_timer = us;
tosihisa 0:a5f26ed3f510 103 } else if(((us - pre_timer) > 1900) && ((us - pre_timer) < 2350)){
tosihisa 0:a5f26ed3f510 104 //printf("Get Repeat-code\n");
tosihisa 0:a5f26ed3f510 105 if((_repeatEnable != 0) && (pre_IR_code != 0)){
tosihisa 0:a5f26ed3f510 106 if((widx + 1) != ridx){
tosihisa 0:a5f26ed3f510 107 buf[widx] = static_cast<unsigned short>(pre_IR_code >> 16);
tosihisa 0:a5f26ed3f510 108 widx = (widx + 1) & (bufsize-1);
tosihisa 0:a5f26ed3f510 109 }
tosihisa 0:a5f26ed3f510 110 }
tosihisa 0:a5f26ed3f510 111 cjobst = WAIT_READER;
tosihisa 0:a5f26ed3f510 112 } else {
tosihisa 0:a5f26ed3f510 113 cjobst = WAIT_READER;
tosihisa 0:a5f26ed3f510 114 }
tosihisa 0:a5f26ed3f510 115 } else if(cjobst == CHK_CODE_2ND){
tosihisa 0:a5f26ed3f510 116 us = IR_Timer.read_us();
tosihisa 0:a5f26ed3f510 117 if((us - pre_timer) > 3000){
tosihisa 0:a5f26ed3f510 118 /* Error. too long (over 3msec). */
tosihisa 0:a5f26ed3f510 119 cjobst = WAIT_READER;
tosihisa 0:a5f26ed3f510 120 } else {
tosihisa 0:a5f26ed3f510 121 IR_code |= (((us - pre_timer) < (1125+(1125/2))) ? (0) : (1)) << bit_index;
tosihisa 0:a5f26ed3f510 122 bit_index++;
tosihisa 0:a5f26ed3f510 123 pre_timer = us;
tosihisa 0:a5f26ed3f510 124 cjobst = CHK_CODE_1ST;
tosihisa 0:a5f26ed3f510 125 if(bit_index >= 32){
tosihisa 0:a5f26ed3f510 126 cjobst = CHK_STOP_1ST;
tosihisa 0:a5f26ed3f510 127 }
tosihisa 0:a5f26ed3f510 128 }
tosihisa 0:a5f26ed3f510 129 } else if(cjobst == CHK_STOP_2ND){
tosihisa 0:a5f26ed3f510 130 //printf("CODE:[%08lX]\n",IR_code);
tosihisa 0:a5f26ed3f510 131 if((IR_code & 0xffff) == ID){
tosihisa 0:a5f26ed3f510 132 if((widx + 1) != ridx){
tosihisa 0:a5f26ed3f510 133 buf[widx] = static_cast<unsigned short>(IR_code >> 16);
tosihisa 0:a5f26ed3f510 134 widx = (widx + 1) & (bufsize-1);
tosihisa 0:a5f26ed3f510 135 }
tosihisa 0:a5f26ed3f510 136 pre_IR_code = IR_code;
tosihisa 0:a5f26ed3f510 137 }
tosihisa 0:a5f26ed3f510 138 cjobst = WAIT_READER;
tosihisa 0:a5f26ed3f510 139 bit_index = 0;
tosihisa 0:a5f26ed3f510 140 IR_code = 0;
tosihisa 0:a5f26ed3f510 141 }
tosihisa 0:a5f26ed3f510 142 }
tosihisa 0:a5f26ed3f510 143
tosihisa 0:a5f26ed3f510 144 void AppleRemote::IR_rise()
tosihisa 0:a5f26ed3f510 145 {
tosihisa 0:a5f26ed3f510 146 int us;
tosihisa 0:a5f26ed3f510 147 if(cjobst == CHK_READER_1ST){
tosihisa 0:a5f26ed3f510 148 us = IR_Timer.read_us();
tosihisa 0:a5f26ed3f510 149 if((us > 8800) && (us < 9500)){
tosihisa 0:a5f26ed3f510 150 cjobst = CHK_READER_2ND;
tosihisa 0:a5f26ed3f510 151 pre_timer = us;
tosihisa 0:a5f26ed3f510 152 } else {
tosihisa 0:a5f26ed3f510 153 cjobst = WAIT_READER;
tosihisa 0:a5f26ed3f510 154 }
tosihisa 0:a5f26ed3f510 155 } else if(cjobst == CHK_CODE_1ST){
tosihisa 0:a5f26ed3f510 156 cjobst = CHK_CODE_2ND;
tosihisa 0:a5f26ed3f510 157 } else if(cjobst == CHK_STOP_1ST){
tosihisa 0:a5f26ed3f510 158 cjobst = CHK_STOP_2ND;
tosihisa 0:a5f26ed3f510 159 } else {
tosihisa 0:a5f26ed3f510 160 cjobst = WAIT_READER;
tosihisa 0:a5f26ed3f510 161 }
tosihisa 0:a5f26ed3f510 162 }
tosihisa 0:a5f26ed3f510 163