Skip to content

Commit bc05681

Browse files
committed
Refactoring
1 parent 05a0a3a commit bc05681

File tree

8 files changed

+252
-80
lines changed

8 files changed

+252
-80
lines changed

examples/WhistleSwitch/ATtinyUtils.cpp

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
22
* ATtinyUtils.cpp
33
*
4-
* Copyright (C) 2018-2020 Armin Joachimsmeyer
4+
* Copyright (C) 2016-2020 Armin Joachimsmeyer
55
* Email: armin.joachimsmeyer@gmail.com
66
*
7-
* This file is part of ArduinoUtils https://github.com/ArminJo/ArduinoUtils.
7+
* This file is part of Arduino-Utils https://github.com/ArminJo/Arduino-Utils.
88
*
99
* ArduinoUtils is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU General Public License as published by
@@ -61,39 +61,8 @@ inline void pinModeFastPortB(uint8_t aOutputPinNumber, uint8_t aMode) {
6161
(aMode ? DDRB |= (1 << aOutputPinNumber) /* OUTPUT */: DDRB &= ~(1 << aOutputPinNumber));
6262
}
6363

64-
void __attribute__((weak)) delayMilliseconds(unsigned int aMillis) {
65-
for (unsigned int i = 0; i < aMillis; ++i) {
66-
delayMicroseconds(1000);
67-
}
68-
}
69-
7064
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
7165

72-
/*
73-
* Like tone(), but use output B - OCR1B (PB4) + !OCR1B (PB3)
74-
* Assumes port mode already set to output
75-
*/
76-
void PWMtone(unsigned int aFrequency, unsigned int aDurationMillis) {
77-
// Determine which prescaler to use
78-
uint8_t tPrescaler = 0x01;
79-
uint16_t tOCR = F_CPU / aFrequency;
80-
while (tOCR > 0x100 && tPrescaler < 0x0F) {
81-
tPrescaler++;
82-
tOCR >>= 1;
83-
84-
}
85-
OCR1C = tOCR - 1; // The frequency of the PWM will be Timer Clock 1 Frequency divided by (OCR1C value + 1).
86-
OCR1B = OCR1C / 2; // set PWM to 50%
87-
GTCCR = (1 << PWM1B) | (1 << COM1B0); // Switch to PWM Mode with OC1B (PB4) + !OC1B (PB3) outputs enabled
88-
TCCR1 = (tPrescaler << CS10);
89-
90-
delayMilliseconds(aDurationMillis);
91-
TCCR1 = 0; // Select no clock
92-
GTCCR = 0; // Disconnect OC1B + !OC1B
93-
digitalWriteFast(PB3, 0);
94-
digitalWriteFast(PB4, 0);
95-
}
96-
9766
/*
9867
* initialize outputs and use PWM Mode
9968
* if aUseOutputB == false output frequency at Pin6/5 - PB1/PB0 - OCR1A/!OCR1A

examples/WhistleSwitch/ATtinyUtils.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* This file is part of ArduinoUtils https://github.com/ArminJo/ArduinoUtils.
88
*
9-
* ArduinoUtils is free software: you can redistribute it and/or modify
9+
* Arduino-Utils is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU General Public License as published by
1111
* the Free Software Foundation, either version 3 of the License, or
1212
* (at your option) any later version.
@@ -39,32 +39,27 @@
3939
#include <avr/io.h>
4040

4141
#if defined(ARDUINO_AVR_DIGISPARK)
42-
#undef LED_BUILTIN
4342
#define LED_BUILTIN PB1
4443
#elif defined(ARDUINO_AVR_DIGISPARKPRO)
45-
// On a Digispark Pro we have PB1 / D9 / PCB pin 1
46-
#undef LED_BUILTIN
47-
#define LED_BUILTIN (9)
44+
#define LED_BUILTIN (9) // On a Digispark Pro we have PB1 / D9 / PCB pin 1
4845
#endif
4946

50-
51-
5247
#if (F_CPU == 1000000)
53-
#define TIMER0_CLOCK_DIVIDER_FOR_64_MICROS ((1 << CS01) | (1 << CS00))
48+
#define TIMER0_CLOCK_DIVIDER_FOR_64_MICROS (_BV(CS01) | _BV(CS00))
5449

55-
#define TIMER1_CLOCK_DIVIDER_FOR_8_MICROS (1 << CS12)
56-
#define TIMER1_CLOCK_DIVIDER_FOR_4_MICROS ((1 << CS11) | (1 << CS10))
57-
#define TIMER1_CLOCK_DIVIDER_FOR_2_MICROS (1 << CS11)
58-
#define TIMER1_CLOCK_DIVIDER_FOR_1_MICRO (1 << CS10)
50+
#define TIMER1_CLOCK_DIVIDER_FOR_8_MICROS _BV(CS12)
51+
#define TIMER1_CLOCK_DIVIDER_FOR_4_MICROS (_BV(CS11) | _BV(CS10))
52+
#define TIMER1_CLOCK_DIVIDER_FOR_2_MICROS _BV(CS11)
53+
#define TIMER1_CLOCK_DIVIDER_FOR_1_MICRO _BV(CS10)
5954
#endif
6055

6156
#if (F_CPU == 8000000)
62-
#define TIMER0_CLOCK_DIVIDER_FOR_128_MICROS ((1 << CS02) | (1 << CS00))
57+
#define TIMER0_CLOCK_DIVIDER_FOR_128_MICROS (_BV(CS02) | _BV(CS00))
6358

64-
#define TIMER1_CLOCK_DIVIDER_FOR_8_MICROS ((1 << CS12) | (1 << CS11)| (1 << CS10))
65-
#define TIMER1_CLOCK_DIVIDER_FOR_4_MICROS ((1 << CS12) | (1 << CS11))
66-
#define TIMER1_CLOCK_DIVIDER_FOR_2_MICROS ((1 << CS12) | (1 << CS10))
67-
#define TIMER1_CLOCK_DIVIDER_FOR_1_MICRO (1 << CS12)
59+
#define TIMER1_CLOCK_DIVIDER_FOR_8_MICROS (_BV(CS12) | _BV(CS11)| _BV(CS10))
60+
#define TIMER1_CLOCK_DIVIDER_FOR_4_MICROS (_BV(CS12) | _BV(CS11))
61+
#define TIMER1_CLOCK_DIVIDER_FOR_2_MICROS (_BV(CS12) | _BV(CS10))
62+
#define TIMER1_CLOCK_DIVIDER_FOR_1_MICRO _BV(CS12)
6863
#endif
6964

7065
/*
@@ -87,7 +82,6 @@ inline void delay4CyclesInlineExact(uint16_t a4Microseconds) {
8782
);
8883
}
8984
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
90-
void PWMtone(unsigned int aFrequency, unsigned int aDurationMillis = 0);
9185
void toneWithTimer1PWM(uint16_t aFrequency, bool aUseOutputB = false);
9286
#endif
9387

examples/WhistleSwitch/WhistleSwitch.ino

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,24 @@
111111
*
112112
*/
113113

114+
#include <Arduino.h>
115+
#include "FrequencyDetector.h"
116+
114117
#define VERSION_EXAMPLE "7.3"
115118

116119
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__)
117120
#error "Code size of this example is too large to fit in an ATtiny 25 or 45."
118121
#endif
119122

120123
#if defined(__AVR_ATtiny85__)
124+
121125
//#define MEASURE_TIMING // not activated yet since there is no timing pin left
126+
122127
//#define TRACE
123128
//#define DEBUG
124129
//#define INFO
130+
#include "DebugLevel.h" // to propagate debug levels
131+
125132
#include "ATtinyUtils.h" // for changeDigisparkClock()
126133
# if ! defined(ARDUINO_AVR_DIGISPARK)
127134
#define INFO // needs around 1600 bytes FLASH - 4450 to 6060 bytes
@@ -132,13 +139,13 @@
132139
#else
133140
//#define ARDUINO_PLOTTER
134141
//#define MEASURE_TIMING
142+
135143
//#define TRACE
136144
//#define DEBUG
137145
#define INFO
138-
#endif // defined(__AVR_ATtiny85__)
146+
#include "DebugLevel.h" // to propagate debug levels
139147

140-
#include <Arduino.h>
141-
#include "FrequencyDetector.h"
148+
#endif // defined(__AVR_ATtiny85__)
142149

143150
/*
144151
* I can whistle from 550 to 1900 Hz (and do it easy from 950 - 1800)
@@ -779,7 +786,9 @@ uint16_t getFreeRam(void) {
779786
* Setup section
780787
*******************************************************************************************/
781788
void setup() {
789+
#ifdef INFO
782790
uint8_t tMCUSRStored = MCUSR; // content of MCUSR register at startup
791+
#endif
783792
MCUSR = 0; // to prepare for next reset or power on
784793

785794
/*
@@ -1359,7 +1368,7 @@ ISR(PCINT0_vect) {
13591368
PCICR = 0; // disable new PCINT's, since we allow nested interrupts
13601369
PCIFR = _BV(PCIF2); // Must clear interrupt flag in order to avoid to call this ISR again, when enabling interrupts below.
13611370
#elif defined(__AVR_ATtiny85__)
1362-
GIMSK &= ~ _BV(PCIE); // disable new PCINT's, since we allow nested interrupts
1371+
GIMSK &= ~_BV(PCIE); // disable new PCINT's, since we allow nested interrupts
13631372
GIFR = _BV(PCIF); // Must clear interrupt flag in order to avoid to call this ISR again, when enabling interrupts below.
13641373
#endif
13651374

src/DebugLevel.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* DebugLevel.h
3+
* Include to propagate debug levels
4+
*
5+
* Copyright (C) 2016-2020 Armin Joachimsmeyer
6+
* Email: armin.joachimsmeyer@gmail.com
7+
*
8+
* This file is part of Arduino-Utils https://github.com/ArminJo/Arduino-Utils.
9+
*
10+
* Arduino-Utils is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
22+
*
23+
*/
24+
25+
#ifndef DEBUGLEVEL_H_
26+
#define DEBUGLEVEL_H_
27+
28+
// Propagate debug level
29+
#ifdef TRACE
30+
# ifndef DEBUG
31+
#define DEBUG
32+
# endif
33+
#endif
34+
#ifdef DEBUG
35+
# ifndef INFO
36+
#define INFO
37+
# endif
38+
#endif
39+
#ifdef INFO
40+
# ifndef WARN
41+
#define WARN
42+
# endif
43+
#endif
44+
#ifdef WARN
45+
# ifndef ERROR
46+
#define ERROR
47+
# endif
48+
#endif
49+
50+
#endif /* DEBUGLEVEL_H_ */
51+
52+
#pragma once

src/FrequencyDetector.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* The value of millis() is adjusted after reading.
1010
* The alternative to disabling the interrupt is getting partially invalid results!
1111
*
12-
* Copyright (C) 2014 Armin Joachimsmeyer
12+
* Copyright (C) 2014-2020 Armin Joachimsmeyer
1313
* Email: armin.joachimsmeyer@gmail.com
1414
*
1515
* This file is part of Arduino-FrequencyDetector https://github.com/ArminJo/Arduino-FrequencyDetector.
@@ -41,15 +41,18 @@
4141

4242
#include <Arduino.h>
4343

44+
#include "FrequencyDetector.h"
45+
4446
//#define INFO
4547
//#define DEBUG
4648
//#define TRACE
49+
#include "DebugLevel.h" // to propagate debug levels
4750

4851
#if (defined(INFO) || defined(DEBUG) || defined(TRACE)) && (defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__))
49-
#include "ATtinySerialOut.h"
52+
#include "ATtinySerialOut.h" // Available as Arduino library and contained in WhistleSwitch example.
5053
#endif
5154

52-
#include "FrequencyDetector.h"
55+
#include "MillisUtils.h" // for timer0_millis
5356

5457
#define maximumAllowableCountOf(aPeriodCountTotal) (aPeriodCountTotal / 8)
5558

src/FrequencyDetector.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,6 @@
3838
#define FREQUENCY_RANGE_DEFAULT
3939
//#define FREQUENCY_RANGE_HIGH // use it for frequencies above approximately 3000 Hz
4040

41-
// Propagate debug level
42-
#ifdef TRACE
43-
#define DEBUG
44-
#endif
45-
#ifdef DEBUG
46-
#define INFO
47-
#endif
48-
#ifdef INFO
49-
#define WARN
50-
#endif
51-
#ifdef WARN
52-
#define ERROR
53-
#endif
54-
5541
/*
5642
* Global settings which are needed at compile time
5743
*/
@@ -148,14 +134,6 @@
148134
// number of needed valid readings (FrequencyRaw > SIGNAL_MAX_ERROR_CODE) before any (lower, match, higher) match - to avoid short flashes at random signal input
149135
#define MIN_NO_DROPOUT_COUNT_BEFORE_ANY_MATCH_DEFAULT ((MIN_NO_DROPOUT_MILLIS_BEFORE_ANY_MATCH_DEFAULT * 1000L) / MICROS_PER_BUFFER_READING)
150136

151-
/*
152-
* storage for millis value to enable compensation for interrupt disable at signal acquisition etc.
153-
*/
154-
#if ( defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) )
155-
#define timer0_millis millis_timer_millis // The ATTinyCore libraries use other variable name in wiring.c
156-
#endif
157-
extern volatile unsigned long timer0_millis;
158-
159137
// FrequencyRaw error values
160138
#define SIGNAL_NO_TRIGGER 0
161139
#define SIGNAL_STRENGTH_LOW 1

0 commit comments

Comments
 (0)