393 lines
8.8 KiB
C++
393 lines
8.8 KiB
C++
/*********************************************************************
|
|
|
|
NMM-1
|
|
NMEA0183 serial monitor.
|
|
|
|
By Jony Silva
|
|
Wed 24 Jan 23:11:25 GMT 2018
|
|
*********************************************************************/
|
|
|
|
// Libraries to be included
|
|
//#include <SPI.h>
|
|
//#include <Wire.h>
|
|
#include <SoftwareSerial.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
|
|
#define OLED_RESET 8
|
|
Adafruit_SSD1306 display(OLED_RESET);
|
|
|
|
#define ON 0 // ON means pin is on GND
|
|
#define OFF 1 // OFF means pin is on VCC
|
|
|
|
// Assign switches and led's to arduino pins
|
|
#define UP 2 // Switch 1 will be function UP connected to arduino PD 2
|
|
#define OK 3 // Switch 2 will be function OK connected to arduino pin 3
|
|
#define DOWN 4 // Switch 3 will be function DOWN connected to arduino pin 4
|
|
#define CANCEL 5 // Switch 4 will be function CANCEL connected to arduino pin 5
|
|
#define led1 9 // Led 1 connected to arduino pin 9
|
|
#define led2 10 // Led 2 connected to arduino pin 10
|
|
#define batt 17 // Battery plus connected to arduino pin 17
|
|
|
|
// Macros to read the switchs
|
|
#define rUP digitalRead(UP) // Read button UP state
|
|
#define rOK digitalRead(OK) // Read button OK state
|
|
#define rDOWN digitalRead(DOWN) // Read button DOWN state
|
|
#define rCANCEL digitalRead(CANCEL) // Read button CANCEL state
|
|
|
|
|
|
// Function declarations
|
|
void zero();
|
|
void one();
|
|
void two();
|
|
void three();
|
|
void four();
|
|
void read_serial();
|
|
void write_serial();
|
|
void config();
|
|
void batt_icon();
|
|
|
|
|
|
//volatile byte state = LOW;
|
|
|
|
// Variables creation
|
|
int menuState = 1; // Integer with the number of the menu you are in
|
|
int prevMenu = 1; // Previous menu
|
|
int UP_state = OFF; // Holds state for UP, either ON or OFF
|
|
int OK_state = OFF; // Holds state for OK, either ON or OFF
|
|
int DOWN_state = OFF; // Holds state for DOWN, either ON or OFF
|
|
int CANCEL_state = OFF; // Holds state for CANCEL, either ON or OFF
|
|
int batt_val = 0; // Holds the value of battery voltage
|
|
|
|
|
|
// Create SoftwareSerial object and designate pins
|
|
SoftwareSerial mySerial(6, 7); // RX, TX
|
|
|
|
void setup() { // BEGIN SETUP ------------------------------------
|
|
|
|
// Configure buttons and led's has inputs or outputs
|
|
pinMode(led1, OUTPUT); // Led 1 is an output
|
|
pinMode(led2, OUTPUT); // Led 2 is an output
|
|
pinMode(UP, INPUT); // Switch 1 is an input
|
|
pinMode(OK, INPUT); // Switch 2 is an input
|
|
pinMode(DOWN, INPUT); // Switch 3 is an input
|
|
pinMode(CANCEL, INPUT); // Switch 4 is an input
|
|
|
|
|
|
// Define the starting state of the led's
|
|
digitalWrite(led1,HIGH); // Turn off led 1
|
|
digitalWrite(led2,HIGH); // Turn off led 2
|
|
|
|
// set the data rate for the SoftwareSerial port
|
|
mySerial.begin(9600);
|
|
|
|
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
|
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
|
|
// init done
|
|
|
|
// Show image buffer on the display hardware.
|
|
// Since the buffer is intialized with an Adafruit splashscreen
|
|
// internally, this will display the splashscreen.
|
|
//display.display();
|
|
//delay(2000); // Wait 2 seconds
|
|
|
|
|
|
// initialize timer1 ---------------------------------------
|
|
noInterrupts(); // disable all interrupts
|
|
TCCR1A = 0;
|
|
TCCR1B = 0;
|
|
TCNT1 = 0;
|
|
|
|
OCR1A = 9000; // Load value to compare
|
|
TCCR1B |= (1 << WGM12); // CTC mode
|
|
TCCR1B |= (1 << CS10); // 64 prescaler
|
|
TCCR1B |= (1 << CS11); //
|
|
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
|
|
interrupts(); // enable all interrupts
|
|
// ----------------------------------------------------------
|
|
|
|
|
|
// Clear the buffer.
|
|
display.clearDisplay();
|
|
display.display();
|
|
|
|
display.setTextSize(2);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(36,12);
|
|
display.println("NMM-1");
|
|
display.display();
|
|
delay(3000);
|
|
display.clearDisplay();
|
|
display.display();
|
|
|
|
|
|
} // END SETUP --------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Timer compare interrupt service routine --------------------------
|
|
ISR(TIMER1_COMPA_vect) // Here we chose between the blinker sequence
|
|
{ // or to show the temperature on the LEDs
|
|
/*
|
|
if (!rUP) {
|
|
if (menuState == 1) {
|
|
menuState = 1;
|
|
}
|
|
else if (menuState == 2) {
|
|
menuState = 1;
|
|
}
|
|
else if (menuState == 3) {
|
|
menuState = 4;
|
|
}
|
|
else if (menuState == 4) {
|
|
menuState = 1;
|
|
}
|
|
while (!rUP); // Check if button is still pressed do nothing
|
|
}
|
|
|
|
|
|
if (!rDOWN) {
|
|
if (menuState == 1) {
|
|
menuState = 2;
|
|
}
|
|
else if (menuState == 2) {
|
|
menuState = 3;
|
|
}
|
|
else if (menuState == 3) {
|
|
menuState = 3;
|
|
}
|
|
else if (menuState == 4) {
|
|
menuState = 3;
|
|
}
|
|
while (!rDOWN); // Check if button is still pressed do nothing
|
|
}
|
|
|
|
|
|
if (!rOK) {
|
|
if (menuState == 1) {
|
|
menuState = 10;
|
|
prevMenu = 1;
|
|
}
|
|
else if (menuState == 2) {
|
|
menuState = 11;
|
|
prevMenu = 2;
|
|
}
|
|
else if (menuState == 3) {
|
|
menuState = 12;
|
|
prevMenu = 3;
|
|
}
|
|
else if (menuState == 4) {
|
|
menuState = 11;
|
|
prevMenu = 4;
|
|
}
|
|
while (!rOK); // Check if button is still pressed do nothing
|
|
}
|
|
|
|
|
|
if (!rCANCEL) {
|
|
if (prevMenu == 1) {
|
|
menuState = 1;
|
|
}
|
|
else if (prevMenu == 2) {
|
|
menuState = 2;
|
|
}
|
|
else if (prevMenu == 3) {
|
|
menuState = 3;
|
|
}
|
|
else if (prevMenu == 4) {
|
|
menuState = 4;
|
|
}
|
|
while (!rCANCEL); // Check if button is still pressed do nothing
|
|
}
|
|
*/
|
|
}
|
|
// -------------------------------------------------------------------
|
|
|
|
|
|
|
|
void loop() {
|
|
//digitalWrite(led2, !digitalRead(led2)); // toggle state
|
|
|
|
|
|
|
|
|
|
batt_val = analogRead(batt);
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(50,18);
|
|
display.println(batt_val);
|
|
batt_icon();
|
|
display.display();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
if (menuState == 1) {
|
|
one();
|
|
}
|
|
else if (menuState == 2) {
|
|
two();
|
|
}
|
|
else if (menuState == 3) {
|
|
three();
|
|
}
|
|
else if (menuState == 4) {
|
|
four();
|
|
}
|
|
else if (menuState == 10) {
|
|
read_serial();
|
|
}
|
|
else if (menuState == 11) {
|
|
write_serial();
|
|
}
|
|
else if (menuState == 12) {
|
|
config();
|
|
}
|
|
else {
|
|
one();
|
|
}
|
|
*/
|
|
}
|
|
|
|
|
|
void batt_icon (void) {
|
|
|
|
// Draw the battery icon
|
|
display.drawRect(111, 1, 16, 6, WHITE);
|
|
display.drawPixel(110, 3, WHITE);
|
|
display.drawPixel(110, 4, WHITE);
|
|
display.drawPixel(109, 3, WHITE);
|
|
display.drawPixel(109, 4, WHITE);
|
|
display.drawPixel(108, 3, WHITE);
|
|
display.drawPixel(108, 4, WHITE);
|
|
}
|
|
|
|
void zero (void) {
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(1,12);
|
|
display.println("Read NMEA0183");
|
|
display.setCursor(1,22);
|
|
display.println("Write NMEA0183");
|
|
batt_icon();
|
|
display.display();
|
|
|
|
}
|
|
|
|
void one (void) {
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(1,12);
|
|
display.println("Read NMEA0183 <--");
|
|
display.setCursor(1,22);
|
|
display.println("Write NMEA0183");
|
|
batt_icon();
|
|
display.display();
|
|
|
|
}
|
|
|
|
void two (void) {
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(1,12);
|
|
display.println("Read NMEA0183");
|
|
display.setCursor(1,22);
|
|
display.println("Write NMEA0183 <--");
|
|
batt_icon();
|
|
display.display();
|
|
|
|
}
|
|
|
|
void three (void) {
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(1,12);
|
|
display.println("Write NMEA0183");
|
|
display.setCursor(1,22);
|
|
display.println("Config NMEA0183 <--");
|
|
batt_icon();
|
|
display.display();
|
|
|
|
}
|
|
|
|
void four (void) {
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(1,12);
|
|
display.println("Write NMEA0183 <--");
|
|
display.setCursor(1,22);
|
|
display.println("Config NMEA0183");
|
|
batt_icon();
|
|
display.display();
|
|
|
|
}
|
|
|
|
void read_serial (void) {
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(1,17);
|
|
display.println("Lets Read :)");
|
|
batt_icon();
|
|
display.display();
|
|
|
|
}
|
|
|
|
void write_serial (void) {
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(1,17);
|
|
display.println("Lets Write :)");
|
|
batt_icon();
|
|
display.display();
|
|
|
|
}
|
|
|
|
void config (void) {
|
|
|
|
display.drawRect(0, 8, 168, 24, BLACK);
|
|
display.display();
|
|
display.clearDisplay();
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
display.setCursor(46,17);
|
|
display.println("BLOCKED");
|
|
batt_icon();
|
|
display.display();
|
|
|
|
}
|