Compare commits

..

2 Commits

7 changed files with 368 additions and 0 deletions

View File

@ -0,0 +1,106 @@
#include "PCF8574.h"
PCF8574::PCF8574(uint8_t address, TwoWire &twc) : _twc(twc), _address(address), _pinConfig(0b11111111), _pddr(0b00000000) //Inputs by default
{
}
boolean PCF8574::begin()
{
_twc.begin();
_twc.beginTransmission(_address);
_twc.write(_pinConfig);
return _twc.endTransmission() == 0;
}
void PCF8574::pinMode(Pin pin, boolean mode)
{
uint8_t mask = mode ? pin : pin; //0 INPUT - 1 OUTPUT
if(mode)//OUTPUT
{
_pinConfig = _pinConfig | mask;
_pddr = _pddr | pin;
}
else
{
_pinConfig = _pinConfig | mask;
_pddr = _pddr & ~pin;
}
_pinConfig = _pinConfig | mask;
_twc.beginTransmission(_address);
_twc.write(_pinConfig);
_twc.endTransmission();
}
void PCF8574::digitalWrite(Pin pin, boolean mode)
{
//We first check that the pin is an output
if((_pddr & pin) == 0)
return;
uint8_t mask = mode ? pin : ~pin; //0 LOW, 1 HIGH
if(mode)
_pinConfig = _pinConfig | mask;
else
_pinConfig = _pinConfig & mask;
_twc.beginTransmission(_address);
_twc.write(_pinConfig);
_twc.endTransmission();
}
void PCF8574::togglePin(Pin pin)
{
//We first check that the pin is an output
if((_pddr & pin) == 0)
return;
_pinConfig = (_pinConfig & pin) == 0 ? _pinConfig | pin : _pinConfig & ~pin;
_twc.beginTransmission(_address);
_twc.write(_pinConfig);
_twc.endTransmission();
}
boolean PCF8574::digitalRead(Pin pin)
{
uint8_t reg = 0b00000000;
_twc.requestFrom((uint8_t)_address,(uint8_t)1,(uint8_t)true);
while(_twc.available())reg = _twc.read();
return (reg & pin) == 0 ? 0 : 1;
}
void PCF8574::digitalReadAll(boolean array[8])
{
uint8_t reg = 0b00000000;
_twc.requestFrom((uint8_t)_address,(uint8_t)1,(uint8_t)true);
while(_twc.available())reg = _twc.read();
array[0] = (reg & P0) == 0 ? 0 : 1;
array[1] = (reg & P1) == 0 ? 0 : 1;
array[2] = (reg & P2) == 0 ? 0 : 1;
array[3] = (reg & P3) == 0 ? 0 : 1;
array[4] = (reg & P4) == 0 ? 0 : 1;
array[5] = (reg & P5) == 0 ? 0 : 1;
array[6] = (reg & P6) == 0 ? 0 : 1;
array[7] = (reg & P7) == 0 ? 0 : 1;
}
boolean PCF8574::getPinMode(Pin pin)
{
return _pddr & pin == 0 ? INPUT : OUTPUT;
}
void PCF8574::getPinModeAll(boolean array[8])
{
array[0] = (_pddr & P0) == 0 ? INPUT : OUTPUT;
array[1] = (_pddr & P1) == 0 ? INPUT : OUTPUT;
array[2] = (_pddr & P2) == 0 ? INPUT : OUTPUT;
array[3] = (_pddr & P3) == 0 ? INPUT : OUTPUT;
array[4] = (_pddr & P4) == 0 ? INPUT : OUTPUT;
array[5] = (_pddr & P5) == 0 ? INPUT : OUTPUT;
array[6] = (_pddr & P6) == 0 ? INPUT : OUTPUT;
array[7] = (_pddr & P7) == 0 ? INPUT : OUTPUT;
}

View File

@ -0,0 +1,34 @@
#ifndef PCF8574_H
#define PCF8574_H
#include <Arduino.h>
#include <Wire.h>
/***
* A very simple class to drive a PCF8574 chip
*
* Anatole SCHRAMM-HENRY -- Th3maz1ng
***/
class PCF8574
{
public:
enum Pin{P0 = 1, P1 = 2, P2 = 4, P3 = 8, P4 = 16, P5 = 32, P6 = 64, P7 = 128};
PCF8574(uint8_t address, TwoWire &twc = Wire);
boolean begin();
void pinMode(Pin pin, boolean mode = INPUT);
void digitalWrite(Pin pin, boolean mode);
boolean digitalRead(Pin pin);
void digitalReadAll(boolean array[8]);
boolean getPinMode(Pin pin);
void getPinModeAll(boolean array[8]);
void togglePin(Pin pin);
private:
TwoWire &_twc;
uint8_t _address;
uint8_t _pinConfig;
uint8_t _pddr;
protected:
};
#endif //PCF8574_H

View File

@ -0,0 +1,22 @@
#include "PCF8574.h"
//Example program with blink on P0 and digitalRead on P1 - P7
PCF8574 pcf(0x27);
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pcf.begin();
pcf.pinMode(PCF8574::P0,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(pcf.digitalRead(PCF8574::P0));Serial.print(pcf.digitalRead(PCF8574::P1));Serial.print(pcf.digitalRead(PCF8574::P2));Serial.print(pcf.digitalRead(PCF8574::P3));Serial.print(pcf.digitalRead(PCF8574::P4));Serial.print(pcf.digitalRead(PCF8574::P5));Serial.print(pcf.digitalRead(PCF8574::P6));Serial.println(pcf.digitalRead(PCF8574::P7));
//pcf.digitalRead(PCF8574::P0);
pcf.digitalWrite(PCF8574::P0, HIGH);
delay(200);
pcf.digitalWrite(PCF8574::P0, LOW);
delay(200);
}

View File

@ -0,0 +1,31 @@
#######################################
# Syntax Coloring Map For PCF8574
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
PCF8574 KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
digitalReadAll KEYWORD2
getPinMode KEYWORD2
getPinModeAll KEYWORD2
togglePin KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
P0 LITERAL1
P1 LITERAL1
P2 LITERAL1
P3 LITERAL1
P4 LITERAL1
P5 LITERAL1
P6 LITERAL1
P7 LITERAL1

20
src/tests/definition.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef DEFINITION_H
#define DEFINITION_H
#include "packet_format.h"
//Payload structure
typedef struct
{
uint16_t id;
HEADER_e header : 6;
unsigned int ldr : 10;
float battery;
float bmpTemp;
float bmpPress;
float humidity;
float compensatedHumidity;
float htuTemp;
} DataPacket __attribute__((__packed__));
#endif //DEFINITION_H

View File

@ -0,0 +1,6 @@
#ifndef PACKET_FORMAT_H
#define PACKET_FORMAT_H
enum HEADER_e {WEATHER_STATION = 0};
#endif //PACKET_FORMAT_H

149
src/tests/tests.ino Normal file
View File

