Skip to content

Commit 3e19822

Browse files
committed
separate implementations
Common.h contains a lot of different Arduino API declarations - this splits their implementations into several files for clarity the files begin with 'Common' to indicate their relation to the Common.h interface and are postfixed with a descriptive word to indicate their particular purpose
1 parent aa423de commit 3e19822

File tree

5 files changed

+246
-120
lines changed

5 files changed

+246
-120
lines changed

core-implement/CommonAnalog.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#include "mbed.h"
24+
25+
#define PinMode Arduino_PinMode
26+
#include "core-api/api/Common.h"
27+
#undef PinMode
28+
29+
#include "bridge/pins.h"
30+
31+
#define standInFunc() printf("Stand-In for '%s' [file: %s, line: %d]\n", __FUNCTION__, __FILE__, __LINE__)
32+
33+
// static int res_analog_w = 8;
34+
// static int res_analog_r = 10;
35+
void indexAnalogWriteDAC(pin_size_t index, int val){
36+
standInFunc();
37+
// // todo: support mbed DAC modules
38+
// mbed::AnalogOut* dac = pinDACByIndex(index);
39+
// if (dac == NULL) {
40+
// dac = new mbed::AnalogOut(pinNameByIndex(index));
41+
// pinDACByIndex(index) = dac;
42+
// }
43+
// float percent = (float)val/(float)(1 << res_analog_w);
44+
// dac->write(percent);
45+
}
46+
47+
void analogWriteDAC(PinName pinName, int val){
48+
pin_size_t index = pinIndexByName(pinName);
49+
if( index == variantPinCount ){ return; }
50+
indexAnalogWriteDAC(index, val);
51+
}
52+
53+
void analogWriteDAC(pin_size_t pinNumber, int val){
54+
pin_size_t index = pinIndexByNumber(pinNumber);
55+
if( index == variantPinCount ){ return; }
56+
indexAnalogWriteDAC(index, val);
57+
}
58+
59+
// void indexAnalogWrite(pin_size_t index, int val){
60+
// standInFunc();
61+
// // mbed::PwmOut* pwm = pinPWMByIndex(index);
62+
// // if (pwm == NULL) {
63+
// // pwm = new mbed::PwmOut(pinNameByIndex(index));
64+
// // pinPWMByIndex(index) = pwm;
65+
// // }
66+
// // pwm->period_ms(2);
67+
// // float percent = (float)val/(float)(1 << res_analog_w);
68+
// // pwm->write(percent);
69+
// }
70+
71+
void analogWrite(PinName pinName, int val){
72+
pin_size_t index = pinIndexByName(pinName);
73+
if( index == variantPinCount ){ return; }
74+
indexAnalogWrite(index, val);
75+
}
76+
77+
void analogWrite(pin_size_t pinNumber, int val){
78+
pin_size_t index = pinIndexByNumber(pinNumber);
79+
if( index == variantPinCount ){ return; }
80+
indexAnalogWrite(index, val);
81+
}

core-implement/Common.cpp renamed to core-implement/CommonDigital.cpp

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -120,123 +120,3 @@ PinStatus digitalRead(PinName pinName){
120120
if( index == variantPinCount ){ return LOW; }
121121
return indexDigitalRead(index);
122122
}
123-
124-
// static int res_analog_w = 8;
125-
// static int res_analog_r = 10;
126-
void indexAnalogWriteDAC(pin_size_t index, int val){
127-
standInFunc();
128-
// // todo: support mbed DAC modules
129-
// mbed::AnalogOut* dac = pinDACByIndex(index);
130-
// if (dac == NULL) {
131-
// dac = new mbed::AnalogOut(pinNameByIndex(index));
132-
// pinDACByIndex(index) = dac;
133-
// }
134-
// float percent = (float)val/(float)(1 << res_analog_w);
135-
// dac->write(percent);
136-
}
137-
138-
void analogWriteDAC(PinName pinName, int val){
139-
pin_size_t index = pinIndexByName(pinName);
140-
if( index == variantPinCount ){ return; }
141-
indexAnalogWriteDAC(index, val);
142-
}
143-
144-
void analogWriteDAC(pin_size_t pinNumber, int val){
145-
pin_size_t index = pinIndexByNumber(pinNumber);
146-
if( index == variantPinCount ){ return; }
147-
indexAnalogWriteDAC(index, val);
148-
}
149-
150-
// void indexAnalogWrite(pin_size_t index, int val){
151-
// standInFunc();
152-
// // mbed::PwmOut* pwm = pinPWMByIndex(index);
153-
// // if (pwm == NULL) {
154-
// // pwm = new mbed::PwmOut(pinNameByIndex(index));
155-
// // pinPWMByIndex(index) = pwm;
156-
// // }
157-
// // pwm->period_ms(2);
158-
// // float percent = (float)val/(float)(1 << res_analog_w);
159-
// // pwm->write(percent);
160-
// }
161-
162-
void analogWrite(PinName pinName, int val){
163-
pin_size_t index = pinIndexByName(pinName);
164-
if( index == variantPinCount ){ return; }
165-
indexAnalogWrite(index, val);
166-
}
167-
168-
void analogWrite(pin_size_t pinNumber, int val){
169-
pin_size_t index = pinIndexByNumber(pinNumber);
170-
if( index == variantPinCount ){ return; }
171-
indexAnalogWrite(index, val);
172-
}
173-
174-
// #if DEVICE_LPTICKER
175-
// static mbed::LowPowerTimer t;
176-
// #else
177-
static mbed::Timer t;
178-
// #endif
179-
180-
using namespace std::chrono_literals;
181-
using namespace std::chrono;
182-
183-
void initTimer(void){
184-
t.start();
185-
}
186-
187-
unsigned long millis(void){
188-
return duration_cast<milliseconds>(t.elapsed_time()).count();
189-
}
190-
191-
unsigned long micros(void){
192-
return t.elapsed_time().count();
193-
}
194-
195-
void delay(unsigned long ms){
196-
#ifndef NO_RTOS
197-
rtos::ThisThread::sleep_for(ms * 1ms);
198-
#else
199-
wait_us(ms * 1000);
200-
#endif
201-
}
202-
203-
void delayMicroseconds(unsigned int us){
204-
wait_us(us);
205-
}
206-
207-
// unsigned long indexPulseIn(pin_size_t index, uint8_t state, unsigned long timeout){
208-
// standInFunc();
209-
// return 0;
210-
// }
211-
212-
unsigned long pulseIn(PinName pinName, uint8_t state, unsigned long timeout){
213-
pin_size_t index = pinIndexByName(pinName);
214-
if( index == variantPinCount ){ return 0; }
215-
return indexPulseIn(index, state, timeout);
216-
}
217-
218-
unsigned long pulseIn(pin_size_t pinNumber, uint8_t state, unsigned long timeout){
219-
pin_size_t index = pinIndexByNumber(pinNumber);
220-
if( index == variantPinCount ){ return 0; }
221-
return indexPulseIn(index, state, timeout);
222-
}
223-
224-
unsigned long indexPulseInLong(pin_size_t index, uint8_t state, unsigned long timeout){
225-
return indexPulseIn(index, state, timeout); // pulseIn and pulseInLong are identical
226-
}
227-
228-
unsigned long pulseInLong(PinName pinName, uint8_t state, unsigned long timeout){
229-
pin_size_t index = pinIndexByName(pinName);
230-
if( index == variantPinCount ){ return 0; }
231-
return pulseInLong(index, state, timeout);
232-
}
233-
234-
unsigned long pulseInLong(pin_size_t pinNumber, uint8_t state, unsigned long timeout){
235-
pin_size_t index = pinIndexByNumber(pinNumber);
236-
if( index == variantPinCount ){ return 0; }
237-
return pulseInLong(index, state, timeout);
238-
}
239-
240-
void init(void){
241-
initTimer();
242-
}

core-implement/CommonInit.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#include "mbed.h"
24+
25+
#define PinMode Arduino_PinMode
26+
#include "core-api/api/Common.h"
27+
#undef PinMode
28+
29+
#include "bridge/pins.h"
30+
31+
#define standInFunc() printf("Stand-In for '%s' [file: %s, line: %d]\n", __FUNCTION__, __FILE__, __LINE__)
32+
33+
extern void initTimer(void);
34+
35+
void init(void){
36+
initTimer();
37+
}

core-implement/CommonPulse.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#include "mbed.h"
24+
25+
#define PinMode Arduino_PinMode
26+
#include "core-api/api/Common.h"
27+
#undef PinMode
28+
29+
#include "bridge/pins.h"
30+
31+
#define standInFunc() printf("Stand-In for '%s' [file: %s, line: %d]\n", __FUNCTION__, __FILE__, __LINE__)
32+
33+
// unsigned long indexPulseIn(pin_size_t index, uint8_t state, unsigned long timeout){
34+
// standInFunc();
35+
// return 0;
36+
// }
37+
38+
unsigned long pulseIn(PinName pinName, uint8_t state, unsigned long timeout){
39+
pin_size_t index = pinIndexByName(pinName);
40+
if( index == variantPinCount ){ return 0; }
41+
return indexPulseIn(index, state, timeout);
42+
}
43+
44+
unsigned long pulseIn(pin_size_t pinNumber, uint8_t state, unsigned long timeout){
45+
pin_size_t index = pinIndexByNumber(pinNumber);
46+
if( index == variantPinCount ){ return 0; }
47+
return indexPulseIn(index, state, timeout);
48+
}
49+
50+
unsigned long indexPulseInLong(pin_size_t index, uint8_t state, unsigned long timeout){
51+
return indexPulseIn(index, state, timeout); // pulseIn and pulseInLong are identical
52+
}
53+
54+
unsigned long pulseInLong(PinName pinName, uint8_t state, unsigned long timeout){
55+
pin_size_t index = pinIndexByName(pinName);
56+
if( index == variantPinCount ){ return 0; }
57+
return pulseInLong(index, state, timeout);
58+
}
59+
60+
unsigned long pulseInLong(pin_size_t pinNumber, uint8_t state, unsigned long timeout){
61+
pin_size_t index = pinIndexByNumber(pinNumber);
62+
if( index == variantPinCount ){ return 0; }
63+
return pulseInLong(index, state, timeout);
64+
}

core-implement/CommonTiming.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#include "mbed.h"
24+
25+
#define PinMode Arduino_PinMode
26+
#include "core-api/api/Common.h"
27+
#undef PinMode
28+
29+
#include "bridge/pins.h"
30+
31+
#define standInFunc() printf("Stand-In for '%s' [file: %s, line: %d]\n", __FUNCTION__, __FILE__, __LINE__)
32+
33+
// #if DEVICE_LPTICKER
34+
// static mbed::LowPowerTimer t;
35+
// #else
36+
static mbed::Timer t;
37+
// #endif
38+
39+
using namespace std::chrono_literals;
40+
using namespace std::chrono;
41+
42+
void initTimer(void){
43+
t.start();
44+
}
45+
46+
unsigned long millis(void){
47+
return duration_cast<milliseconds>(t.elapsed_time()).count();
48+
}
49+
50+
unsigned long micros(void){
51+
return t.elapsed_time().count();
52+
}
53+
54+
void delay(unsigned long ms){
55+
#ifndef NO_RTOS
56+
rtos::ThisThread::sleep_for(ms * 1ms);
57+
#else
58+
wait_us(ms * 1000);
59+
#endif
60+
}
61+
62+
void delayMicroseconds(unsigned int us){
63+
wait_us(us);
64+
}

0 commit comments

Comments
 (0)