8000 add digital gpio functions · sparkfun/arduino-mbed-bridge@192bc0c · GitHub
[go: up one dir, main page]

Skip to content

Commit 192bc0c

Browse files
committed
add digital gpio functions
- pinMode - digitalWrite - digitalRead
1 parent 6deb064 commit 192bc0c

File tree

4 files changed

+191
-7
lines changed

4 files changed

+191
-7
lines changed

Arduino.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ SOFTWARE.
3131
#include "core-api/api/ArduinoAPI.h"
3232
#undef PinMode
3333

34+
#include "pins.h"
35+
3436
#endif // _ARDUINO_H_

bridge/pins.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 "bridge/pins.h"
24+
25+
pin_size_t pinIndexByName(PinName name){
26+
pin_size_t index = 0;
27+
while(index < variantPinCount){
28+
if(variantPinStates[index].name == name){ return index; }
29+
index++;
30+
}
31+
return variantPinCount;
32+
}
33+
34+
pin_size_t pinIndexByNumber(pin_size_t number){
35+
pin_size_t index = 0;
36+
while(index < variantPinCount){
37+
if(variantPinStates[index].number == number){ return index; }
38+
index++;
39+
}
40+
return variantPinCount;
41+
}
42+
43+
PinName pinNameByIndex(pin_size_t index){
44+
if(index >= variantPinCount){ return NC; }
45+
return variantPinStates[index].name;
46+
}
47+
48+
pin_size_t pinNumberByIndex(pin_size_t index){
49+
if(index >= variantPinCount){ return (pin_size_t)NC; }
50+
return variantPinStates[index].number;
51+
}

bridge/pins.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
#ifndef _ARDUINO_MBED_BRIDGE_PINS_H_
24+
#define _ARDUINO_MBED_BRIDGE_PINS_H_
25+
26+
#include "Arduino.h"
27+
28+
typedef struct _PinState {
29+
PinName name;
30+
pin_size_t number;
31+
InterruptIn* irq;
32+
// PwmOut* pwm; // todo: implement this in mbed
33+
DigitalInOut* gpio;
34+
} PinState;
35+
36+
pin_size_t pinIndexByName(PinName name);
37+
pin_size_t pinIndexByNumber(pin_size_t number);
38+
39+
PinName pinNameByIndex(pin_size_t index);
40+
pin_size_t pinNumberByIndex(pin_size_t index);
41+
42+
#define pinGPIOByIndex(I) variantPinStates[I].gpio
43+
44+
void pinMode(PinName pinName, Arduino_PinMode pinMode);
45+
void digitalWrite(PinName pinName, PinStatus val);
46+
PinStatus digitalRead(PinName pinName);
47+
48+
extern const pin_size_t variantPinCount;
49+
extern PinState variantPinStates[];
50+
51+
#endif // _ARDUINO_MBED_BRIDGE_PINS_H_

core-implement/Common.cpp

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,98 @@ SOFTWARE.
2626
#include "core-api/api/Common.h"
2727
#undef PinMode
2828

29+
#include "bridge/pins.h"
30+
2931
#define standInFunc() printf("%s [file: %s, line: %d]\n", __FUNCTION__, __FILE__, __LINE__)
3032

31-
void pinMode(pin_size_t pinNumber, Arduino_PinMode pinMode){
33+
void indexMode(pin_size_t index, Arduino_PinMode pinMode){
3234
standInFunc();
35+
printf("\tindex: %d\n", index);
36+
DigitalInOut* gpio = pinGPIOByIndex(index);
37+
if( gpio == NULL ){
38+
gpio = new DigitalInOut(pinNameByIndex(index));
39+
printf("\tcreating a new DigitalInOut object! 0x%08X\n", (uint32_t)gpio);
40+
}
41+
printf("\tgpio = 0x%08X\n", (uint32_t)gpio);
42+
pinGPIOByIndex(index) = gpio;
3343

34-
switch( pinMode ){
35-
case INPUT: printf("\tINPUT\n"); break;
36-
case OUTPUT: printf("\tOUTPUT\n"); break;
37-
case INPUT_PULLUP: printf("\tINPUT_PULLUP\n"); break;
38-
case INPUT_PULLDOWN: printf("\tINPUT_PULLDOWN\n"); break;
44+
switch (pinMode) {
45+
case INPUT:
46+
gpio->input();
47+
gpio->mode(PullNone);
48+
break;
49+
case OUTPUT:
50+
gpio->output();
51+
break;
52+
case INPUT_PULLUP:
53+
gpio->input();
54+
gpio->mode(PullUp);
55+
break;
56+
case INPUT_PULLDOWN:
3957
default:
40-
printf("\tunknown pinMode given\n");
58+
gpio->input();
59+
gpio->mode(PullDown);
4160
break;
4261
}
4362
}
63+
64+
void pinMode(pin_size_t pinNumber, Arduino_PinMode pinMode){
65+
pin_size_t index = pinIndexByNumber(pinNumber);
66+
printf("via PinNumber\n");
67+
if( index == variantPinCount ){ return; }
68+
indexMode(index, pinMode);
69+
}
70+
71+
void pinMode(PinName pinName, Arduino_PinMode pinMode){
72+
pin_size_t index = pinIndexByName(pinName);
73+
printf("via PinName\n");
74+
if( index == variantPinCount ){ return; }
75+
indexMode(index, pinMode);
76+
}
77+
78+
void indexDigitalWrite(pin_size_t index, PinStatus val){
79+
mbed::DigitalInOut* gpio = pinGPIOByIndex(index);
80+
if (gpio == NULL) {
81+
gpio = new mbed::DigitalInOut(pinNameByIndex(index), PIN_OUTPUT, PullNone, val);
82+
pinGPIOByIndex(index) = gpio;
83+
}
84+
gpio->write(val);
85+
}
86+
87+
void digitalWrite(pin_size_t pinNumber, PinStatus val){
88+
pin_size_t index = pinIndexByNumber(pinNumber);
89+
printf("via PinNumber\n");
90+
if( index == variantPinCount ){ return; }
91+
indexDigitalWrite(index, val);
92+
}
93+
94+
void digitalWrite(PinName pinName, PinStatus val){
95+
pin_size_t index = pinIndexByName(pinName);
96+
printf("via PinName\n");
97+
if( index == variantPinCount ){ return; }
98+
indexDigitalWrite(index, val);
99+
}
100+
101+
PinStatus indexDigitalRead(pin_size_t index){
102+
mbed::DigitalInOut* gpio = pinGPIOByIndex(index);
103+
if (gpio == NULL) {
104+
gpio = new mbed::DigitalInOut(pinNameByIndex(index), PIN_INPUT, PullNone, 0);
105+
pinGPIOByIndex(index) = gpio;
106+
}
107+
return (PinStatus) gpio->read();
108+
}
109+
110+
PinStatus digitalRead(pin_size_t pinNumber){
111+
pin_size_t index = pinIndexByNumber(pinNumber);
112+
printf("via PinNumber\n");
113+
if( index == variantPinCount ){ return LOW; }
114+
return indexDigitalRead(index);
115+
}
116+
117+
PinStatus digitalRead(PinName pinName){
118+
pin_size_t index = pinIndexByName(pinName);
119+
printf("via PinName\n");
120+
if( index == variantPinCount ){ return LOW; }
121+
return indexDigitalRead(index);
122+
}
123+

0 commit comments

Comments
 (0)
0