rad 0.2.2 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +34 -0
- data/Manifest.txt +113 -7
- data/{README.txt → README.rdoc} +17 -5
- data/Rakefile +3 -0
- data/bin/rad +106 -1
- data/lib/examples/add_hysteresis.rb +13 -0
- data/lib/examples/basic_blink.rb +10 -0
- data/lib/examples/blink_m_address_assignment.rb +104 -0
- data/lib/examples/blink_m_hello.rb +14 -0
- data/lib/examples/blink_m_multi.rb +61 -0
- data/lib/examples/blink_with_serial.rb +16 -0
- data/lib/examples/configure_pa_lcd_boot.rb +91 -0
- data/lib/examples/debounce_methods.rb +49 -0
- data/lib/examples/external_variable_fu.rb +26 -0
- data/lib/examples/external_variables.rb +32 -0
- data/lib/examples/first_sound.rb +23 -0
- data/lib/examples/frequency_generator.rb +30 -0
- data/lib/examples/hello_array.rb +48 -0
- data/lib/examples/hello_array2.rb +79 -0
- data/lib/examples/hello_array_eeprom.rb +59 -0
- data/lib/examples/hello_clock.rb +84 -0
- data/lib/examples/hello_eeprom.rb +51 -0
- data/lib/examples/hello_eeprom_lcdpa.rb +81 -0
- data/lib/examples/hello_format_print.rb +94 -0
- data/lib/examples/hello_lcd_charset.rb +75 -0
- data/lib/examples/hello_pa_lcd.rb +59 -0
- data/lib/examples/hello_servos.rb +88 -0
- data/lib/examples/hello_spectra_sound.rb +38 -0
- data/lib/examples/hello_world.rb +11 -0
- data/lib/examples/hello_xbee.rb +12 -0
- data/lib/examples/hysteresis_duel.rb +39 -0
- data/lib/examples/i2c_with_clock_chip.rb +124 -0
- data/lib/examples/midi_beat_box.rb +86 -0
- data/lib/examples/midi_scales.rb +94 -0
- data/lib/examples/motor_knob.rb +30 -0
- data/lib/examples/servo_buttons.rb +23 -0
- data/lib/examples/servo_calibrate_continuous.rb +92 -0
- data/lib/examples/servo_throttle.rb +40 -0
- data/lib/examples/sparkfun_lcd.rb +48 -0
- data/lib/examples/spectra_soft_pot.rb +34 -0
- data/lib/examples/times_method.rb +8 -0
- data/lib/examples/toggle.rb +10 -0
- data/lib/examples/twitter.rb +57 -0
- data/lib/examples/two_wire.rb +14 -0
- data/lib/libraries/AFSoftSerial/AFSoftSerial.cpp +321 -0
- data/lib/libraries/AFSoftSerial/AFSoftSerial.h +61 -0
- data/lib/libraries/AFSoftSerial/keywords.txt +18 -0
- data/lib/libraries/AF_XPort/AF_XPort.cpp +166 -0
- data/lib/libraries/AF_XPort/AF_XPort.h +48 -0
- data/lib/libraries/DS1307/DS1307.cpp +162 -0
- data/lib/libraries/DS1307/DS1307.h +66 -0
- data/lib/libraries/{SWSerLCDpa → DS1307}/keywords.txt +1 -1
- data/lib/libraries/FrequencyTimer2/FrequencyTimer2.cpp +144 -0
- data/lib/libraries/FrequencyTimer2/FrequencyTimer2.h +42 -0
- data/lib/libraries/FrequencyTimer2/keywords.txt +22 -0
- data/lib/libraries/I2CEEPROM/I2CEEPROM.cpp +120 -0
- data/lib/libraries/I2CEEPROM/I2CEEPROM.h +70 -0
- data/lib/libraries/I2CEEPROM/keywords.txt +21 -0
- data/lib/libraries/LoopTimer/LoopTimer.cpp +35 -0
- data/lib/libraries/LoopTimer/LoopTimer.h +34 -0
- data/lib/libraries/LoopTimer/keywords.txt +27 -0
- data/lib/libraries/OneWire/OneWire.cpp +194 -0
- data/lib/libraries/OneWire/OneWire.h +63 -0
- data/lib/libraries/OneWire/keywords.txt +35 -0
- data/lib/libraries/OneWire/readme.txt +13 -0
- data/lib/libraries/SWSerLCDpa/SWSerLCDpa.cpp +93 -47
- data/lib/libraries/SWSerLCDpa/SWSerLCDpa.h +16 -9
- data/lib/libraries/SWSerLCDsf/SWSerLCDsf.cpp +311 -0
- data/lib/libraries/SWSerLCDsf/SWSerLCDsf.h +67 -0
- data/lib/libraries/Servo/Servo.cpp +192 -0
- data/lib/libraries/Servo/Servo.h +61 -0
- data/lib/libraries/Stepper/Stepper.cpp +220 -0
- data/lib/libraries/Stepper/Stepper.h +86 -0
- data/lib/libraries/Stepper/keywords.txt +28 -0
- data/lib/libraries/Wire/Wire.cpp +262 -0
- data/lib/libraries/Wire/Wire.h +67 -0
- data/lib/libraries/Wire/keywords.txt +31 -0
- data/lib/libraries/Wire/twi.h +57 -0
- data/lib/libraries/Wire/utility/twi.c +449 -0
- data/lib/libraries/Wire/utility/twi.h +57 -0
- data/lib/plugins/bitwise_ops.rb +54 -0
- data/lib/plugins/blink.rb +25 -0
- data/lib/plugins/blink_m.rb +356 -0
- data/lib/plugins/debounce.rb +138 -0
- data/lib/plugins/debug_output_to_lcd.rb +71 -0
- data/lib/plugins/hysteresis.rb +52 -0
- data/lib/plugins/input_output_state.rb +84 -0
- data/lib/plugins/lcd_padding.rb +58 -0
- data/lib/plugins/mem_test.rb +37 -0
- data/lib/plugins/midi.rb +60 -0
- data/lib/plugins/parallax_ping.rb +50 -0
- data/lib/plugins/servo_pulse.rb +31 -0
- data/lib/plugins/servo_setup.rb +86 -0
- data/lib/plugins/smoother.rb +54 -0
- data/lib/plugins/spark_fun_serial_lcd.rb +100 -0
- data/lib/plugins/spectra_symbol.rb +79 -0
- data/lib/plugins/twitter_connect.rb +145 -0
- data/lib/rad/README.rdoc +5 -0
- data/lib/rad/arduino_plugin.rb +246 -0
- data/lib/rad/arduino_sketch.rb +351 -257
- data/lib/rad/generators/makefile/makefile.erb +1 -1
- data/lib/rad/generators/makefile/makefile.rb +9 -10
- data/lib/rad/hardware_library.rb +813 -0
- data/lib/rad/init.rb +3 -1
- data/lib/rad/rad_processor.rb +128 -0
- data/lib/rad/rad_rewriter.rb +94 -0
- data/lib/rad/rad_type_checker.rb +26 -0
- data/lib/rad/sim/arduino_sketch.rb +57 -0
- data/lib/rad/sketch_compiler.rb +47 -0
- data/lib/rad/tasks/build_and_make.rake +146 -24
- data/lib/rad/variable_processing.rb +153 -0
- data/lib/rad/version.rb +1 -1
- data/spec/examples/hello_world.rb +11 -0
- data/spec/examples/serial_motor.rb +12 -0
- data/spec/models/sketch_compiler_spec.rb +96 -0
- data/spec/sim/hello_world_spec.rb +42 -0
- data/test/test_array_processing.rb +179 -0
- data/test/test_plugin_loading.rb +151 -0
- data/test/test_translation_post_processing.rb +185 -0
- data/test/test_variable_processing.rb +238 -0
- data/website/index.html +22 -7
- data/website/stylesheets/screen.css +32 -1
- metadata +130 -13
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
|
|
2
|
+
#ifndef _AFXPort_h_
|
|
3
|
+
#define _AFXPort_h_
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
#define ERROR_NONE 0
|
|
7
|
+
#define ERROR_TIMEDOUT 2
|
|
8
|
+
#define ERROR_BADRESP 3
|
|
9
|
+
#define ERROR_DISCONN 4
|
|
10
|
+
|
|
11
|
+
#include <avr/pgmspace.h>
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class AF_XPort
|
|
15
|
+
{
|
|
16
|
+
private:
|
|
17
|
+
|
|
18
|
+
uint8_t rxpin, txpin, resetpin, dtrpin, rtspin, ctspin;
|
|
19
|
+
public:
|
|
20
|
+
AF_XPort(uint8_t rx, uint8_t tx, uint8_t reset=0, uint8_t dtr=0, uint8_t rts=0, uint8_t cts=0);
|
|
21
|
+
void begin(uint16_t b);
|
|
22
|
+
uint8_t reset(void);
|
|
23
|
+
uint8_t serialavail_timeout(int timeout);
|
|
24
|
+
uint8_t readline_timeout(char *buff, uint8_t maxlen, int timeout);
|
|
25
|
+
void flush(int timeout);
|
|
26
|
+
void disconnect();
|
|
27
|
+
uint8_t connect(char *ipaddr, long port);
|
|
28
|
+
void ROM_print(const char *pSTR);
|
|
29
|
+
uint8_t disconnected();
|
|
30
|
+
|
|
31
|
+
void print(char);
|
|
32
|
+
void print(const char[]);
|
|
33
|
+
void print(uint8_t);
|
|
34
|
+
void print(int);
|
|
35
|
+
void print(unsigned int);
|
|
36
|
+
void print(long);
|
|
37
|
+
void print(unsigned long);
|
|
38
|
+
void print(long, int);
|
|
39
|
+
void println(void);
|
|
40
|
+
void println(char);
|
|
41
|
+
void println(const char[]);
|
|
42
|
+
void println(uint8_t);
|
|
43
|
+
void println(int);
|
|
44
|
+
void println(long);
|
|
45
|
+
void println(unsigned long);
|
|
46
|
+
void println(long, int);
|
|
47
|
+
};
|
|
48
|
+
#endif
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
extern "C" {
|
|
2
|
+
#include <../Wire/Wire.h>
|
|
3
|
+
}
|
|
4
|
+
#include "DS1307.h"
|
|
5
|
+
|
|
6
|
+
DS1307::DS1307()
|
|
7
|
+
{
|
|
8
|
+
// Wire.begin(); // no no no no ..... do it explictly outside of library - multiple invocations is TROUBLE!!!!! BBR
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// DS1307 RTC=DS1307();
|
|
12
|
+
|
|
13
|
+
// PRIVATE FUNCTIONS
|
|
14
|
+
|
|
15
|
+
// Aquire data from the RTC chip in BCD format
|
|
16
|
+
// refresh the buffer
|
|
17
|
+
void DS1307::read(void)
|
|
18
|
+
{
|
|
19
|
+
// use the Wire lib to connect to the rtc
|
|
20
|
+
// reset the register pointer to zero
|
|
21
|
+
Wire.beginTransmission(DS1307_CTRL_ID);
|
|
22
|
+
Wire.send(0x00);
|
|
23
|
+
Wire.endTransmission();
|
|
24
|
+
|
|
25
|
+
// request the 7 bytes of data (secs, min, hr, dow, date, mth, yr)
|
|
26
|
+
Wire.requestFrom(DS1307_CTRL_ID, 7);
|
|
27
|
+
for(int i=0; i<7; i++)
|
|
28
|
+
{
|
|
29
|
+
// store data in raw bcd format
|
|
30
|
+
rtc_bcd[i]=Wire.receive();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// update the data on the IC from the bcd formatted data in the buffer
|
|
35
|
+
void DS1307::save(void)
|
|
36
|
+
{
|
|
37
|
+
Wire.beginTransmission(DS1307_CTRL_ID);
|
|
38
|
+
Wire.send(0x00); // reset register pointer
|
|
39
|
+
for(int i=0; i<7; i++)
|
|
40
|
+
{
|
|
41
|
+
Wire.send(rtc_bcd[i]);
|
|
42
|
+
}
|
|
43
|
+
Wire.endTransmission();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
// PUBLIC FUNCTIONS
|
|
48
|
+
|
|
49
|
+
void DS1307::get(byte *rtc, boolean refresh) // Aquire data from buffer and convert to int, refresh buffer if required
|
|
50
|
+
{
|
|
51
|
+
if(refresh) read();
|
|
52
|
+
for(int i=0;i<7;i++) // cycle through each component, create array of data
|
|
53
|
+
{
|
|
54
|
+
rtc[i]=get(i, 0);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
byte DS1307::get(int c, boolean refresh) // aquire individual RTC item from buffer, return as int, refresh buffer if required
|
|
60
|
+
{
|
|
61
|
+
if(refresh) read();
|
|
62
|
+
byte v=-1;
|
|
63
|
+
switch(c)
|
|
64
|
+
{
|
|
65
|
+
case DS1307_SEC:
|
|
66
|
+
v=(10*((rtc_bcd[DS1307_SEC] & DS1307_HI_SEC)>>4))+(rtc_bcd[DS1307_SEC] & DS1307_LO_BCD);
|
|
67
|
+
break;
|
|
68
|
+
case DS1307_MIN:
|
|
69
|
+
v=(10*((rtc_bcd[DS1307_MIN] & DS1307_HI_MIN)>>4))+(rtc_bcd[DS1307_MIN] & DS1307_LO_BCD);
|
|
70
|
+
break;
|
|
71
|
+
case DS1307_HR:
|
|
72
|
+
v=(10*((rtc_bcd[DS1307_HR] & DS1307_HI_HR)>>4))+(rtc_bcd[DS1307_HR] & DS1307_LO_BCD);
|
|
73
|
+
break;
|
|
74
|
+
case DS1307_DOW:
|
|
75
|
+
v=rtc_bcd[DS1307_DOW] & DS1307_LO_DOW;
|
|
76
|
+
break;
|
|
77
|
+
case DS1307_DATE:
|
|
78
|
+
v=(10*((rtc_bcd[DS1307_DATE] & DS1307_HI_DATE)>>4))+(rtc_bcd[DS1307_DATE] & DS1307_LO_BCD);
|
|
79
|
+
break;
|
|
80
|
+
case DS1307_MTH:
|
|
81
|
+
v=(10*((rtc_bcd[DS1307_MTH] & DS1307_HI_MTH)>>4))+(rtc_bcd[DS1307_MTH] & DS1307_LO_BCD);
|
|
82
|
+
break;
|
|
83
|
+
case DS1307_YR:
|
|
84
|
+
// v=(10*((rtc_bcd[DS1307_YR] & DS1307_HI_YR)>>4))+(rtc_bcd[DS1307_YR] & DS1307_LO_BCD)+DS1307_BASE_YR;
|
|
85
|
+
v=(10*((rtc_bcd[DS1307_YR] & DS1307_HI_YR)>>4))+(rtc_bcd[DS1307_YR] & DS1307_LO_BCD);
|
|
86
|
+
break;
|
|
87
|
+
} // end switch
|
|
88
|
+
return v;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
void DS1307::set(int c, int v) // Update buffer, then update the chip
|
|
92
|
+
{
|
|
93
|
+
switch(c)
|
|
94
|
+
{
|
|
95
|
+
case DS1307_SEC:
|
|
96
|
+
if(v<60 && v>-1)
|
|
97
|
+
{
|
|
98
|
+
//preserve existing clock state (running/stopped)
|
|
99
|
+
int state=rtc_bcd[DS1307_SEC] & DS1307_CLOCKHALT;
|
|
100
|
+
rtc_bcd[DS1307_SEC]=state | ((v / 10)<<4) + (v % 10);
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
case DS1307_MIN:
|
|
104
|
+
if(v<60 && v>-1)
|
|
105
|
+
{
|
|
106
|
+
rtc_bcd[DS1307_MIN]=((v / 10)<<4) + (v % 10);
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
case DS1307_HR:
|
|
110
|
+
// TODO : AM/PM 12HR/24HR
|
|
111
|
+
if(v<24 && v>-1)
|
|
112
|
+
{
|
|
113
|
+
rtc_bcd[DS1307_HR]=((v / 10)<<4) + (v % 10);
|
|
114
|
+
}
|
|
115
|
+
break;
|
|
116
|
+
case DS1307_DOW:
|
|
117
|
+
if(v<8 && v>-1)
|
|
118
|
+
{
|
|
119
|
+
rtc_bcd[DS1307_DOW]=v;
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
case DS1307_DATE:
|
|
123
|
+
if(v<31 && v>-1)
|
|
124
|
+
{
|
|
125
|
+
rtc_bcd[DS1307_DATE]=((v / 10)<<4) + (v % 10);
|
|
126
|
+
}
|
|
127
|
+
break;
|
|
128
|
+
case DS1307_MTH:
|
|
129
|
+
if(v<13 && v>-1)
|
|
130
|
+
{
|
|
131
|
+
rtc_bcd[DS1307_MTH]=((v / 10)<<4) + (v % 10);
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
case DS1307_YR:
|
|
135
|
+
if(v<13 && v>-1)
|
|
136
|
+
{
|
|
137
|
+
rtc_bcd[DS1307_YR]=((v / 10)<<4) + (v % 10);
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
} // end switch
|
|
141
|
+
save();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
void DS1307::stop(void)
|
|
145
|
+
{
|
|
146
|
+
// set the ClockHalt bit high to stop the rtc
|
|
147
|
+
// this bit is part of the seconds byte
|
|
148
|
+
rtc_bcd[DS1307_SEC]=rtc_bcd[DS1307_SEC] | DS1307_CLOCKHALT;
|
|
149
|
+
save();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
void DS1307::start(void)
|
|
153
|
+
{
|
|
154
|
+
// unset the ClockHalt bit to start the rtc
|
|
155
|
+
// TODO : preserve existing seconds
|
|
156
|
+
rtc_bcd[DS1307_SEC]=0;
|
|
157
|
+
save();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
DS1307.h - library for DS1307 rtc
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// ensure this library description is only included once
|
|
6
|
+
#ifndef DS1307_h
|
|
7
|
+
#define DS1307_h
|
|
8
|
+
|
|
9
|
+
// include types & constants of Wiring core API
|
|
10
|
+
#include <WConstants.h>
|
|
11
|
+
|
|
12
|
+
// include types & constants of Wire ic2 lib
|
|
13
|
+
#include <../Wire/Wire.h>
|
|
14
|
+
|
|
15
|
+
#define DS1307_SEC 0
|
|
16
|
+
#define DS1307_MIN 1
|
|
17
|
+
#define DS1307_HR 2
|
|
18
|
+
#define DS1307_DOW 3
|
|
19
|
+
#define DS1307_DATE 4
|
|
20
|
+
#define DS1307_MTH 5
|
|
21
|
+
#define DS1307_YR 6
|
|
22
|
+
#define DS1307_CTRL 7
|
|
23
|
+
|
|
24
|
+
#define DS1307_BASE_YR 2000 // with change of get() return from int to byte. this is no longer
|
|
25
|
+
// used, the user must add 2000 himself
|
|
26
|
+
|
|
27
|
+
#define DS1307_CTRL_ID B1101000 // DS1307
|
|
28
|
+
|
|
29
|
+
// Define register bit masks
|
|
30
|
+
#define DS1307_CLOCKHALT B10000000
|
|
31
|
+
|
|
32
|
+
#define DS1307_LO_BCD B00001111
|
|
33
|
+
#define DS1307_HI_BCD B11110000
|
|
34
|
+
|
|
35
|
+
#define DS1307_HI_SEC B01110000
|
|
36
|
+
#define DS1307_HI_MIN B01110000
|
|
37
|
+
#define DS1307_HI_HR B00110000
|
|
38
|
+
#define DS1307_LO_DOW B00000111
|
|
39
|
+
#define DS1307_HI_DATE B00110000
|
|
40
|
+
#define DS1307_HI_MTH B00110000
|
|
41
|
+
#define DS1307_HI_YR B11110000
|
|
42
|
+
|
|
43
|
+
// library interface description
|
|
44
|
+
class DS1307
|
|
45
|
+
{
|
|
46
|
+
// user-accessible "public" interface
|
|
47
|
+
public:
|
|
48
|
+
DS1307();
|
|
49
|
+
void get(byte*, boolean);
|
|
50
|
+
byte get(int, boolean);
|
|
51
|
+
void set(int, int);
|
|
52
|
+
void start(void);
|
|
53
|
+
void stop(void);
|
|
54
|
+
|
|
55
|
+
// library-accessible "private" interface
|
|
56
|
+
private:
|
|
57
|
+
byte rtc_bcd[7]; // used prior to read/set ds1307 registers;
|
|
58
|
+
void read(void);
|
|
59
|
+
void save(void);
|
|
60
|
+
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
extern DS1307 RTC;
|
|
64
|
+
|
|
65
|
+
#endif
|
|
66
|
+
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
#include <FrequencyTimer2.h>
|
|
5
|
+
|
|
6
|
+
#include <avr/interrupt.h>
|
|
7
|
+
|
|
8
|
+
void (*FrequencyTimer2::onOverflow)() = 0;
|
|
9
|
+
uint8_t FrequencyTimer2::enabled = 0;
|
|
10
|
+
|
|
11
|
+
#if defined(__AVR_ATmega168__)
|
|
12
|
+
SIGNAL(SIG_OUTPUT_COMPARE2A)
|
|
13
|
+
#else
|
|
14
|
+
SIGNAL(SIG_OUTPUT_COMPARE2)
|
|
15
|
+
#endif
|
|
16
|
+
{
|
|
17
|
+
static uint8_t inHandler = 0; // protect us from recursion if our handler enables interrupts
|
|
18
|
+
|
|
19
|
+
if ( !inHandler && FrequencyTimer2::onOverflow) {
|
|
20
|
+
inHandler = 1;
|
|
21
|
+
(*FrequencyTimer2::onOverflow)();
|
|
22
|
+
inHandler = 0;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
void FrequencyTimer2::setOnOverflow( void (*func)() )
|
|
27
|
+
{
|
|
28
|
+
FrequencyTimer2::onOverflow = func;
|
|
29
|
+
#if defined(__AVR_ATmega168__)
|
|
30
|
+
if ( func) TIMSK2 |= _BV(OCIE2A);
|
|
31
|
+
else TIMSK2 &= ~_BV(OCIE2A);
|
|
32
|
+
#else
|
|
33
|
+
if ( func) TIMSK |= _BV(OCIE2);
|
|
34
|
+
else TIMSK &= ~_BV(OCIE2);
|
|
35
|
+
#endif
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
void FrequencyTimer2::setPeriod(unsigned long period)
|
|
39
|
+
{
|
|
40
|
+
uint8_t pre, top;
|
|
41
|
+
|
|
42
|
+
if ( period == 0) period = 1;
|
|
43
|
+
period *= clockCyclesPerMicrosecond();
|
|
44
|
+
|
|
45
|
+
period /= 2; // we work with half-cycles before the toggle
|
|
46
|
+
if ( period <= 256) {
|
|
47
|
+
pre = 1;
|
|
48
|
+
top = period-1;
|
|
49
|
+
} else if ( period <= 256L*8) {
|
|
50
|
+
pre = 2;
|
|
51
|
+
top = period/8-1;
|
|
52
|
+
} else if ( period <= 256L*32) {
|
|
53
|
+
pre = 3;
|
|
54
|
+
top = period/32-1;
|
|
55
|
+
} else if ( period <= 256L*64) {
|
|
56
|
+
pre = 4;
|
|
57
|
+
top = period/64-1;
|
|
58
|
+
} else if ( period <= 256L*128) {
|
|
59
|
+
pre = 5;
|
|
60
|
+
top = period/128-1;
|
|
61
|
+
} else if ( period <= 256L*256) {
|
|
62
|
+
pre = 6;
|
|
63
|
+
top = period/256-1;
|
|
64
|
+
} else if ( period <= 256L*1024) {
|
|
65
|
+
pre = 7;
|
|
66
|
+
top = period/1024-1;
|
|
67
|
+
} else {
|
|
68
|
+
pre = 7;
|
|
69
|
+
top = 255;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#if defined(__AVR_ATmega168__)
|
|
73
|
+
TCCR2B = 0;
|
|
74
|
+
TCCR2A = 0;
|
|
75
|
+
TCNT2 = 0;
|
|
76
|
+
ASSR &= ~_BV(AS2); // use clock, not T2 pin
|
|
77
|
+
OCR2A = top;
|
|
78
|
+
TCCR2A = (_BV(WGM21) | ( FrequencyTimer2::enabled ? _BV(COM2A0) : 0));
|
|
79
|
+
TCCR2B = pre;
|
|
80
|
+
#else
|
|
81
|
+
TCCR2 = 0;
|
|
82
|
+
TCNT2 = 0;
|
|
83
|
+
ASSR &= ~_BV(AS2); // use clock, not T2 pin
|
|
84
|
+
OCR2 = top;
|
|
85
|
+
TCCR2 = (_BV(WGM21) | ( FrequencyTimer2::enabled ? _BV(COM20) : 0) | pre);
|
|
86
|
+
#endif
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
unsigned long FrequencyTimer2::getPeriod()
|
|
90
|
+
{
|
|
91
|
+
#if defined(__AVR_ATmega168__)
|
|
92
|
+
uint8_t p = (TCCR2B & 7);
|
|
93
|
+
unsigned long v = OCR2A;
|
|
94
|
+
#else
|
|
95
|
+
uint8_t p = (TCCR2 & 7);
|
|
96
|
+
unsigned long v = OCR2;
|
|
97
|
+
#endif
|
|
98
|
+
uint8_t shift;
|
|
99
|
+
|
|
100
|
+
switch(p) {
|
|
101
|
+
case 0 ... 1:
|
|
102
|
+
shift = 0;
|
|
103
|
+
break;
|
|
104
|
+
case 2:
|
|
105
|
+
shift = 3;
|
|
106
|
+
break;
|
|
107
|
+
case 3:
|
|
108
|
+
shift = 5;
|
|
109
|
+
break;
|
|
110
|
+
case 4:
|
|
111
|
+
shift = 6;
|
|
112
|
+
break;
|
|
113
|
+
case 5:
|
|
114
|
+
shift = 7;
|
|
115
|
+
break;
|
|
116
|
+
case 6:
|
|
117
|
+
shift = 8;
|
|
118
|
+
break;
|
|
119
|
+
case 7:
|
|
120
|
+
shift = 10;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
return (((v+1) << (shift+1)) + 1) / clockCyclesPerMicrosecond(); // shift+1 converts from half-period to period
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
void FrequencyTimer2::enable()
|
|
127
|
+
{
|
|
128
|
+
FrequencyTimer2::enabled = 1;
|
|
129
|
+
#if defined(__AVR_ATmega168__)
|
|
130
|
+
TCCR2A |= _BV(COM2A0);
|
|
131
|
+
#else
|
|
132
|
+
TCCR2 |= _BV(COM20);
|
|
133
|
+
#endif
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
void FrequencyTimer2::disable()
|
|
137
|
+
{
|
|
138
|
+
FrequencyTimer2::enabled = 0;
|
|
139
|
+
#if defined(__AVR_ATmega168__)
|
|
140
|
+
TCCR2A &= ~_BV(COM2A0);
|
|
141
|
+
#else
|
|
142
|
+
TCCR2 &= ~_BV(COM20);
|
|
143
|
+
#endif
|
|
144
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#ifndef FREQUENCYTIMER2_IS_IN
|
|
2
|
+
#define FREQUENCYTIMER2_IS_IN
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
FrequencyTimer2.h - A frequency generator and interrupt generator library
|
|
6
|
+
Author: Jim Studt, jim@federated.com
|
|
7
|
+
Copyright (c) 2007 David A. Mellis. All right reserved.
|
|
8
|
+
|
|
9
|
+
This library is free software; you can redistribute it and/or
|
|
10
|
+
modify it under the terms of the GNU Lesser General Public
|
|
11
|
+
License as published by the Free Software Foundation; either
|
|
12
|
+
version 2.1 of the License, or (at your option) any later version.
|
|
13
|
+
|
|
14
|
+
This library is distributed in the hope that it will be useful,
|
|
15
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17
|
+
Lesser General Public License for more details.
|
|
18
|
+
|
|
19
|
+
You should have received a copy of the GNU Lesser General Public
|
|
20
|
+
License along with this library; if not, write to the Free Software
|
|
21
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
#include <wiring.h>
|
|
26
|
+
|
|
27
|
+
class FrequencyTimer2
|
|
28
|
+
{
|
|
29
|
+
private:
|
|
30
|
+
static uint8_t enabled;
|
|
31
|
+
public:
|
|
32
|
+
static void (*onOverflow)(); // not really public, but I can't work out the 'friend' for the SIGNAL
|
|
33
|
+
|
|
34
|
+
public:
|
|
35
|
+
static void setPeriod(unsigned long);
|
|
36
|
+
static unsigned long getPeriod();
|
|
37
|
+
static void setOnOverflow( void (*)() );
|
|
38
|
+
static void enable();
|
|
39
|
+
static void disable();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
#endif
|