Update ArduinoPilotMicro433WindNG.ino

This commit is contained in:
AK-Homberger
2019-07-21 11:57:24 +02:00
committed by GitHub
parent 196419e14e
commit 981468fc0d

View File

@@ -1,206 +1,220 @@
#include <avr/pgmspace.h> /*
#include <RCSwitch.h> This code is free software; you can redistribute it and/or
#include <SPI.h> modify it under the terms of the GNU Lesser General Public
#include <Wire.h> License as published by the Free Software Foundation; either
#include <Adafruit_GFX.h> version 2.1 of the License, or (at your option) any later version.
#include <Adafruit_SSD1306.h> This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
#define OLED_RESET 4 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Adafruit_SSD1306 display(OLED_RESET); Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
RCSwitch mySwitch = RCSwitch(); License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
const long unsigned int Key1 PROGMEM = 0000001; // Change values to individual values programmed to remote control */
const long unsigned int Key2 PROGMEM = 0000002;
const long unsigned int Key3 PROGMEM = 0000003; #include <avr/pgmspace.h>
const long unsigned int Key4 PROGMEM = 0000004; #include <RCSwitch.h>
#include <SPI.h>
// Seatalk datagrams #include <Wire.h>
#include <Adafruit_GFX.h>
const PROGMEM uint16_t ST_NMEA_BridgeID[] = { 0x190, 0x00, 0xA3 }; #include <Adafruit_SSD1306.h>
const PROGMEM uint16_t ST_Minus_1[] = { 0x186, 0x21, 0x05, 0xFA }; #define OLED_RESET 4
const PROGMEM uint16_t ST_Minus_10[] = { 0x186, 0x21, 0x06, 0xF9 }; Adafruit_SSD1306 display(OLED_RESET);
const PROGMEM uint16_t ST_Plus_1[] = { 0x186, 0x21, 0x07, 0xF8 };
const PROGMEM uint16_t ST_Plus_10[] = { 0x186, 0x21, 0x08, 0xF7 }; RCSwitch mySwitch = RCSwitch();
const PROGMEM uint16_t ST_BeepOn[] = { 0x1A8, 0x53, 0x80, 0x00, 0x00, 0xD3 }; const long unsigned int Key1 PROGMEM = 0000001; // Change values to individual values programmed to remote control
const PROGMEM uint16_t ST_BeepOff[] = { 0x1A8, 0x43, 0x80, 0x00, 0x00, 0xC3 }; const long unsigned int Key2 PROGMEM = 0000002;
const long unsigned int Key3 PROGMEM = 0000003;
boolean blink = true; const long unsigned int Key4 PROGMEM = 0000004;
long unsigned int timer=0;
long unsigned int timer1=0; // Seatalk datagrams
long unsigned int timer2=0;
const PROGMEM uint16_t ST_NMEA_BridgeID[] = { 0x190, 0x00, 0xA3 };
boolean sendDatagram(const uint16_t data[]) {
int i = 0; int j = 0; const PROGMEM uint16_t ST_Minus_1[] = { 0x186, 0x21, 0x05, 0xFA };
boolean ok = true; const PROGMEM uint16_t ST_Minus_10[] = { 0x186, 0x21, 0x06, 0xF9 };
int bytes; const PROGMEM uint16_t ST_Plus_1[] = { 0x186, 0x21, 0x07, 0xF8 };
unsigned int inbyte; const PROGMEM uint16_t ST_Plus_10[] = { 0x186, 0x21, 0x08, 0xF7 };
unsigned int outbyte;
const PROGMEM uint16_t ST_BeepOn[] = { 0x1A8, 0x53, 0x80, 0x00, 0x00, 0xD3 };
bytes = (pgm_read_byte_near(data + 1) & 0x0f) + 3; // Messege length is minimum 3, additional bytes in nibble 4 const PROGMEM uint16_t ST_BeepOff[] = { 0x1A8, 0x43, 0x80, 0x00, 0x00, 0xC3 };
while (j < 5 ) { // CDMA/CD 5 tries boolean blink = true;
while (Serial1.available ()) { // Wait for silence on the bus long unsigned int timer=0;
inbyte = (Serial1.read()); long unsigned int timer1=0;
delay(3); long unsigned int timer2=0;
}
boolean sendDatagram(const uint16_t data[]) {
ok = true; int i = 0; int j = 0;
for (i = 0; (i < bytes) & (ok == true); i++) { // Write and listen to detect collisions boolean ok = true;
outbyte = pgm_read_word_near(data + i); int bytes;
Serial1.write(outbyte); unsigned int inbyte;
delay(3); unsigned int outbyte;
if (Serial1.available ()) { bytes = (pgm_read_byte_near(data + 1) & 0x0f) + 3; // Messege length is minimum 3, additional bytes in nibble 4
inbyte = Serial1.read(); // Not what we sent, collision!
while (j < 5 ) { // CDMA/CD 5 tries
if (inbyte != outbyte) ok = false; while (Serial1.available ()) { // Wait for silence on the bus
} inbyte = (Serial1.read());
else ok = false; // Nothing received delay(3);
} }
if ( ok )return ok; ok = true;
for (i = 0; (i < bytes) & (ok == true); i++) { // Write and listen to detect collisions
j++; // Collision detected outbyte = pgm_read_word_near(data + i);
// Serial.println("CD"); Serial1.write(outbyte);
// Display("Collision", 2); delay(3);
delay(random(2, 50)); // Random wait for next try
} if (Serial1.available ()) {
Display("Send Error", 2); inbyte = Serial1.read(); // Not what we sent, collision!
return false;
} if (inbyte != outbyte) ok = false;
}
else ok = false; // Nothing received
void Display(char *string, int size) }
{
display.clearDisplay(); if ( ok )return ok;
display.setTextSize(size);
display.setCursor(0, 0); j++; // Collision detected
display.println(string); // Serial.println("CD");
display.display(); // Display("Collision", 2);
timer = 0; delay(random(2, 50)); // Random wait for next try
} }
Display("Send Error", 2);
return false;
int checkWind(char * AWS) // Receice apparent wind speed from bus }
{
unsigned int xx;
unsigned int y; void Display(char *string, int size)
unsigned int inbyte; {
int wind = -1; display.clearDisplay();
display.setTextSize(size);
if (Serial1.available ()) { display.setCursor(0, 0);
inbyte = Serial1.read(); display.println(string);
if (inbyte == 0x111) { // AWS Seatalk command - See reference from Thomas Knauf display.display();
delay(3); timer = 0;
inbyte = Serial1.read(); }
if (inbyte == 0x01) { // AWS Setalk command
delay(3);
xx = Serial1.read(); int checkWind(char * AWS) // Receice apparent wind speed from bus
delay(3); {
y = Serial1.read(); unsigned int xx;
wind = (xx & 0x7f) + (y / 10); // Wind speed unsigned int y;
if (wind < 100) itoa (wind , AWS, 10); // Greater 100 must be a receive error unsigned int inbyte;
} int wind = -1;
}
} if (Serial1.available ()) {
return wind; inbyte = Serial1.read();
} if (inbyte == 0x111) { // AWS Seatalk command - See reference from Thomas Knauf
delay(3);
inbyte = Serial1.read();
void setup() if (inbyte == 0x01) { // AWS Setalk command
{ delay(3);
Serial.begin( 9600 ); // Serial out put for function checks with PC xx = Serial1.read();
Serial1.begin( 4800, SERIAL_9N1 ); // Set the Seatalk modus - 9 bit delay(3);
Serial1.setTimeout(5); y = Serial1.read();
wind = (xx & 0x7f) + (y / 10); // Wind speed
mySwitch.enableReceive(4); // RF Receiver on inerrupt 4 => that is pin 7 on Micro if (wind < 100) itoa (wind , AWS, 10); // Greater 100 must be a receive error
}
pinMode(9, OUTPUT); // LED to show if keys are received }
digitalWrite(9, HIGH); }
return wind;
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64 from Conrad else 3D) }
display.setTextColor(WHITE);
Display("Start", 4);
void setup()
sendDatagram(ST_NMEA_BridgeID); // Send NMEA Seatakl BridgeID to make Seatalk to Seatalk NG converter happy {
} Serial.begin( 9600 ); // Serial out put for function checks with PC
Serial1.begin( 4800, SERIAL_9N1 ); // Set the Seatalk modus - 9 bit
Serial1.setTimeout(5);
void loop()
{ mySwitch.enableReceive(4); // RF Receiver on inerrupt 4 => that is pin 7 on Micro
int i;
char AWS[4] = ""; pinMode(9, OUTPUT); // LED to show if keys are received
digitalWrite(9, HIGH);
timer++;timer1++;timer2++;
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64 from Conrad else 3D)
if (timer > 200000 ) { display.setTextColor(WHITE);
Display("---", 7); // Show --- after about two seconds when no wind data is received Display("Start", 4);
timer = 0;
} sendDatagram(ST_NMEA_BridgeID); // Send NMEA Seatakl BridgeID to make Seatalk to Seatalk NG converter happy
}
if (timer1 > 300000 ) {
sendDatagram(ST_BeepOff); // Additional Beep off after three seconds
timer1 = 0; void loop()
} {
int i;
char AWS[4] = "";
if (timer2 > 1000000 ) {
sendDatagram(ST_NMEA_BridgeID); // Send NMEA Seatakl BridgeID every 10 seconds to make Seatalk to Seatalk NG converter happy timer++;timer1++;timer2++;
timer2 = 0;
} if (timer > 200000 ) {
Display("---", 7); // Show --- after about two seconds when no wind data is received
timer = 0;
if (checkWind(AWS) > -1) Display(AWS, 7); }
if (mySwitch.available()) { if (timer1 > 300000 ) {
long unsigned int value = mySwitch.getReceivedValue(); sendDatagram(ST_BeepOff); // Additional Beep off after three seconds
timer1 = 0;
digitalWrite(9, blink); // LED on/off }
blink = !blink; // Toggle LED to show received key
mySwitch.resetAvailable(); if (timer2 > 1000000 ) {
sendDatagram(ST_NMEA_BridgeID); // Send NMEA Seatakl BridgeID every 10 seconds to make Seatalk to Seatalk NG converter happy
if (value == Key1) { timer2 = 0;
Display("-1", 7); }
sendDatagram(ST_Minus_1);
sendDatagram(ST_BeepOn);
delay(150); if (checkWind(AWS) > -1) Display(AWS, 7);
sendDatagram(ST_BeepOff);
} if (mySwitch.available()) {
long unsigned int value = mySwitch.getReceivedValue();
if (value == Key2) {
Display("+1", 7); digitalWrite(9, blink); // LED on/off
sendDatagram(ST_Plus_1); blink = !blink; // Toggle LED to show received key
sendDatagram(ST_BeepOn);
delay(150); mySwitch.resetAvailable();
sendDatagram(ST_BeepOff);
} if (value == Key1) {
Display("-1", 7);
if (value == Key3) { sendDatagram(ST_Minus_1);
Display("-10", 7); sendDatagram(ST_BeepOn);
sendDatagram(ST_Minus_10); delay(150);
sendDatagram(ST_BeepOn); sendDatagram(ST_BeepOff);
delay(150); }
sendDatagram(ST_BeepOff);
} if (value == Key2) {
Display("+1", 7);
if (value == Key4) { sendDatagram(ST_Plus_1);
Display("+10", 7); sendDatagram(ST_BeepOn);
sendDatagram(ST_Plus_10); delay(150);
sendDatagram(ST_BeepOn); sendDatagram(ST_BeepOff);
delay(150); }
sendDatagram(ST_BeepOff);
} if (value == Key3) {
Display("-10", 7);
i = 0; sendDatagram(ST_Minus_10);
while (mySwitch.available() && i < 2) { sendDatagram(ST_BeepOn);
mySwitch.resetAvailable(); delay(150);
delay (150); sendDatagram(ST_BeepOff);
i++; }
}
} if (value == Key4) {
} Display("+10", 7);
sendDatagram(ST_Plus_10);
sendDatagram(ST_BeepOn);
delay(150);
sendDatagram(ST_BeepOff);
}
i = 0;
while (mySwitch.available() && i < 2) {
mySwitch.resetAvailable();
delay (150);
i++;
}
}
}