#include "powerserial.h" PowerSerial PowerSerial::power; PowerSerial PowerSerial::solar; const char* PowerSerial::PATTERN = "1-0:n.8.1*255("; void PowerSerial::begin (const char* _name, HardwareSerial& _serial, unsigned long _maxage, volatile uint8_t & ucsrc, uint8_t ucsz0, uint8_t upm1) { name = _name; serial = &_serial; maxage = _maxage; serial->begin(9600); ucsrc = ucsrc & ~_BV(ucsz0) | _BV(upm1); pp = PATTERN; count = 0; kwh[0] = kwh[1] = 0; } void PowerSerial::parseMe () { unsigned long newms = millis(); if (maxage && (kwh[0] || kwh[1]) && newms>ms+maxage) { kwh[0] = kwh[1] = 0; } int c; if ((c = serial->read())>0) { if (c=='/') { light.readout(); kwhtemp[0] = kwhtemp[1] = 0; } else if (c=='!') { kwh[0] = kwhtemp[0]; kwh[1] = kwhtemp[1]; ms = newms; count++; } else if (*pp=='\0') { if (c==')') { pp = PATTERN; } else if (c>='0' && c<='9') { kwhtemp[n] = kwhtemp[n]*10+(c-'0'); } else if (c=='.') { } else { kwhtemp[n] = 0; pp = PATTERN; } } else if (*pp=='n' && c>='1' && c<='2') { n = c-'1'; pp++; } else if (c==*pp) { pp++; } else { pp = PATTERN; } } } void interrupt0() { PowerSerial::power.light.interrupt(); } void interrupt1() { PowerSerial::solar.light.interrupt(); } void PowerSerial::setup () { power.begin("Power",Serial1,4500,UCSR1C,UCSZ10,UPM11); solar.begin("Solar",Serial2,4500,UCSR2C,UCSZ20,UPM21); attachInterrupt(0,interrupt0,RISING); attachInterrupt(1,interrupt1,RISING); } void PowerSerial::parse () { power.parseMe(); solar.parseMe(); }