primer commit probando arduino, va el .ino de sensor de temperatura y humedad
This commit is contained in:
112
libraries/Firmata/utility/EthernetClientStream.cpp
Normal file
112
libraries/Firmata/utility/EthernetClientStream.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
EthernetClientStream.cpp
|
||||
An Arduino-Stream that wraps an instance of Client reconnecting to
|
||||
the remote-ip in a transparent way. A disconnected client may be
|
||||
recognized by the returnvalues -1 from calls to peek or read and
|
||||
a 0 from calls to write.
|
||||
|
||||
Copyright (C) 2013 Norbert Truchsess. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
See file LICENSE.txt for further informations on licensing terms.
|
||||
|
||||
formatted using the GNU C formatting and indenting
|
||||
*/
|
||||
|
||||
#include "EthernetClientStream.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
//#define SERIAL_DEBUG
|
||||
#include "firmataDebug.h"
|
||||
|
||||
#define MILLIS_RECONNECT 5000
|
||||
|
||||
EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port)
|
||||
: client(client),
|
||||
localip(localip),
|
||||
ip(ip),
|
||||
host(host),
|
||||
port(port),
|
||||
connected(false)
|
||||
{
|
||||
}
|
||||
|
||||
int
|
||||
EthernetClientStream::available()
|
||||
{
|
||||
return maintain() ? client.available() : 0;
|
||||
}
|
||||
|
||||
int
|
||||
EthernetClientStream::read()
|
||||
{
|
||||
return maintain() ? client.read() : -1;
|
||||
}
|
||||
|
||||
int
|
||||
EthernetClientStream::peek()
|
||||
{
|
||||
return maintain() ? client.peek() : -1;
|
||||
}
|
||||
|
||||
void EthernetClientStream::flush()
|
||||
{
|
||||
if (maintain())
|
||||
client.flush();
|
||||
}
|
||||
|
||||
size_t
|
||||
EthernetClientStream::write(uint8_t c)
|
||||
{
|
||||
return maintain() ? client.write(c) : 0;
|
||||
}
|
||||
|
||||
void
|
||||
EthernetClientStream::maintain(IPAddress localip)
|
||||
{
|
||||
// temporary hack to Firmata to compile for Intel Galileo
|
||||
// the issue is documented here: https://github.com/firmata/arduino/issues/218
|
||||
#if !defined(ARDUINO_LINUX)
|
||||
if (this->localip!=localip)
|
||||
{
|
||||
this->localip = localip;
|
||||
if (connected)
|
||||
stop();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
EthernetClientStream::stop()
|
||||
{
|
||||
client.stop();
|
||||
connected = false;
|
||||
time_connect = millis();
|
||||
}
|
||||
|
||||
bool
|
||||
EthernetClientStream::maintain()
|
||||
{
|
||||
if (client && client.connected())
|
||||
return true;
|
||||
|
||||
if (connected)
|
||||
{
|
||||
stop();
|
||||
}
|
||||
else if (millis()-time_connect >= MILLIS_RECONNECT)
|
||||
{
|
||||
connected = host ? client.connect(host,port) : client.connect(ip,port);
|
||||
if (!connected) {
|
||||
time_connect = millis();
|
||||
DEBUG_PRINTLN("connection failed");
|
||||
} else {
|
||||
DEBUG_PRINTLN("connected");
|
||||
}
|
||||
}
|
||||
return connected;
|
||||
}
|
52
libraries/Firmata/utility/EthernetClientStream.h
Normal file
52
libraries/Firmata/utility/EthernetClientStream.h
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
EthernetClientStream.h
|
||||
An Arduino-Stream that wraps an instance of Client reconnecting to
|
||||
the remote-ip in a transparent way. A disconnected client may be
|
||||
recognized by the returnvalues -1 from calls to peek or read and
|
||||
a 0 from calls to write.
|
||||
|
||||
Copyright (C) 2013 Norbert Truchsess. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
See file LICENSE.txt for further informations on licensing terms.
|
||||
|
||||
formatted using the GNU C formatting and indenting
|
||||
*/
|
||||
|
||||
#ifndef ETHERNETCLIENTSTREAM_H
|
||||
#define ETHERNETCLIENTSTREAM_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <Stream.h>
|
||||
#include <Client.h>
|
||||
#include <IPAddress.h>
|
||||
|
||||
class EthernetClientStream : public Stream
|
||||
{
|
||||
public:
|
||||
EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port);
|
||||
int available();
|
||||
int read();
|
||||
int peek();
|
||||
void flush();
|
||||
size_t write(uint8_t);
|
||||
void maintain(IPAddress localip);
|
||||
|
||||
private:
|
||||
Client &client;
|
||||
IPAddress localip;
|
||||
IPAddress ip;
|
||||
const char* host;
|
||||
uint16_t port;
|
||||
bool connected;
|
||||
uint32_t time_connect;
|
||||
bool maintain();
|
||||
void stop();
|
||||
};
|
||||
|
||||
#endif
|
14
libraries/Firmata/utility/firmataDebug.h
Normal file
14
libraries/Firmata/utility/firmataDebug.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef FIRMATA_DEBUG_H
|
||||
#define FIRMATA_DEBUG_H
|
||||
|
||||
#ifdef SERIAL_DEBUG
|
||||
#define DEBUG_BEGIN(baud) Serial.begin(baud); while(!Serial) {;}
|
||||
#define DEBUG_PRINTLN(x) Serial.println (x)
|
||||
#define DEBUG_PRINT(x) Serial.print (x)
|
||||
#else
|
||||
#define DEBUG_BEGIN(baud)
|
||||
#define DEBUG_PRINTLN(x)
|
||||
#define DEBUG_PRINT(x)
|
||||
#endif
|
||||
|
||||
#endif /* FIRMATA_DEBUG_H */
|
120
libraries/Firmata/utility/serialUtils.h
Normal file
120
libraries/Firmata/utility/serialUtils.h
Normal file
@ -0,0 +1,120 @@
|
||||
/*
|
||||
serialUtils.h - Definitions and utility functions for the Serial feature.
|
||||
Copyright (c) 2015 Jeff Hoefs. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
See file LICENSE.txt for further informations on licensing terms.
|
||||
|
||||
Last updated by Jeff Hoefs: October 3rd, 2015
|
||||
*/
|
||||
|
||||
#ifndef SERIAL_UTILS_H
|
||||
#define SERIAL_UTILS_H
|
||||
|
||||
// Serial port Ids
|
||||
#define HW_SERIAL0 0x00
|
||||
#define HW_SERIAL1 0x01
|
||||
#define HW_SERIAL2 0x02
|
||||
#define HW_SERIAL3 0x03
|
||||
// extensible up to 0x07
|
||||
|
||||
#define SW_SERIAL0 0x08
|
||||
#define SW_SERIAL1 0x09
|
||||
#define SW_SERIAL2 0x0A
|
||||
#define SW_SERIAL3 0x0B
|
||||
// extensible up to 0x0F
|
||||
|
||||
#define SERIAL_PORT_ID_MASK 0x0F
|
||||
#define MAX_SERIAL_PORTS 8
|
||||
#define SERIAL_READ_ARR_LEN 12
|
||||
|
||||
// map configuration query response resolution value to serial pin type
|
||||
#define RES_RX1 0x02
|
||||
#define RES_TX1 0x03
|
||||
#define RES_RX2 0x04
|
||||
#define RES_TX2 0x05
|
||||
#define RES_RX3 0x06
|
||||
#define RES_TX3 0x07
|
||||
|
||||
// Serial command bytes
|
||||
#define SERIAL_CONFIG 0x10
|
||||
#define SERIAL_WRITE 0x20
|
||||
#define SERIAL_READ 0x30
|
||||
#define SERIAL_REPLY 0x40
|
||||
#define SERIAL_CLOSE 0x50
|
||||
#define SERIAL_FLUSH 0x60
|
||||
#define SERIAL_LISTEN 0x70
|
||||
|
||||
// Serial read modes
|
||||
#define SERIAL_READ_CONTINUOUSLY 0x00
|
||||
#define SERIAL_STOP_READING 0x01
|
||||
#define SERIAL_MODE_MASK 0xF0
|
||||
|
||||
struct serial_pins {
|
||||
uint8_t rx;
|
||||
uint8_t tx;
|
||||
};
|
||||
|
||||
/*
|
||||
* Get the serial serial pin type (RX1, TX1, RX2, TX2, etc) for the specified pin.
|
||||
*/
|
||||
inline uint8_t getSerialPinType(uint8_t pin) {
|
||||
#if defined(PIN_SERIAL_RX)
|
||||
// TODO when use of HW_SERIAL0 is enabled
|
||||
#endif
|
||||
#if defined(PIN_SERIAL1_RX)
|
||||
if (pin == PIN_SERIAL1_RX) return RES_RX1;
|
||||
if (pin == PIN_SERIAL1_TX) return RES_TX1;
|
||||
#endif
|
||||
#if defined(PIN_SERIAL2_RX)
|
||||
if (pin == PIN_SERIAL2_RX) return RES_RX2;
|
||||
if (pin == PIN_SERIAL2_TX) return RES_TX2;
|
||||
#endif
|
||||
#if defined(PIN_SERIAL3_RX)
|
||||
if (pin == PIN_SERIAL3_RX) return RES_RX3;
|
||||
if (pin == PIN_SERIAL3_TX) return RES_TX3;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the RX and TX pins numbers for the specified HW serial port.
|
||||
*/
|
||||
inline serial_pins getSerialPinNumbers(uint8_t portId) {
|
||||
serial_pins pins;
|
||||
switch (portId) {
|
||||
#if defined(PIN_SERIAL_RX)
|
||||
// case HW_SERIAL0:
|
||||
// // TODO when use of HW_SERIAL0 is enabled
|
||||
// break;
|
||||
#endif
|
||||
#if defined(PIN_SERIAL1_RX)
|
||||
case HW_SERIAL1:
|
||||
pins.rx = PIN_SERIAL1_RX;
|
||||
pins.tx = PIN_SERIAL1_TX;
|
||||
break;
|
||||
#endif
|
||||
#if defined(PIN_SERIAL2_RX)
|
||||
case HW_SERIAL2:
|
||||
pins.rx = PIN_SERIAL2_RX;
|
||||
pins.tx = PIN_SERIAL2_TX;
|
||||
break;
|
||||
#endif
|
||||
#if defined(PIN_SERIAL3_RX)
|
||||
case HW_SERIAL3:
|
||||
pins.rx = PIN_SERIAL3_RX;
|
||||
pins.tx = PIN_SERIAL3_TX;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
pins.rx = 0;
|
||||
pins.tx = 0;
|
||||
}
|
||||
return pins;
|
||||
}
|
||||
|
||||
#endif /* SERIAL_UTILS_H */
|
Reference in New Issue
Block a user