Retained MQTT topics logic developed, unused sources (SD & extra 1-wire) moved to spare folders to save static RAM

This commit is contained in:
2018-03-27 04:14:34 +03:00
parent e4f47cdef3
commit ec840f784a
11 changed files with 102 additions and 58 deletions

457
spare_files/owSwitch.cpp Normal file
View File

@@ -0,0 +1,457 @@
/* Copyright © 2017-2018 Andrey Klimov. (anklimov@gmail.com) All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Homepage: http://lazyhome.ru
GIT: https://github.com/anklimov/lighthub
*/
#include "owSwitch.h"
#include "owTerm.h"
#include <Arduino.h>
#include "utils.h"
int owRead2408(uint8_t* addr) {
uint8_t buf[13];
// PrintBytes(buf, 13, true);
if (!net) return -1;
net->reset();
net->select(addr);
// uint8_t buf[13]; // Put everything in the buffer so we can compute CRC easily.
buf[0] = 0xF0; // Read PIO Registers
buf[1] = 0x88; // LSB address
buf[2] = 0x00; // MSB address
net->write_bytes(buf, 3,1);
net->read_bytes(buf+3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16
net->reset();
if (!OneWire::check_crc16(buf, 11, &buf[11])) {
Serial.print(F("CRC failure in DS2408 at "));
PrintBytes(addr, 8, true);
PrintBytes(buf+3,10);
return -1;
}
return (buf[3]);
}
/*
int read1W(int i)
{
Serial.print("1W requested: ");
Serial.println (i);
int t=-1;
switch (term[i][0]){
case 0x29: // DS2408
t=owRead2408(term[i]);
break;
case 0x28: // Thermomerer
t=sensors.getTempC(term[i]);
}
return t;
}
*/
int ow2408out(DeviceAddress addr,uint8_t cur)
{
if (!net) return -1;
uint8_t buf[5];
net->reset();
net->select(addr);
buf[0] = 0x5A; // Write PIO Registers
buf[1]=cur;
buf[2] = ~buf[1];
net->write_bytes(buf, 3);
net->read_bytes(buf+3, 2);
//net.reset();
PrintBytes(buf, 5);
Serial.print(F(" Out: "));Serial.print(buf[1],BIN);
Serial.print(F(" In: "));Serial.println(buf[4],BIN);
if (buf[3] != 0xAA) {
Serial.print(F("Write failure in DS2408 at "));
PrintBytes(addr, 8, true);
return -2;
}
return buf[4];
}
int cntrl2408(uint8_t* addr, int subchan, int val) {
if (!net) return -1;
uint8_t buf;
int mask,devnum;
if ((devnum=owFind(addr))<0) return -1;
buf=regs[devnum];
Serial.print(F("Current: "));Serial.println(buf,BIN);
mask=0;
int r,f;
switch (subchan) {
case 0:
if ((buf & SW_STAT0) != ((val)?SW_STAT0:0))
{
if (wstat[devnum] & (SW_PULSE0|SW_PULSE_P0))
{
wstat[devnum]|=SW_CHANGED_P0;
Serial.println(F("Rollback 0"));
}
else {
wstat[devnum]|=SW_PULSE0;
regs[devnum] = (ow2408out(addr,(buf | SW_MASK) & ~SW_OUT0) & SW_INMASK) ^ SW_STAT0; ///?
}
}
return 0;
case 1:
if ((buf & SW_STAT1) != ((val)?SW_STAT1:0))
{
if (wstat[devnum] & (SW_PULSE1|SW_PULSE_P1))
{
wstat[devnum]|=SW_CHANGED_P1;
Serial.println(F("Rollback 1"));
}
else {
wstat[devnum]|=SW_PULSE1;
regs[devnum] =(ow2408out(addr,(buf | SW_MASK) & ~SW_OUT1) & SW_INMASK) ^ SW_STAT1; /// -?
}
}
return 0;
/* Assume AUX 0&1 it is INPUTS - no write
case 2:
mask=SW_AUX0;
break;
case 3:
mask=SW_AUX1; */
}
/* Assume AUX 0&1 it is INPUTS - no write
switch (val) {
case 0: buf=(buf | SW_MASK | SW_OUT0 | SW_OUT1) | mask;
break;
default: buf= (buf | SW_MASK | SW_OUT0 | SW_OUT1) & ~mask;
}
regs[devnum] = ow2408out(addr,buf); */
return 0;
}
int cntrl2890(uint8_t* addr, int val) {
uint8_t buf[13];
if (!net) return -1;
// case 0x2C: //Dimmer
Serial.print(F("Update dimmer "));PrintBytes(addr, 8, true);Serial.print(F(" = "));
Serial.println(val);
net->reset();
net->select(addr);
buf[0] = 0x55;
buf[1] = 0x4c;
net->write_bytes(buf, 2);
net->read_bytes(buf+2, 1); // check if buf[2] == val = ok
buf[3]=0x96;
net->write_bytes(buf+3, 1);
net->read_bytes(buf+4, 1); // 0 = updated ok
PrintBytes(buf, 5, true);
net->select(addr);
if (val==-1)
{
buf[0] = 0xF0;
net->write_bytes(buf, 1);
net->read_bytes(buf+1, 2); // check if buf[2] == val = ok
net->reset();
return buf[2];
}
else
{
buf[0] = 0x0F;
buf[1] = val;
net->write_bytes(buf, 2);
net->read_bytes(buf+2, 1); // check if buf[2] == val = ok
buf[3]=0x96;
net->write_bytes(buf+3, 1);
net->read_bytes(buf+4, 1); // 0 = updated ok
net->reset();
PrintBytes(buf, 5, true);
return buf[2];
}
}
#define DS2413_FAMILY_ID 0x3A
#define DS2413_ACCESS_READ 0xF5
#define DS2413_ACCESS_WRITE 0x5A
#define DS2413_ACK_SUCCESS 0xAA
#define DS2413_ACK_ERROR 0xFF
#define DS2413_IN_PinA 1
#define DS2413_IN_LatchA 2
#define DS2413_IN_PinB 4
#define DS2413_IN_LatchB 8
#define DS2413_OUT_PinA 1
#define DS2413_OUT_PinB 2
/*
byte read(void)
{
bool ok = false;
uint8_t results;
oneWire.reset();
oneWire.select(address);
oneWire.write(DS2413_ACCESS_READ);
results = oneWire.read(); / Get the register results /
ok = (!results & 0x0F) == (results >> 4); / Compare nibbles /
results &= 0x0F; / Clear inverted values /
oneWire.reset();
// return ok ? results : -1;
return results;
}
bool write(uint8_t state)
{
uint8_t ack = 0;
/ Top six bits must '1' /
state |= 0xFC;
oneWire.reset();
oneWire.select(address);
oneWire.write(DS2413_ACCESS_WRITE);
oneWire.write(state);
oneWire.write(~state); / Invert data and resend /
ack = oneWire.read(); / 0xAA=success, 0xFF=failure /
if (ack == DS2413_ACK_SUCCESS)
{
oneWire.read(); / Read the status byte /
}
oneWire.reset();
return (ack == DS2413_ACK_SUCCESS ? true : false);
}
*/
int cntrl2413(uint8_t* addr, int subchan, int val) {
bool ok = false;
uint8_t results;
uint8_t cmd;
uint8_t set=0;
uint8_t count =10;
if (!net) return -1;
// case 0x85: //Switch
Serial.print(F("Update switch "));PrintBytes(addr, 8, false); Serial.print(F("/"));Serial.print(subchan);Serial.print(F(" = "));Serial.println(val);
while (count--)
{
net->reset();
net->select(addr);
net->setStrongPullup();
cmd = DS2413_ACCESS_READ;
net->write(cmd);
results = net->read();
Serial.print(F("Got: ")); Serial.println(results,BIN);
//Serial.println((~results & 0x0F),BIN); Serial.println ((results >> 4),BIN);
ok = (~results & 0x0F) == (results >> 4); // Compare nibbles
results &= 0x0F; // Clear inverted values
if (ok) {Serial.println(F("Read ok"));break;} else {Serial.println(F("read Error"));delay(1);}
} //while
if (ok && (val>=0))
{
count=10;
while (count--)
{
net->reset();
net->select(addr);
if (results & DS2413_IN_LatchA) set|=DS2413_OUT_PinA;
if (results & DS2413_IN_LatchB) set|=DS2413_OUT_PinB;
switch (subchan) {
case 0:
if (!val) set|=DS2413_OUT_PinA; else set &= ~DS2413_OUT_PinA;
break;
case 1:
if (!val) set|=DS2413_OUT_PinB; else set &= ~DS2413_OUT_PinB;
};
set |= 0xFC;
Serial.print(F("New: "));Serial.println(set,BIN);
cmd = DS2413_ACCESS_WRITE;
net->write(cmd);
net->write(set);
net->write(~set);
uint8_t ack = net->read(); // 0xAA=success, 0xFF=failure
if (ack == DS2413_ACK_SUCCESS)
{
results=net->read();
Serial.print(F("Updated ok: ")); Serial.println(results,BIN);
ok = (~results & 0x0F) == (results >> 4); // Compare nibbles
{
if (ok)
{Serial.println(F("Readback ok"));
break;}
else {Serial.println(F("readback Error"));delay(1);}
}
results &= 0x0F; // Clear inverted values
}
else Serial.println (F("Write failed"));;
} //while
} //if
return ok ? results : -1;
}
int sensors_ext(void)
{
int t;
switch (term[si][0]){
case 0x29: // DS2408
//Serial.println(wstat[si],BIN);
if (wstat[si] & SW_PULSE0) {
wstat[si]&=~SW_PULSE0;
wstat[si]|=SW_PULSE_P0;
Serial.println(F("Pulse0 in progress"));
return 500;
}
if (wstat[si] & SW_PULSE0_R) {
wstat[si]&=~SW_PULSE0_R;
wstat[si]|=SW_PULSE_P0;
regs[si] =(ow2408out(term[si],(regs[si] | SW_MASK) & ~SW_OUT0) & SW_INMASK) ^ SW_STAT0;
Serial.println(F("Pulse0 in activated"));
return 500;
}
if (wstat[si] & SW_PULSE1) {
wstat[si]&=~SW_PULSE1;
wstat[si]|=SW_PULSE_P1;
Serial.println(F("Pulse1 in progress"));
return 500;
}
if (wstat[si] & SW_PULSE1_R) {
wstat[si]&=~SW_PULSE1_R;
wstat[si]|=SW_PULSE_P1;
regs[si] =(ow2408out(term[si],(regs[si] | SW_MASK) & ~SW_OUT1) & SW_INMASK) ^ SW_STAT1;
Serial.println(F("Pulse0 in activated"));
return 500;
}
if (wstat[si] & SW_PULSE_P0) {
wstat[si]&=~SW_PULSE_P0;
Serial.println(F("Pulse0 clearing"));
ow2408out(term[si],regs[si] | SW_MASK | SW_OUT0);
if (wstat[si] & SW_CHANGED_P0) {
wstat[si]&=~SW_CHANGED_P0;
wstat[si]|=SW_PULSE0_R;
return 500;
}
}
if (wstat[si] & SW_PULSE_P1) {
wstat[si]&=~SW_PULSE_P1;
Serial.println(F("Pulse1 clearing"));
ow2408out(term[si],regs[si] | SW_MASK | SW_OUT1);
if (wstat[si] & SW_CHANGED_P1) {
wstat[si]&=~SW_CHANGED_P1;
wstat[si]|=SW_PULSE1_R;
return 500;
}
}
if (wstat[si])
{
t=owRead2408(term[si]) & SW_INMASK;
if (t!=regs[si]) {
Serial.print(F("DS2408 data = "));
Serial.println(t, BIN);
if (!(wstat[si] & SW_DOUBLECHECK))
{
wstat[si]|=SW_DOUBLECHECK; //suspected
Serial.println(F("DOUBLECHECK"));
return recheck_interval;
}
Serial.println(F("Really Changed"));
if (owChanged) owChanged(si,term[si],t);
regs[si]=t;
}
wstat[si]&=~SW_DOUBLECHECK;
}
break;
case 0x01:
case 0x81:
t=wstat[si];
if (t!=regs[si])
{ Serial.println(F("Changed"));
if (owChanged) owChanged(si,term[si],t);
regs[si]=t;
}
}
si++;
return check_circle;
}

32
spare_files/owSwitch.h Normal file
View File

@@ -0,0 +1,32 @@
/* Copyright © 2017-2018 Andrey Klimov. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Homepage: http://lazyhome.ru
GIT: https://github.com/anklimov/lighthub
e-mail anklimov@gmail.com
*/
//define APU_OFF
#include <DS2482_OneWire.h>
#include <DallasTemperature.h>
int owRead2408(uint8_t* addr);
int ow2408out(DeviceAddress addr,uint8_t cur);
//int read1W(int i);
int cntrl2408(uint8_t* addr, int subchan, int val=-1);
int cntrl2413(uint8_t* addr, int subchan, int val=-1);
int cntrl2890(uint8_t* addr, int val);

View File

@@ -0,0 +1,212 @@
//
// Created by livello on 14.10.17.
//
/*
SD card test
This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS - depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples
created 28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
*/
//template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
#include "sd_card_w5100.h"
#include <SD.h>
#include <SPI.h>
//#include <iostream>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile sdFile;
File myFile;
char *entireFileContents;
// Arduino Ethernet shield: pin 4
const int chipSelect = 4;
void bench() {
/* uint8_t buf[BUF_SIZE];
long maxLatency,minLatency,totalLatency,temp_DHT22;
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
} else {
// if the file didn'temp_DHT22 open, print an error:
Serial.println("error opening test.txt");
}
for (uint16_t i = 0; i < (BUF_SIZE - 2); i++) {
buf[i] = 'A' + (i % 26);
}
buf[BUF_SIZE - 2] = '\r';
buf[BUF_SIZE - 1] = '\n';
Serial.println("File size MB: ");
Serial.print(FILE_SIZE_MB);
Serial.println("Buffer size ");
Serial.print(BUF_SIZE);
Serial.println("Starting write test, please wait.");
// do write test
uint32_t n = FILE_SIZE / sizeof(buf);
Serial.println("write speed and latency");
Serial.print(" speed,max,min,avg");
Serial.print(" KB/Sec,usec,usec,usec");
for (uint8_t nTest = 0; nTest < WRITE_PASS_COUNT; nTest++) {
// myFile.truncate(0);
maxLatency = 0;
minLatency = 9999999;
totalLatency = 0;
temp_DHT22 = millis();
for (uint32_t i = 0; i < n; i++) {
uint32_t m = micros();
if (myFile.write(buf, sizeof(buf)) != sizeof(buf)) {
Serial.println("write failed");
}
m = micros() - m;
if (maxLatency < m) {
maxLatency = m;
}
if (minLatency > m) {
minLatency = m;
}
totalLatency += m;
}
myFile.flush();
temp_DHT22 = millis() - temp_DHT22;
int s = myFile.size();
Serial.println(s / temp_DHT22);
Serial.print(',');
Serial.print(maxLatency);
Serial.print(',');
Serial.print(minLatency);
Serial.print(',');
Serial.print(totalLatency / n);
}*/
myFile.close();
Serial.println("done.");
}
char* sdW5100_readEntireFile(const char *filename) {
SdFile requestedFile;
if(requestedFile.open(sdFile,filename)) {
Serial.println("Success open INDEX.HTM:");
long time_started = millis();
entireFileContents = new char[requestedFile.fileSize()];
requestedFile.read(entireFileContents,requestedFile.fileSize());
Serial.print(millis()-time_started);
Serial.println(" milliseconds takes to read.");
return entireFileContents;
}
else {
Serial.print("Failed sdFile.open ");
Serial.println(filename);
}
return NULL;
}
uint32_t sdW5100_getFileSize(const char *filename){
SdFile requestedFile;
if(requestedFile.open(sdFile,filename))
return requestedFile.fileSize();
else
return 0;
}
void sd_card_w5100_setup() {
Serial.print("\nInitializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(chipSelect, OUTPUT);
if (!card.init(SPI_FULL_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card is inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
sdFile.openRoot(volume);
// list all files in the card with date and size
sdFile.ls(LS_R | LS_DATE | LS_SIZE);
Serial.println(sdW5100_readEntireFile("INDEX.HTM"));
}

View File

@@ -0,0 +1,15 @@
//
// Created by livello on 14.10.17.
//
#include <stdint.h>
#ifndef NODEMCU_STOPWATCH_SD_CARD_W5100_H
#define NODEMCU_STOPWATCH_SD_CARD_W5100_H
#endif //NODEMCU_STOPWATCH_SD_CARD_W5100_H
void sd_card_w5100_setup();
void cidDmp();
void bench();
char* sdW5100_readEntireFile(const char *filename);
uint32_t sdW5100_getFileSize(const char *filename);