@ -0,0 +1,149 @@
/**
* Author : Anatole SCHRAMM-HENRY
* Created the : 13/06/2021
* This TEST program aims to verify all the basic functions of the gateway by displaying some output to a serial console :
* - Testing that both NRF work correctly
* - Testing the LED status lights
* - Testing the NRF IRQs detection with the PCF8574
*/
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <PCF8574.h>
#include "RF24.h"
#include "definition.h"
#define NRF_1_CE 15 //chip enable
#define NRF_2_CE 16 //chip enable
#define NRF_1_CS 5 //chip select
#define NRF_2_CS 4 //chip select
#define CHAN 108 //0-125
#define RECV_CHECK 100 //We test every 100 ms if we received something
#define WIFI_CHECK_TIMEOUT 20000
DataPacket p;
RF24 NRF_1(NRF_1_CE, NRF_1_CS);
RF24 NRF_2(NRF_2_CE, NRF_2_CS);
PCF8574 PCF(0x20);
const uint8_t ADDR[] = "WEST1";
uint64_t timeStamp(0), recvSlot(0), packetTimeout(0);
uint32_t freeMem(0);
uint16_t biggestContigMemBlock(0);
uint8_t frag(0);
void setup() {
//We do not need to read on the serial bus
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
Serial.println("Setup begin");
//To prevent wear and tear on the flash mem
WiFi.persistent(false);
WiFi.disconnect(true);
WiFi.softAPdisconnect(true);
Serial.printf("NRF 1 %s\n",NRF_1.begin() ? "started" : "error");
if(!NRF_1.isChipConnected())
Serial.println("NRF 1 is missing");
else
Serial.println("NRF 1 is detected");
NRF_1.setChannel(CHAN);
NRF_1.setPALevel(RF24_PA_MIN);
NRF_1.setDataRate(RF24_250KBPS);
NRF_1.setRetries(8,15);
NRF_1.openReadingPipe(1, ADDR);
NRF_1.startListening();
Serial.printf("NRF 2 %s\n",NRF_2.begin() ? "started" : "error");
if(!NRF_2.isChipConnected())
Serial.println("NRF 2 is missing");
else
Serial.println("NRF 2 is detected");
NRF_2.setChannel(CHAN);
NRF_2.setPALevel(RF24_PA_MIN);
NRF_2.setDataRate(RF24_250KBPS);
NRF_2.setRetries(8,15);
NRF_2.openReadingPipe(1, ADDR);
NRF_2.startListening();
memset(&p, 0, sizeof p);
//Setting the I2C pins and the PCF8574
Wire.begin(0,2);
Serial.printf("PCF %s\n", PCF.begin() ? "found" : "not found");
PCF.pinMode(PCF8574::P2, OUTPUT);
PCF.pinMode(PCF8574::P3, OUTPUT);
PCF.pinMode(PCF8574::P4, OUTPUT);
Serial.println("End setup");
}
void loop() {
// put your main code here, to run repeatedly:
if(millis() - recvSlot > RECV_CHECK)
{
if(NRF_1.available()) //Then we read the incoming data
{
NRF_1.read(&p, sizeof(p));
Serial.printf("NRF 1 Received : \n");
debugStruct(&p);
}
if(NRF_2.available()) //Then we read the incoming data
{
NRF_2.read(&p, sizeof(p));
Serial.printf("NRF 2 Received : \n");
debugStruct(&p);
}
recvSlot = millis();
}
//Here we check if we are still connected
if(millis() - timeStamp > WIFI_CHECK_TIMEOUT)
{
PCF.togglePin(PCF8574::P2);
PCF.togglePin(PCF8574::P3);
PCF.togglePin(PCF8574::P4);
ESP.getHeapStats(&freeMem, &biggestContigMemBlock, &frag);
printf("Memory Info :\n - Free Mem > %u\n - Heap frag > %u\n - Max block > %u\n", freeMem, frag, biggestContigMemBlock);
timeStamp = millis();
}
}
void debugStruct(DataPacket *p)
{
Serial.println("##############DATA##############");
Serial.print("ID : ");
Serial.println(p->id);
Serial.print("HEADER : ");
Serial.println(p->header);
Serial.printf("BATT : %.5f V\n", p->battery);
Serial.print("LDR : ");
Serial.println(p->ldr);
Serial.print("BMP TEMP : ");
Serial.print(p->bmpTemp);
Serial.println(" *C");
Serial.print("BMP PRESS : ");
Serial.print(p->bmpPress);
Serial.println(" Pa");
Serial.print("HUM : ");
Serial.print(p->humidity);
Serial.println(" %");
Serial.print("COM HUM : ");
Serial.print(p->compensatedHumidity);
Serial.println(" %");
Serial.print("HTU TEMP : ");
Serial.print(p->htuTemp);
Serial.println(" *C");
}