Sigfox Communication library, allowing you to use any kind of UART able Sigfox Transmitter

Dependents:   TestVirtualisation

sigfox.cpp

Committer:
RB62680
Date:
2016-03-12
Revision:
2:975b82a3cde0
Parent:
1:39a0558a3a3a
Child:
3:3ef537a95c15

File content as of revision 2:975b82a3cde0:

#include "mbed.h"
#include "millis.h"
#include "sigfox.h"

Sigfox_ Sigfox;

Sigfox_::Sigfox_()  {
    Serial device(PIN_RX, PIN_TX);
    _lastSend=-1;
}

uint8_t Sigfox_::_nextReturn() {
    Serial device(PIN_RX, PIN_TX);     
    char fstChar = device.getc();
    while(device.getc() != ';');
    return fstChar;
}

void Sigfox_::begin () {
    Serial device(PIN_RX, PIN_TX); 
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)';');
    
    //while(device.available() < 3);

    device.getc(); //'K'
    device.getc(); //'O'
    device.getc(); //';'
}

bool Sigfox_::isReady() {
    Serial device(PIN_RX, PIN_TX);

    unsigned long currentTime = millis();
    if(currentTime >= _lastSend && (currentTime - _lastSend) <= 600000) {
        return false;
    }
    // Time is ok, ask the modem's status
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc('\r');
    //device.putc((uint8_t)';');

    //return _nextReturn() == OK;
    return true;
}
 
bool Sigfox_::send(const void* data, uint8_t len) {
    uint8_t* bytes = (uint8_t*)data;
    _lastSend = millis();
    
    Serial device(PIN_RX, PIN_TX);
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc((uint8_t)'$');
    device.putc((uint8_t)'S');
    device.putc((uint8_t)'F');
    device.putc((uint8_t)'=');
    
    for(uint8_t i = 0; i < len; ++i) {
        device.putc(bytes[i]);
    }
    device.putc('\r');

    uint8_t ok = _nextReturn();
    if(ok == OK) {
        _nextReturn(); //SENT
        return true;
    }
    return false;
}
 
unsigned long Sigfox_::getID() {
    unsigned long id = 0;
    Serial device(PIN_RX, PIN_TX);
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc((uint8_t)'$');
    device.putc((uint8_t)'I');
    device.putc((uint8_t)'=');
    device.putc((uint8_t)'1');
    device.putc((uint8_t)'0');
    device.putc('\r');
    
    uint8_t response[8] = {0};
    uint8_t i = 0;
    for (i = 0; i<7;++i) {
        response[i] = device.getc();
        ++i;
    }
    /*uint8_t response[8] = {0};
    while(device.getc() != '\0') {
        response[i] = device.getc();
        while(!device.readable());
        ++i;
    }*/
    device.getc(); //';'
    for(uint8_t j = 0; j < i; ++j) {
        id += response[j] << ((i-3-j) * 8);
    }
    return id;
}

unsigned long Sigfox_::getPAC() {
    unsigned long id = 0;
    Serial device(PIN_RX, PIN_TX);
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc((uint8_t)'$');
    device.putc((uint8_t)'I');
    device.putc((uint8_t)'=');
    device.putc((uint8_t)'1');
    device.putc((uint8_t)'1');
    device.putc('\r');
    
    uint8_t response[8] = {0};
    uint8_t i = 0;
    for (i = 0; i<7;++i) {
        response[i] = device.getc();
        ++i;
    }
    
    for(uint8_t j = 0; j < i; ++j) {
        id += response[j] << ((i-3-j) * 16);
    }
    return true;
}

bool Sigfox_::setPowerMode(uint8_t power) {
    Serial device(PIN_RX, PIN_TX);
    device.putc((uint8_t)'\0');
    device.putc((uint8_t)'A');
    device.putc((uint8_t)'T');
    device.putc((uint8_t)'$');
    device.putc((uint8_t)'I');
    device.putc((uint8_t)'=');
    device.putc(power);
    device.putc('\r');

    //return _nextReturn() == OK;
    return 1;
}

//------------------------------------------------------------------------------------------------//
//Destructors

/*sigfox::~sf() {
}*/