Skip to content

Commit

Permalink
fix library
Browse files Browse the repository at this point in the history
  • Loading branch information
nomad605dis committed Apr 12, 2018
1 parent 5afb444 commit 00d64bc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ GPS/GLONASS (Troyka-модуль)
«Добавить библиотеку…». В появившемся окне выберите скачаный архив с
библиотекой. Установка завершена.

Подробности и примеры работы — [в статье на Амперка / Вики](http://wiki.amperka.ru/%D0%BF%D1%80%D0%BE%D0%B4%D1%83%D0%BA%D1%82%D1%8B:troyka-gps-glonass)
Подробности и примеры работы — [в статье на Амперка / Вики](http://wiki.amperka.ru/продукты:troyka-gps-glonass)
8 changes: 5 additions & 3 deletions src/TroykaGPS.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <stdio.h>
#include "TroykaGPS.h"

using namespace GPSNAME;

enum { ZERO, ONE, TWO, THREE, FOUR, FIVE, FIND, COPY, SAVE };

GPS::GPS(Stream &serial) {
uartDeviceInit(&serial);
GPS::GPS(Stream &serial) : UARTDevice(&serial) {

}

int GPS::available() {
Expand Down Expand Up @@ -431,4 +433,4 @@ void GPS::getTime(char* time, size_t maxLength) const {

void GPS::getDate(char* date, size_t maxLength) const {
strncpy(date, _date, maxLength);
}
}
4 changes: 2 additions & 2 deletions src/TroykaGPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

#define KNOT_TO_KM 1.852

class GPS
class GPS : public GPSNAME::UARTDevice
{

public:
GPS(Stream &serial);
int available();
Expand Down Expand Up @@ -68,4 +67,5 @@ class GPS
int8_t _findGNGGA;
int8_t _findGNRMC;
};

#endif
56 changes: 30 additions & 26 deletions src/utility/uartDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#include "uartDevice.h"

Stream* serialUartDevice;
using namespace GPSNAME;

void uartDeviceInit(void* uartDevice) {
serialUartDevice = (Stream*)uartDevice;
UARTDevice::UARTDevice(Stream* uartDevice) {
_uartDevice = uartDevice;
}

int uartDeviceAvailable() {
return serialUartDevice->available();
UARTDevice::~UARTDevice() {
}

int uartDeviceAvailable (int waitTime) {
int UARTDevice::uartDeviceAvailable() {
return _uartDevice -> available();
}

int UARTDevice::uartDeviceAvailable (int waitTime) {
unsigned long timerStart;
int dataLen = 0;
timerStart = millis();
Expand All @@ -23,24 +26,24 @@ int uartDeviceAvailable (int waitTime) {
return dataLen;
}

void uartDeviceFlushSerial() {
void UARTDevice::uartDeviceFlushSerial() {
while (uartDeviceAvailable()) {
char c = serialUartDevice -> read();
char c = _uartDevice -> read();
}
}

int uartDeviceReadSerial() {
return serialUartDevice -> read();
int UARTDevice::uartDeviceReadSerial() {
return _uartDevice -> read();
}

void uartDeviceReadBuffer(char* buffer, int count, unsigned int timeout, unsigned int charTimeout) {
void UARTDevice::uartDeviceReadBuffer(char* buffer, int count, unsigned int timeout, unsigned int charTimeout) {
int i = 0;
unsigned long timerStart, prevChar;
timerStart = millis();
prevChar = 0;
while(1) {
while (uartDeviceAvailable()) {
char c = serialUartDevice -> read();
char c = _uartDevice -> read();
prevChar = millis();
buffer[i++] = c;
if(i >= count)
Expand All @@ -58,49 +61,49 @@ void uartDeviceReadBuffer(char* buffer, int count, unsigned int timeout, unsigne
}
}

void uartDeviceCleanBuffer(char* buffer, int count) {
void UARTDevice::uartDeviceCleanBuffer(char* buffer, int count) {
for(int i = 0; i < count; i++) {
buffer[i] = '\0';
}
}

void uartDeviceSendByte(uint8_t data) {
serialUartDevice->write(data);
void UARTDevice::uartDeviceSendByte(uint8_t data) {
_uartDevice->write(data);
}

void uartDeviceSendChar(const char c) {
serialUartDevice->write(c);
void UARTDevice::uartDeviceSendChar(const char c) {
_uartDevice->write(c);
}

void uartDeviceSendCMD(const char* cmd) {
void UARTDevice::uartDeviceSendCMD(const char* cmd) {
for(int i = 0; i < strlen(cmd); i++) {
uartDeviceSendByte(cmd[i]);
}
}

void uartDeviceSendCMD(const __FlashStringHelper* cmd) {
void UARTDevice::uartDeviceSendCMD(const __FlashStringHelper* cmd) {
int i = 0;
const char *ptr = (const char *) cmd;
while (pgm_read_byte(ptr + i) != 0x00) {
uartDeviceSendByte(pgm_read_byte(ptr + i++));
}
}

void uartDeviceSendCMDP(const char* cmd) {
void UARTDevice::uartDeviceSendCMDP(const char* cmd) {
// pgm_read_byte(address_short) - Read a byte from the program space with a 16-bit (near) address
while (pgm_read_byte(cmd) != 0x00)
uartDeviceSendByte(pgm_read_byte(cmd++));
}

void uartDeviceSendAT(void) {
void UARTDevice::uartDeviceSendAT() {
uartDeviceCheckWithCMD(F("AT\r\n"),"OK", CMD);
}

void uartDeviceSendEndMark(void) {
void UARTDevice::uartDeviceSendEndMark() {
uartDeviceSendByte((char)26);
}

bool uartDeviceWaitForResp(const char* resp, dataType type, unsigned int timeout, unsigned int charTimeout) {
bool UARTDevice::uartDeviceWaitForResp(const char* resp, dataType type, unsigned int timeout, unsigned int charTimeout) {
int len = strlen(resp);
int sum = 0;
// prevChar is the time when the previous Char has been read
Expand All @@ -109,7 +112,7 @@ bool uartDeviceWaitForResp(const char* resp, dataType type, unsigned int timeout
prevChar = 0;
while(1) {
if (uartDeviceAvailable()) {
char c = serialUartDevice -> read();
char c = _uartDevice -> read();
prevChar = millis();
sum = (c == resp[sum]) ? sum + 1 : 0;
if (sum == len) {
Expand All @@ -132,13 +135,14 @@ bool uartDeviceWaitForResp(const char* resp, dataType type, unsigned int timeout
}


bool uartDeviceCheckWithCMD(const char* cmd, const char *resp, dataType type, unsigned int timeout, unsigned int charTimeout) {
bool UARTDevice::uartDeviceCheckWithCMD(const char* cmd, const char *resp, dataType type, unsigned int timeout, unsigned int charTimeout) {
uartDeviceSendCMD(cmd);
return uartDeviceWaitForResp(resp, type, timeout, charTimeout);
}

// HACERR que tambien la respuesta pueda ser FLASH STRING
bool uartDeviceCheckWithCMD(const __FlashStringHelper* cmd, const char *resp, dataType type, unsigned int timeout, unsigned int charTimeout) {
bool UARTDevice::uartDeviceCheckWithCMD(const __FlashStringHelper* cmd, const char *resp, dataType type, unsigned int timeout, unsigned int charTimeout) {
uartDeviceSendCMD(cmd);
return uartDeviceWaitForResp(resp, type, timeout, charTimeout);
}

51 changes: 32 additions & 19 deletions src/utility/uartDevice.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
#ifndef __UART_DEVICE_H__
#define __UART_DEVICE_H__
#ifndef __UART_DEVICE_GPRS_H__
#define __UART_DEVICE_GPRS_H__
#include <Arduino.h>

// miliseconds
#define DEFAULT_TIMEOUT 1000
// miliseconds
#define DEFAULT_INTERCHAR_TIMEOUT 1000

// using namespace for GPS
namespace GPSNAME {

enum dataType {
CMD = 0,
DATA = 1,
};

void uartDeviceInit(void* uartDevice);
int uartDeviceAvailable();
int uartDeviceAvailable(int waitTime);
void uartDeviceFlushSerial();
int uartDeviceReadSerial();
void uartDeviceReadBuffer(char* buffer,int count, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int charTimeout = DEFAULT_INTERCHAR_TIMEOUT);
void uartDeviceCleanBuffer(char* buffer, int count);
void uartDeviceSendByte(uint8_t data);
void uartDeviceSendChar(const char c);
void uartDeviceSendCMD(const char* cmd);
void uartDeviceSendCMD(const __FlashStringHelper* cmd);
void uartDeviceSendCMDP(const char* cmd);
void uartDeviceSendAT(void);
void uartDeviceSendEndMark(void);
bool uartDeviceWaitForResp(const char* resp, dataType type, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int charTimeout = DEFAULT_INTERCHAR_TIMEOUT);
bool uartDeviceCheckWithCMD(const char* cmd, const char *resp, dataType type, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int charTimeout = DEFAULT_INTERCHAR_TIMEOUT);
bool uartDeviceCheckWithCMD(const __FlashStringHelper* cmd, const char *resp, dataType type, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int charTimeout = DEFAULT_INTERCHAR_TIMEOUT);
// abstract base class
class UARTDevice
{
public:
UARTDevice(Stream* uartDevice);
virtual ~UARTDevice() = 0;
int uartDeviceAvailable();
int uartDeviceAvailable(int waitTime);
void uartDeviceFlushSerial();
int uartDeviceReadSerial();
void uartDeviceReadBuffer(char* buffer, int count, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int charTimeout = DEFAULT_INTERCHAR_TIMEOUT);
void uartDeviceCleanBuffer(char* buffer, int count);
void uartDeviceSendByte(uint8_t data);
void uartDeviceSendChar(const char c);
void uartDeviceSendCMD(const char* cmd);
void uartDeviceSendCMD(const __FlashStringHelper* cmd);
void uartDeviceSendCMDP(const char* cmd);
void uartDeviceSendAT();
void uartDeviceSendEndMark();
bool uartDeviceWaitForResp(const char* resp, dataType type, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int charTimeout = DEFAULT_INTERCHAR_TIMEOUT);
bool uartDeviceCheckWithCMD(const char* cmd, const char* resp, dataType type, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int charTimeout = DEFAULT_INTERCHAR_TIMEOUT);
bool uartDeviceCheckWithCMD(const __FlashStringHelper* cmd, const char* resp, dataType type, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int charTimeout = DEFAULT_INTERCHAR_TIMEOUT);
private:
Stream* _uartDevice;
};

}

#endif

0 comments on commit 00d64bc

Please sign in to comment.