Last active
September 6, 2018 20:25
-
-
Save ircphp/b25f979855252b0e630c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <SD.h> | |
#define SER_SP 9600 // Скорость порта, такая же, как на GPS-модуле! | |
File flDataFile; // Файл для записи GPS-строк. | |
word uFileNo = 0; // Номер файла. | |
byte btBlinkCount = 0; // Количество пыхов светодиода. | |
char chGpsLint[128]; // Переменная для хранения GPS-строки, | |
byte btReaded; // её длина. | |
bool bWaitingGps = true; // Флаг ожидания актуальных данных GPS. | |
//volatile byte btCadCurr; // Текущий каденс. | |
//.byte btCadPrev; // Предыдущий каденс, для усреднения. | |
void setup() { | |
delay(2000); // При включении возможен; | |
// дребезг контактов питания. | |
pinMode(6, OUTPUT); // Синий светодиод (PD6). | |
pinMode(7, INPUT_PULLUP); // Кнопка без резисора (PD7). | |
while (!SD.begin()) { // Забыл вставить SD-карту, - | |
btBlinkCount = 2; // мигаем по 2 раза. | |
blink(true); | |
delay(800); | |
} | |
PORTD |= 0b001000000; // Усанавливаем пин PD6. | |
Serial.begin(SER_SP); | |
} // setup() // | |
void loop() { | |
if (Serial.available()) { // Считываем 128 симвоов или до конца строки. | |
btReaded = Serial.readBytesUntil('\n', chGpsLint, 128); | |
if (btReaded > 48) { // Данные актуальны, длинна строки от GPS-модуля. | |
if (bWaitingGps) { | |
bWaitingGps = false; | |
PORTD &= 0b110111111; // Сбрасываем пин PD6. | |
mkLogFile(); // Открываем лог GPS. | |
} | |
flDataFile.write(chGpsLint, btReaded); | |
} else { | |
if (!bWaitingGps) { | |
bWaitingGps = true; | |
PORTD |= 0b001000000; // Усанавливаем пин PD6. | |
flDataFile.close(); // Закрываем лог GPS. | |
} | |
} | |
} | |
if ((PIND & 0b010000000) == 0) { // Проверяем, не была ли зажата кнопка (PD7). | |
btBlinkCount = 4; // Мигаем 4 раза | |
halt(); // если завершили работу по кнопке при обычой работе. | |
} | |
} // loop() // | |
void mkLogFile() { | |
String sFileName; | |
do { | |
sFileName = String(uFileNo++)+".GPS"; | |
} while (SD.exists(sFileName)); | |
flDataFile = SD.open(sFileName, FILE_WRITE); | |
} // remkLogFile() // | |
void blink(bool slow) { | |
for (byte i = btBlinkCount; i > 0; i--) { | |
PORTD |= 0b001000000; // Усанавливаем пин PD6. | |
delay(100); | |
PORTD &= 0b110111111; // Сбрасываем пин PD6. | |
if (slow) | |
delay(150); | |
} | |
} // blink() // | |
void halt() { // "Завершение работы". | |
Serial.end(); // Отключаеся от последовательного порта. | |
if (flDataFile) // Если открывали файл, | |
flDataFile.close(); // закрываем. | |
SPI.end(); // Отключаеся от SD-карты. | |
while (true) { // Вечно мигаем, количество пыхов зарается перед вызовом функции. | |
blink(true); | |
delay(1350); // пауза между группами - полторы секунды (с учетом loDelay). | |
} | |
} // halt() // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment