Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
InterruptIn.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "InterruptIn.h" 00017 00018 #if DEVICE_INTERRUPTIN 00019 00020 static int button_buf[5] = {0}; 00021 static int inc = 0; 00022 00023 namespace mbed { 00024 00025 InterruptIn::InterruptIn(PinName pin) : gpio(), 00026 gpio_irq(), 00027 _rise(), 00028 _fall() { 00029 gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this); 00030 gpio_init_in(&gpio, pin); 00031 } 00032 00033 InterruptIn::~InterruptIn() { 00034 gpio_irq_free(&gpio_irq); 00035 } 00036 00037 int InterruptIn::read() { 00038 return gpio_read(&gpio); 00039 } 00040 00041 void InterruptIn::mode(PinMode pull) { 00042 gpio_mode(&gpio, pull); 00043 } 00044 00045 void InterruptIn::rise(void (*fptr)(void)) { 00046 if (fptr) { 00047 _rise.attach(fptr); 00048 gpio_irq_set(&gpio_irq, IRQ_RISE, 1); 00049 } else { 00050 gpio_irq_set(&gpio_irq, IRQ_RISE, 0); 00051 } 00052 } 00053 00054 void InterruptIn::fall(void (*fptr)(void)) { 00055 if (fptr) { 00056 _fall.attach(fptr); 00057 gpio_irq_set(&gpio_irq, IRQ_FALL, 1); 00058 } else { 00059 gpio_irq_set(&gpio_irq, IRQ_FALL, 0); 00060 } 00061 } 00062 00063 void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event) { 00064 InterruptIn *handler = (InterruptIn*)id; 00065 switch (event) { 00066 case IRQ_RISE: handler->_rise.call(); break; 00067 case IRQ_FALL: handler->_fall.call(); break; 00068 case IRQ_NONE: break; 00069 } 00070 } 00071 00072 void InterruptIn::enable_irq() { 00073 gpio_irq_enable(&gpio_irq); 00074 } 00075 00076 void InterruptIn::disable_irq() { 00077 gpio_irq_disable(&gpio_irq); 00078 } 00079 00080 #ifdef MBED_OPERATORS 00081 InterruptIn::operator int() { 00082 00083 button_buf[inc++] = read(); 00084 if (inc == 5) inc = 0; 00085 if (button_buf[0] == button_buf[1] && 00086 button_buf[1] == button_buf[2] && 00087 button_buf[2] == button_buf[3] && 00088 button_buf[3] == button_buf[4]){ 00089 00090 return button_buf[0]; 00091 } else { 00092 return 1; 00093 } 00094 } 00095 #endif 00096 00097 } // namespace mbed 00098 00099 #endif
Generated on Tue Jul 12 2022 20:15:32 by
1.7.2
