Skip to content

Commit c4a074c

Browse files
committed
clean up
1 parent eb339b9 commit c4a074c

File tree

4 files changed

+173
-3
lines changed

4 files changed

+173
-3
lines changed

libraries/Firmata/Boards.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,27 @@ writePort(port, value, bitmask): Write an 8 bit port.
780780
#define PIN_TO_SERVO(p) (p)
781781
#define DEFAULT_PWM_RESOLUTION 10
782782

783+
// STM32 based boards
784+
#elif defined(ARDUINO_ARCH_STM32)
785+
#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
786+
#define TOTAL_PINS NUM_DIGITAL_PINS
787+
#define TOTAL_PORTS MAX_NB_PORT
788+
#define VERSION_BLINK_PIN LED_BUILTIN
789+
// PIN_SERIALY_RX/TX defined in the variant.h
790+
#define IS_PIN_DIGITAL(p) (digitalPinIsValid(p) && !pinIsSerial(p))
791+
#define IS_PIN_ANALOG(p) ((p >= A0) && (p < AEND) && !pinIsSerial(p))
792+
#define IS_PIN_PWM(p) (IS_PIN_DIGITAL(p) && digitalPinHasPWM(p))
793+
#define IS_PIN_SERVO(p) IS_PIN_DIGITAL(p)
794+
#define IS_PIN_I2C(p) (IS_PIN_DIGITAL(p) && digitalPinHasI2C(p))
795+
#define IS_PIN_SPI(p) (IS_PIN_DIGITAL(p) && digitalPinHasSPI(p))
796+
#define IS_PIN_INTERRUPT(p) (IS_PIN_DIGITAL(p) && (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT)))
797+
#define IS_PIN_SERIAL(p) (digitalPinHasSerial(p) && !pinIsSerial(p))
798+
#define PIN_TO_DIGITAL(p) (p)
799+
#define PIN_TO_ANALOG(p) (p-A0)
800+
#define PIN_TO_PWM(p) (p)
801+
#define PIN_TO_SERVO(p) (p)
802+
#define DEFAULT_PWM_RESOLUTION PWM_RESOLUTION
803+
783804
// Adafruit Bluefruit nRF52 boards
784805
#elif defined(ARDUINO_NRF52_ADAFRUIT)
785806
#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS
@@ -798,7 +819,6 @@ writePort(port, value, bitmask): Write an 8 bit port.
798819
#define PIN_TO_PWM(p) (p)
799820
#define PIN_TO_SERVO(p) (p)
800821

801-
802822
// anything else
803823
#else
804824
#error "Please edit Boards.h with a hardware abstraction for this board"

libraries/Firmata/utility/EthernetClientStream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ EthernetClientStream::maintain()
130130
connected = host ? client.connect(host, port) : client.connect(ip, port);
131131
if (!connected) {
132132
time_connect = millis();
133-
DEBUG_PRINTLN("connection failed. attempting to reconnect...");
133+
DEBUG_PRINTLN("Connection failed. Attempting to reconnect...");
134134
} else {
135-
DEBUG_PRINTLN("connected");
135+
DEBUG_PRINTLN("Connected");
136136
}
137137
}
138138
return connected;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
* Implementation is in EthernetServerStream.h to avoid linker issues.
3+
*/
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
EthernetServerStream.h
3+
4+
Copyright (C) 2017 Marc Josef Pees. All rights reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
See file LICENSE.txt for further informations on licensing terms.
12+
13+
Last updated July 10th, 2017
14+
*/
15+
16+
#ifndef ETHERNETSERVERSTREAM_H
17+
#define ETHERNETSERVERSTREAM_H
18+
19+
#include <inttypes.h>
20+
#include <Stream.h>
21+
#include <Ethernet.h>
22+
23+
//#define SERIAL_DEBUG
24+
#include "firmataDebug.h"
25+
26+
class EthernetServerStream : public Stream
27+
{
28+
public:
29+
EthernetServerStream(IPAddress localip, uint16_t port);
30+
int available();
31+
int read();
32+
int peek();
33+
void flush();
34+
size_t write(uint8_t);
35+
void maintain(IPAddress localip);
36+
37+
private:
38+
EthernetClient client;
39+
IPAddress localip;
40+
uint16_t port;
41+
bool connected;
42+
bool maintain();
43+
void stop();
44+
45+
protected:
46+
EthernetServer server = EthernetServer(3030);
47+
bool listening = false;
48+
bool connect_client();
49+
};
50+
51+
52+
/*
53+
* EthernetServerStream.cpp
54+
* Copied here as a hack to linker issues with 3rd party board packages that don't properly
55+
* implement the Arduino network APIs.
56+
*/
57+
EthernetServerStream::EthernetServerStream(IPAddress localip, uint16_t port)
58+
: localip(localip),
59+
port(port),
60+
connected(false)
61+
{
62+
}
63+
64+
bool EthernetServerStream::connect_client()
65+
{
66+
if ( connected )
67+
{
68+
if ( client && client.connected() ) return true;
69+
stop();
70+
}
71+
72+
EthernetClient newClient = server.available();
73+
if ( !newClient ) return false;
74+
client = newClient;
75+
connected = true;
76+
DEBUG_PRINTLN("Connected");
77+
return true;
78+
}
79+
80+
int
81+
EthernetServerStream::available()
82+
{
83+
return maintain() ? client.available() : 0;
84+
}
85+
86+
int
87+
EthernetServerStream::read()
88+
{
89+
return maintain() ? client.read() : -1;
90+
}
91+
92+
int
93+
EthernetServerStream::peek()
94+
{
95+
return maintain() ? client.peek() : -1;
96+
}
97+
98+
void EthernetServerStream::flush()
99+
{
100+
if (maintain())
101+
client.flush();
102+
}
103+
104+
size_t
105+
EthernetServerStream::write(uint8_t c)
106+
{
107+
return maintain() ? client.write(c) : 0;
108+
}
109+
110+
void
111+
EthernetServerStream::maintain(IPAddress localip)
112+
{
113+
// ensure the local IP is updated in the case that it is changed by the DHCP server
114+
if (this->localip != localip) {
115+
this->localip = localip;
116+
if (connected)
117+
stop();
118+
}
119+
}
120+
121+
void
122+
EthernetServerStream::stop()
123+
{
124+
if(client)
125+
{
126+
client.stop();
127+
}
128+
connected = false;
129+
}
130+
131+
bool
132+
EthernetServerStream::maintain()
133+
{
134+
if (connect_client()) return true;
135+
136+
stop();
137+
138+
if(!listening)
139+
{
140+
server = EthernetServer(port);
141+
server.begin();
142+
listening = true;
143+
}
144+
return false;
145+
}
146+
147+
#endif /* ETHERNETSERVERSTREAM_H */

0 commit comments

Comments
 (0)