/* Copyright 2010 GHI Electronics LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; namespace GHIElectronics.NETMF.FEZ { public static partial class FEZ_Components { public class EasyRemote : IDisposable { InterruptPort IRM; uint bit_count; long last_tick; long[] bit_time = new long[10]; public void Dispose() { IRM.Dispose(); } // Initial state private ButtonState PreState_A = ButtonState.B; private ButtonState PreState_B = ButtonState.A; private ButtonState PreState_C = ButtonState.B; private ButtonState PreState_D = ButtonState.B; private enum ButtonState { A,a, B,b, C,c, D,d, } public enum Button { A, B, C, D, } public EasyRemote(FEZ_Pin.Interrupt pin) { IRM = new InterruptPort((Cpu.Pin)pin, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); IRM.OnInterrupt += new NativeEventHandler(IRM_OnInterrupt); last_tick = DateTime.Now.Ticks; } public delegate void ButtonPressEventHandler(Button button, bool new_press); public event ButtonPressEventHandler ButtonPressEvent = delegate { }; void IRM_OnInterrupt(uint port, uint state, DateTime time) { bit_time[bit_count]= time.Ticks-last_tick; last_tick = time.Ticks; // if long time has passed then dum what we have if (bit_time[bit_count] > 400000) bit_count = 0; //get ready for the next bit bit_count++; // check button every 10 bits if (bit_count >= 10) { int i; /* for (i = 0; i < 10; i++) { Debug.Print(bit_time[i].ToString()); } //*/ for (i = 0; i < 5; i++) { //check up to teh first 3 bits if (bit_time[i] > 100000) { if (bit_time[i + 1] > (8373 * 0.9) && bit_time[i + 1] < (8373 * 1.1) && bit_time[i + 2] > (8338 * 0.9) && bit_time[i + 2] < (8338 * 1.1) && bit_time[i + 3] > (34100 * 0.9) && bit_time[i + 3] < (34100 * 1.1)) { // button A //Debug.Print("A"); if (PreState_A == ButtonState.A) ButtonPressEvent(Button.A,false); else ButtonPressEvent(Button.A, true); PreState_A = ButtonState.A; } else if (bit_time[i + 1] > (8373 * 0.9) && bit_time[i + 1] < (8373 * 1.1) && bit_time[i + 2] > (8338 * 0.9) && bit_time[i + 2] < (8338 * 1.1) && bit_time[i + 3] > (42410 * 0.9) && bit_time[i + 3] < (42410 * 1.1)) { // button ~A //Debug.Print("a"); if (PreState_A == ButtonState.a) ButtonPressEvent(Button.A, false); else ButtonPressEvent(Button.A, true); PreState_A = ButtonState.a; } else if (bit_time[i + 1] > (17000 * 0.9) && bit_time[i + 1] < (17000 * 1.1) && bit_time[i + 2] > (8100 * 0.9) && bit_time[i + 2] < (8100 * 1.1) && bit_time[i + 3] > (25300 * 0.9) && bit_time[i + 3] < (25300 * 1.1) ) { // button B //Debug.Print("B"); if (PreState_B == ButtonState.B) ButtonPressEvent(Button.B, false); else ButtonPressEvent(Button.B, true); PreState_B = ButtonState.B; } else if (bit_time[i + 1] > (17000 * 0.9) && bit_time[i + 1] < (17000 * 1.1) && bit_time[i + 2] > (7900 * 0.9) && bit_time[i + 2] < (7900 * 1.1) && bit_time[i + 3] > (34000 * 0.9) && bit_time[i + 3] < (34000 * 1.1) ) { // button ~B //Debug.Print("b"); if (PreState_B == ButtonState.b) ButtonPressEvent(Button.B, false); else ButtonPressEvent(Button.B, true); PreState_B = ButtonState.b; } else if (bit_time[i + 1] > (8850 * 0.9) && bit_time[i + 1] < (8850 * 1.1) && bit_time[i + 2] > (16150 * 0.9) && bit_time[i + 2] < (16150 * 1.1) && bit_time[i + 3] > (25620 * 0.9) && bit_time[i + 3] < (25620 * 1.1)) { // button C //Debug.Print("C"); if (PreState_C == ButtonState.C) ButtonPressEvent(Button.C, false); else ButtonPressEvent(Button.C, true); PreState_C = ButtonState.C; } else if (bit_time[i + 1] > (8390 * 0.9) && bit_time[i + 1] < (8390 * 1.1) && bit_time[i + 2] > (16550 * 0.9) && bit_time[i + 2] < (16550 * 1.1) && bit_time[i + 3] > (33500 * 0.9) && bit_time[i + 3] < (33500 * 1.1) ) { // button ~C //Debug.Print("c"); if (PreState_C == ButtonState.c) ButtonPressEvent(Button.C, false); else ButtonPressEvent(Button.C, true); PreState_C = ButtonState.c; } else if (bit_time[i + 1] > (25830 * 0.9) && bit_time[i + 1] < (25830 * 1.1) && bit_time[i + 2] > (7600 * 0.9) && bit_time[i + 2] < (7600 * 1.1) && bit_time[i + 3] > (25830 * 0.9) && bit_time[i + 3] < (25830 * 1.1)) { // button D //Debug.Print("D"); if (PreState_D == ButtonState.D) ButtonPressEvent(Button.D, false); else ButtonPressEvent(Button.D, true); PreState_D = ButtonState.D; } else if (bit_time[i + 1] > (25830 * 0.9) && bit_time[i + 1] < (25830 * 1.1) && bit_time[i + 2] > (7600 * 0.9) && bit_time[i + 2] < (7600 * 1.1) && bit_time[i + 3] > (17400 * 0.9) && bit_time[i + 3] < (17400 * 1.1) ) { // button ~D //Debug.Print("d"); if (PreState_D == ButtonState.d) ButtonPressEvent(Button.D, false); else ButtonPressEvent(Button.D, true); PreState_D = ButtonState.d; } } } // reset the counter bit_count = 0; } } } } }