Skip to content

Instantly share code, notes, and snippets.

@erikbuild
Created January 7, 2020 20:15
Show Gist options
  • Save erikbuild/3f1db5fc5794e68c138d141dd7510fd0 to your computer and use it in GitHub Desktop.
Save erikbuild/3f1db5fc5794e68c138d141dd7510fd0 to your computer and use it in GitHub Desktop.
/*
* HART Industrial Unions, LLC
* Erik J. Reynolds
* 2019-11-13
*
* Position Sensor Switch Status Receiver
*/
// Includes
#include <RGBmatrixPanel.h>
#include <SPI.h>
#include <RH_RF69.h>
#include <RHReliableDatagram.h>
// Radio Setup
#define RF69_FREQ 915.0
// who am i? (server address)
#define MY_ADDRESS 1
// Metro M4 Airlift
#define RFM69_CS 11
#define RFM69_INT 0
#define RFM69_RST 1
#define LED 13
// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram rf69_manager(rf69, MY_ADDRESS);
// LED Grid Setup
#define CLK A4 // USE THIS ON METRO M4 (not M0)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
// Define matrix width and height.
#define mw 32
#define mh 16
// Panel Matrix doesn't fully work like Neomatrix (which I originally
// wrote this demo for), so map a few calls to be compatible. The rest
// comes from Adafruit_GFX and works the same on both backends.
#define setBrightness(x) fillScreen(0) // no-op, no brightness on this board
#define clear() fillScreen(0)
#define show() swapBuffers(true)
#define Color(x,y,z) Color444(x/16,y/16,z/16)
// This could also be defined as matrix->color(255,0,0) but those defines
// are meant to work for Adafruit::GFX backends that are lacking color()
#define LED_BLACK 0
#define LED_RED_VERYLOW (3 << 11)
#define LED_RED_LOW (7 << 11)
#define LED_RED_MEDIUM (15 << 11)
#define LED_RED_HIGH (31 << 11)
#define LED_GREEN_VERYLOW (1 << 5)
#define LED_GREEN_LOW (15 << 5)
#define LED_GREEN_MEDIUM (31 << 5)
#define LED_GREEN_HIGH (63 << 5)
#define LED_BLUE_VERYLOW 3
#define LED_BLUE_LOW 7
#define LED_BLUE_MEDIUM 15
#define LED_BLUE_HIGH 31
#define LED_ORANGE_VERYLOW (LED_RED_VERYLOW + LED_GREEN_VERYLOW)
#define LED_ORANGE_LOW (LED_RED_LOW + LED_GREEN_LOW)
#define LED_ORANGE_MEDIUM (LED_RED_MEDIUM + LED_GREEN_MEDIUM)
#define LED_ORANGE_HIGH (LED_RED_HIGH + LED_GREEN_HIGH)
#define LED_PURPLE_VERYLOW (LED_RED_VERYLOW + LED_BLUE_VERYLOW)
#define LED_PURPLE_LOW (LED_RED_LOW + LED_BLUE_LOW)
#define LED_PURPLE_MEDIUM (LED_RED_MEDIUM + LED_BLUE_MEDIUM)
#define LED_PURPLE_HIGH (LED_RED_HIGH + LED_BLUE_HIGH)
#define LED_CYAN_VERYLOW (LED_GREEN_VERYLOW + LED_BLUE_VERYLOW)
#define LED_CYAN_LOW (LED_GREEN_LOW + LED_BLUE_LOW)
#define LED_CYAN_MEDIUM (LED_GREEN_MEDIUM + LED_BLUE_MEDIUM)
#define LED_CYAN_HIGH (LED_GREEN_HIGH + LED_BLUE_HIGH)
#define LED_WHITE_VERYLOW (LED_RED_VERYLOW + LED_GREEN_VERYLOW + LED_BLUE_VERYLOW)
#define LED_WHITE_LOW (LED_RED_LOW + LED_GREEN_LOW + LED_BLUE_LOW)
#define LED_WHITE_MEDIUM (LED_RED_MEDIUM + LED_GREEN_MEDIUM + LED_BLUE_MEDIUM)
#define LED_WHITE_HIGH (LED_RED_HIGH + LED_GREEN_HIGH + LED_BLUE_HIGH)
// Last parameter = 'true' enables double-buffering, for flicker-free,
// buttery smooth animation. Note that NOTHING WILL SHOW ON THE DISPLAY
// until the first call to swapBuffers(). This is normal.
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, true);
// AirCompressorState: 0/LOW = off, 1/HIGH = on
int airCompressorState;
void setup() {
// initialize states to off
airCompressorState = LOW;
// Setup the LED matrix
matrix.begin();
matrix.setTextWrap(true);
matrix.setBrightness(BRIGHTNESS);
// Test full bright of all LEDs. If brightness is too high
// for your current limit (i.e. USB), decrease it.
matrix.fillScreen(LED_WHITE_HIGH);
matrix.setRotation(3); // rotate 90 deg
matrix.show();
delay(1000);
//matrix->clear();
// Setup the Radio Stuff
//Serial.begin(115200); // serial output if connected to a computer
pinMode(LED, OUTPUT);
pinMode(RFM69_RST, OUTPUT);
digitalWrite(RFM69_RST, LOW);
// manual reset
digitalWrite(RFM69_RST, HIGH);
delay(10);
digitalWrite(RFM69_RST, LOW);
delay(10);
if (!rf69_manager.init()) {
//Serial.println("RFM69 radio init failed");
while (1);
}
//Serial.println("RFM69 radio init OK!");
// No encryption
if (!rf69.setFrequency(RF69_FREQ)) {
//Serial.println("setFrequency failed");
}
// If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
// ishighpowermodule flag set like this:
rf69.setTxPower(20, true); // range from 14-20 for power, 2nd arg must be true for 69HCW
// The encryption key has to be the same as the one in the server
// this is the default key from the example code but sure why not
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
rf69.setEncryptionKey(key);
pinMode(LED, OUTPUT);
//Serial.print("RFM69 radio @"); Serial.print((int)RF69_FREQ); Serial.println(" MHz");
}
uint8_t buf[RH_RF69_MAX_MESSAGE_LEN]; // receive buffer
uint8_t data[] = " OK"; // I don't know what this is used for.
void loop() {
if (rf69_manager.available()) {
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
if (rf69_manager.recvfromAck(buf, &len, &from)) {
buf[len] = 0; // zero out remaining string
//Serial.print("Got packet from #"); Serial.print(from);
//Serial.print(" [RSSI :");
//Serial.print(rf69.lastRssi());
//Serial.print("] : ");
//Serial.println((char*)buf);
Blink(LED, 40, 3); //blink LED 3 times, 40ms between blinks
// Depends on who the message is coming from... (mostly for future usage)
switch (from) {
case 2: // Air Compressor Switch Status
if (strstr((char *) buf, "AIRCOMPRESSOR: ON")) {
airCompressorState = HIGH;
} else if (strstr((char *) buf, "AIRCOMPRESSOR: OFF")) {
airCompressorState = LOW;
}
break;
}
// Send a reply back to the originator client
if (!rf69_manager.sendtoWait(data, sizeof(data), from)) {
//Serial.println("Sending failed (no ack)");
}
}
}
// blank it out
matrix.fillScreen(0);
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextColor(LED_BLUE_HIGH);
// Print STATUS
if (airCompressorState == HIGH) {
// Air
matrix.setCursor(0, 0);
matrix.print("A");
matrix.setCursor(5, 0);
matrix.print("i");
matrix.setCursor(10, 0);
matrix.print("r");
// is
matrix.setCursor(0, 8);
matrix.print("is");
// ON
matrix.setTextColor(LED_RED_HIGH);
matrix.setCursor(2, 18);
matrix.print("O");
matrix.setCursor(9, 18);
matrix.print("N");
} else {
// OFF
matrix.drawLine(0, 29, 2, 31, LED_GREEN_LOW);
matrix.drawLine(3, 30, 8, 25, LED_GREEN_LOW);
}
// Display the text
matrix.show();
// Wait a second...
delay(1000);
}
void Blink(byte PIN, byte DELAY_MS, byte loops) {
for (byte i=0; i<loops; i++) {
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
delay(DELAY_MS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment