RPi: Add support for I2C

Add MyHw file for RaspberryPi.

Fix compilation warnings in x86.

Fix some error messages.
This commit is contained in:
Marcelo Aquino
2016-10-16 13:40:23 -02:00
parent 3d51b31e71
commit 69ac4b759a
21 changed files with 817 additions and 147 deletions
+12
View File
@@ -18,6 +18,18 @@
using namespace rpi_util;
#endif
#undef PSTR
#define PSTR(x) (x)
#undef F
#define F(x) (x)
#define PROGMEM __attribute__(( section(".progmem.data") ))
#define vsnprintf_P(...) vsnprintf( __VA_ARGS__ )
#define snprintf_P(...) snprintf( __VA_ARGS__ )
#define memcpy_P memcpy
#define pgm_read_byte(p) (*(p))
#define pgm_read_dword(p) (*(p))
#define pgm_read_byte_near(p) (*(p))
#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398
#define TWO_PI 6.283185307179586476925286766559
+3 -3
View File
@@ -84,7 +84,7 @@ void EthernetServer::begin(IPAddress address)
}
if (p == NULL) {
mys_log(LOG_ERR, "failed to bind\n");
mys_log(LOG_ERR, "Failed to bind!\n");
freeaddrinfo(servinfo);
return;
}
@@ -142,7 +142,7 @@ size_t EthernetServer::write(const uint8_t *buffer, size_t size)
client.stop();
clients[i] = clients.back();
clients.pop_back();
mys_log(LOG_DEBUG, "Client disconnected.");
mys_log(LOG_DEBUG, "Client disconnected.\n");
}
}
@@ -194,7 +194,7 @@ void EthernetServer::_accept()
}
}
if (no_free_slots) {
mys_log(LOG_DEBUG, "Max number of ethernet clients reached.");
mys_log(LOG_DEBUG, "Max number of ethernet clients reached.\n");
return;
}
}
+9
View File
@@ -34,10 +34,19 @@
class Print {
private:
int write_error;
size_t printNumber(unsigned long n, uint8_t base);
size_t printFloat(double number, uint8_t digits);
protected:
void setWriteError(int err = 1) { write_error = err; }
public:
Print() : write_error(0) {}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(0); }
virtual size_t write(uint8_t) = 0;
size_t write(const char *str) {
if (str == NULL)
+18 -4
View File
@@ -194,18 +194,32 @@ int SerialPort::read()
{
unsigned char c;
::read(sd, &c, 1);
return c;
int ret = ::read(sd, &c, 1);
if (ret < 0) {
mys_log(LOG_ERR, "Serial - read failed: %s\n", strerror(errno));
} else if (ret == 1) {
return c;
}
return -1;
}
size_t SerialPort::write(uint8_t b)
{
return ::write(sd, &b, 1);
int ret = ::write(sd, &b, 1);
if (ret < 0) {
mys_log(LOG_ERR, "Serial - write failed: %s\n", strerror(errno));
}
return ret;
}
size_t SerialPort::write(const uint8_t *buffer, size_t size)
{
return ::write(sd, buffer, size);
int ret = ::write(sd, buffer, size);
if (ret < 0) {
mys_log(LOG_ERR, "Serial - write failed: %s\n", strerror(errno));
}
return ret;
}
int SerialPort::peek()
+129
View File
@@ -0,0 +1,129 @@
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Marcelo Aquino <marceloaqno@gmail.org>
* Copyright (C) 2016 Marcelo Aquino
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <string.h>
#include "log.h"
#include "SoftEeprom.h"
SoftEeprom::SoftEeprom(const char *fileName, size_t length)
{
struct stat fileInfo;
_fileName = strdup(fileName);
if (_fileName == NULL) {
perror("Error: ");
exit(1);
}
_length = length;
_values = new uint8_t[_length];
for (size_t i = 0; i < _length; ++i) {
_values[i] = 0xFF;
}
if (stat(_fileName, &fileInfo) != 0) {
//File does not exist. Create it.
mys_log(LOG_INFO, "Config file %s does not exist, creating new config file.\n", _fileName);
std::ofstream myFile(_fileName, std::ios::out | std::ios::binary);
if (!myFile) {
mys_log(LOG_ERR, "Unable to create config file %s.\n", _fileName);
exit(1);
}
myFile.write((const char*)_values, _length);
myFile.close();
} else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) {
mys_log(LOG_ERR, "Config file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n", _fileName, _length);
exit(1);
} else {
//Read config into local memory.
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
if (!myFile) {
mys_log(LOG_ERR, "Unable to open config to file %s for reading.\n", _fileName);
exit(1);
}
myFile.read((char*)_values, _length);
myFile.close();
}
}
SoftEeprom::~SoftEeprom()
{
delete[] _values;
free(_fileName);
}
void SoftEeprom::readBlock(void* buf, void* addr, size_t length)
{
static bool config_to_mem = false;
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
if (!config_to_mem) {
//Read config into local memory.
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
if (!myFile) {
mys_log(LOG_ERR, "Unable to open config to file %s for reading.\n", _fileName);
exit(1);
}
myFile.read((char*)_values, _length);
myFile.close();
config_to_mem = true;
}
if (length && offs + length <= _length) {
memcpy(buf, _values+offs, length);
}
}
void SoftEeprom::writeBlock(void* buf, void* addr, size_t length)
{
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
if (length && offs + length <= _length) {
memcpy(_values+offs, buf, length);
std::ofstream myFile(_fileName, std::ios::out | std::ios::in | std::ios::binary);
if (!myFile) {
mys_log(LOG_ERR, "Unable to write config to file %s.\n", _fileName);
return;
}
myFile.seekp(offs);
myFile.write((const char*)buf, length);
myFile.close();
}
}
uint8_t SoftEeprom::readByte(int addr)
{
uint8_t value = 0xFF;
readBlock(&value, reinterpret_cast<void*>(addr), 1);
return value;
}
void SoftEeprom::writeByte(int addr, uint8_t value)
{
uint8_t curr = readByte(addr);
if (curr != value) {
writeBlock(&value, reinterpret_cast<void*>(addr), 1);
}
}
+81
View File
@@ -0,0 +1,81 @@
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Marcelo Aquino <marceloaqno@gmail.org>
* Copyright (C) 2016 Marcelo Aquino
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*/
/**
* This a software emulation of EEPROM that uses a file for data storage.
* A copy of the eeprom values are also held in memory for faster reading.
*/
#ifndef SoftEeprom_h
#define SoftEeprom_h
#include <stdint.h>
/**
* SoftEeprom class
*/
class SoftEeprom {
private:
size_t _length; //!< @brief Eeprom max size.
char *_fileName; //!< @brief file where the eeprom values are stored.
uint8_t *_values; //!< @brief copy of the eeprom values held in memory for a faster reading.
public:
/**
* @brief SoftEeprom constructor.
*/
SoftEeprom(const char *fileName, size_t length);
/**
* @brief SoftEeprom destructor.
*/
~SoftEeprom();
/**
* @brief Read a block of bytes from eeprom.
*
* @param buf buffer to copy to.
* @param addr eeprom address to read from.
* @param length number of bytes to read.
*/
void readBlock(void* buf, void* addr, size_t length);
/**
* @brief Write a block of bytes to eeprom.
*
* @param buf buffer to read from.
* @param addr eeprom address to write to.
* @param length number of bytes to write.
*/
void writeBlock(void* buf, void* addr, size_t length);
/**
* @brief Read a byte from eeprom.
*
* @param addr eeprom address to read from.
* @return the read byte.
*/
uint8_t readByte(int addr);
/**
* @brief Write a byte to eeprom.
*
* @param addr eeprom address to write to.
* @param value to write.
*/
void writeByte(int addr, uint8_t value);
};
#endif