diff --git a/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ClickButton.cpp b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ClickButton.cpp new file mode 100644 index 0000000..c4b08d0 --- /dev/null +++ b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ClickButton.cpp @@ -0,0 +1,150 @@ +/* ClickButton + + Arduino library that decodes multiple clicks on one button. + Also copes with long clicks and click-and-hold. + + Usage: ClickButton buttonObject(pin [LOW/HIGH, [CLICKBTN_PULLUP]]); + + where LOW/HIGH denotes active LOW or HIGH button (default is LOW) + CLICKBTN_PULLUP is only possible with active low buttons. + + + Returned click counts: + + A positive number denotes the number of (short) clicks after a released button + A negative number denotes the number of "long" clicks + +NOTE! + This is the OPPOSITE/negative of click codes from the last pre-2013 versions! + (this seemed more logical and simpler, so I finally changed it) + + Based on the Debounce example at arduino playground site + + + Copyright (C) 2010,2012, 2013 raron + + + GNU GPLv3 license + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + + + Contact: raronzen@gmail.com + + + History: + 2013.08.29 - Some small clean-up of code, more sensible variable names etc. + Added another example code for multiple buttons in an object array + 2013.04.23 - A "minor" debugging: active-high buttons now work (wops)! + Thanks goes to John F. H. for pointing that out! + 2013.02.17 - Some improvements, simplified click codes. + Added a LED fader example. Thanks to Tom K. for the idea. + 2012.01.31 - Tiny update for Arduino 1.0 + 2010.06.15 - First version. Basically just a small OOP programming exercise. +*/ + +#include "ClickButton.h" + +ClickButton::ClickButton(uint8_t buttonPin) +{ + _pin = buttonPin; + _activeHigh = LOW; // Assume active-low button + _btnState = !_activeHigh; // initial button state in active-high logic + _lastState = _btnState; + _clickCount = 0; + clicks = 0; + depressed = false; + _lastBounceTime= 0; + debounceTime = 20; // Debounce timer in ms + multiclickTime = 250; // Time limit for multi clicks + longClickTime = 1000; // time until long clicks register + pinMode(_pin, INPUT); +} + + +ClickButton::ClickButton(uint8_t buttonPin, boolean activeType) +{ + _pin = buttonPin; + _activeHigh = activeType; + _btnState = !_activeHigh; // initial button state in active-high logic + _lastState = _btnState; + _clickCount = 0; + clicks = 0; + depressed = 0; + _lastBounceTime= 0; + debounceTime = 20; // Debounce timer in ms + multiclickTime = 250; // Time limit for multi clicks + longClickTime = 1000; // time until long clicks register + pinMode(_pin, INPUT); +} + +ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internalPullup) +{ + _pin = buttonPin; + _activeHigh = activeType; + _btnState = !_activeHigh; // initial button state in active-high logic + _lastState = _btnState; + _clickCount = 0; + clicks = 0; + depressed = 0; + _lastBounceTime= 0; + debounceTime = 20; // Debounce timer in ms + multiclickTime = 250; // Time limit for multi clicks + longClickTime = 1000; // time until "long" click register + pinMode(_pin, INPUT); + // Turn on internal pullup resistor if applicable + if (_activeHigh == LOW && internalPullup == CLICKBTN_PULLUP) digitalWrite(_pin,HIGH); +} + + + +void ClickButton::Update() +{ + long now = (long)millis(); // get current time + _btnState = digitalRead(_pin); // current appearant button state + + // Make the button logic active-high in code + if (!_activeHigh) _btnState = !_btnState; + + // If the switch changed, due to noise or a button press, reset the debounce timer + if (_btnState != _lastState) _lastBounceTime = now; + + + // debounce the button (Check if a stable, changed state has occured) + if (now - _lastBounceTime > debounceTime && _btnState != depressed) + { + depressed = _btnState; + if (depressed) _clickCount++; + } + + // If the button released state is stable, report nr of clicks and start new cycle + if (!depressed && (now - _lastBounceTime) > multiclickTime) + { + // positive count for released buttons + clicks = _clickCount; + _clickCount = 0; + } + + // Check for "long click" + if (depressed && (now - _lastBounceTime > longClickTime)) + { + // negative count for long clicks + clicks = 0 - _clickCount; + _clickCount = 0; + } + + _lastState = _btnState; +} + diff --git a/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ClickButton.h b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ClickButton.h new file mode 100644 index 0000000..ac3dbf0 --- /dev/null +++ b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ClickButton.h @@ -0,0 +1,36 @@ +#ifndef ClickButton_H +#define ClickButton_H + +#if (ARDUINO < 100) +#include +#else +#include +#endif + + +#define CLICKBTN_PULLUP HIGH + + +class ClickButton +{ + public: + ClickButton(uint8_t buttonPin); + ClickButton(uint8_t buttonPin, boolean active); + ClickButton(uint8_t buttonPin, boolean active, boolean internalPullup); + void Update(); + int clicks; // button click counts to return + boolean depressed; // the currently debounced button (press) state (presumably it is not sad :) + long debounceTime; + long multiclickTime; + long longClickTime; + private: + uint8_t _pin; // Arduino pin connected to the button + boolean _activeHigh; // Type of button: Active-low = 0 or active-high = 1 + boolean _btnState; // Current appearant button state + boolean _lastState; // previous button reading + int _clickCount; // Number of button clicks within multiclickTime milliseconds + long _lastBounceTime; // the last time the button input pin was toggled, due to noise or a press +}; + +#endif + diff --git a/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/DDS-AD9910-Arduino-Shield.ino.ino b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/DDS-AD9910-Arduino-Shield.ino similarity index 99% rename from Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/DDS-AD9910-Arduino-Shield.ino.ino rename to Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/DDS-AD9910-Arduino-Shield.ino index 7d23e55..42a2aac 100644 --- a/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/DDS-AD9910-Arduino-Shield.ino.ino +++ b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/DDS-AD9910-Arduino-Shield.ino @@ -941,7 +941,7 @@ void LoadMain() } /* - * Расчет корректировки в dBm используется только для AM модуляции, значение зависит ТОЛЬКО от глубины модуляции + * Calculation of the correction in dBm is used only for AM modulation, the value depends ONLY on the modulation depth */ int CalcDBCorrection() { diff --git a/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ad9910.cpp b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ad9910.cpp index 29ad347..2d3f1a0 100644 --- a/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ad9910.cpp +++ b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/ad9910.cpp @@ -340,7 +340,8 @@ void SaveAMWavesToRAM(uint32_t F_carrier, uint32_t F_mod, uint32_t AM_DEPH, int1 //Rad = Deg * 0.01745; // conversion from degrees to radians RAD=DEG*Pi/180 Rad = Deg * PI/180.0; Sin = sin(Rad); // Get Sinus - Amplitude_AM = MaxAmplitudeValue - (((MaxAmplitudeValue * (1 + Sin)) / 2) * (AM_DEPH/100.0)); +// Amplitude_AM = MaxAmplitudeValue - (((MaxAmplitudeValue * (1 + Sin)) / 2) * (AM_DEPH/100.0)); + Amplitude_AM = MaxAmplitudeValue * (1+Sin*AM_DEPH/100.0) / (1 + AM_DEPH/100.0); #if DBG==1 //Serial.print("Amplitude_AM="); Serial.println(Amplitude_AM); diff --git a/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/main.h b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/main.h index 7708133..d59f903 100644 --- a/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/main.h +++ b/Firmware Source (.INO File)/DDS-AD9910-Arduino-Shield/main.h @@ -8,7 +8,7 @@ #include #include -#include +#include "ClickButton.h" #include "AD9910.h" #include "menuclk.h"