sakura fan / SakuraAlpha

Dependents:   SakuraAlpha_SPI SakuraAlpha_I2C

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SakuraAlpha.cpp Source File

SakuraAlpha.cpp

00001 /* SAKURA Internet IoT Alpha Communication Module Library for mbed
00002  *
00003  * Copyright (c) SAKURA Internet Inc.
00004  *   The MIT License (MIT)
00005  *   https://github.com/sakura-internet/SakuraAlphaArduino
00006  *
00007  * https://iot.sakura.ad.jp/
00008  *
00009  * Porting to mbed by sakurafan. 2016
00010  */
00011 
00012 #include "mbed.h"
00013 #include "SakuraAlpha.h"
00014 
00015 void SakuraAlpha::writeChannel(uint8_t ch, int32_t data) {
00016     uint8_t *p = (uint8_t *)&data;
00017 
00018     begin();
00019 
00020     sendByte(CMD_WRITE);
00021     sendByte(ch);
00022     sendByte(TYPE_INT32);
00023 
00024     for ( int i = 0 ; i < 4 ; i++ ) {
00025         sendByte(p[i]);
00026     }
00027 
00028     finishSending();
00029     end();
00030 }
00031 
00032 void SakuraAlpha::writeChannel(uint8_t ch, uint32_t data) {
00033     uint8_t *p = (uint8_t *)&data;
00034 
00035     begin();
00036 
00037     sendByte(CMD_WRITE);
00038     sendByte(ch);
00039     sendByte(TYPE_UINT32);
00040 
00041     for ( int i = 0 ; i < 4 ; i++ ) {
00042         sendByte(p[i]);
00043     }
00044 
00045     finishSending();
00046     end();
00047 }
00048 
00049 void SakuraAlpha::writeChannel(uint8_t ch, int64_t data) {
00050     uint8_t *p = (uint8_t *)&data;
00051 
00052     begin();
00053 
00054     sendByte(CMD_WRITE);
00055     sendByte(ch);
00056     sendByte(TYPE_INT64);
00057 
00058     for ( int i = 0 ; i < 8 ; i++ ) {
00059         sendByte(p[i]);
00060     }
00061 
00062     finishSending();
00063     end();
00064 }
00065 
00066 void SakuraAlpha::writeChannel(uint8_t ch, uint64_t data) {
00067     uint8_t *p = (uint8_t *)&data;
00068 
00069     begin();
00070 
00071     sendByte(CMD_WRITE);
00072     sendByte(ch);
00073     sendByte(TYPE_UINT64);
00074 
00075     for ( int i = 0 ; i < 8 ; i++ ) {
00076         sendByte(p[i]);
00077     }
00078 
00079     finishSending();
00080     end();
00081 }
00082 
00083 void SakuraAlpha::writeChannel(uint8_t ch, float data) {
00084     uint8_t *p = (uint8_t *)&data;
00085 
00086     begin();
00087 
00088     sendByte(CMD_WRITE);
00089     sendByte(ch);
00090     sendByte(TYPE_FLOAT);
00091 
00092     for ( int i = 0 ; i < 4 ; i++ ) {
00093         sendByte(p[i]);
00094     }
00095 
00096     finishSending();
00097     end();
00098 }
00099 
00100 void SakuraAlpha::writeChannel(uint8_t ch, double data) {
00101     uint8_t *p = (uint8_t *)&data;
00102 
00103     begin();
00104 
00105     sendByte(CMD_WRITE);
00106     sendByte(ch);
00107     sendByte(TYPE_DOUBLE);
00108 
00109     for ( int i = 0 ; i < 8 ; i++ ) {
00110         sendByte(p[i]);
00111     }
00112 
00113     finishSending();
00114     end();
00115 }
00116 
00117 void SakuraAlpha::writeChannel(uint8_t ch, uint8_t *data) {
00118     uint8_t *p = (uint8_t *)data;
00119 
00120     begin();
00121 
00122     sendByte(CMD_WRITE);
00123     sendByte(ch);
00124     sendByte(TYPE_BYTES);
00125 
00126     for ( int i = 0 ; i < 8 ; i++ ) {
00127         sendByte(p[i]);
00128     }
00129 
00130     finishSending();
00131     end();
00132 }
00133 
00134 void SakuraAlpha::readChannel(uint8_t ch, char *type, uint8_t *data) {
00135     uint8_t *p = (uint8_t *)&data;
00136 
00137     begin();
00138 
00139     sendByte(CMD_READ);
00140     sendByte(ch);
00141     finishSending();
00142 
00143     startReceiving(9);
00144     *(uint8_t *)type = receiveByte();
00145 
00146     for ( int i = 0 ; i < 8 ; i++ ) {
00147         data[i] = receiveByte();
00148     }
00149     finishReceiving();
00150 
00151     end();
00152 
00153 }
00154 
00155 void SakuraAlpha::transmit(uint8_t mode){
00156     begin();
00157     sendByte(CMD_TRANSMIT);
00158     sendByte(mode);
00159 
00160     finishSending();
00161     end();
00162 }
00163 
00164 uint8_t SakuraAlpha::getTxChannelStatus(uint8_t ch){
00165     uint8_t status=0xff;
00166 
00167     begin();
00168 
00169     sendByte(CMD_TXCH_STATUS);
00170     sendByte(ch);
00171     finishSending();
00172 
00173     startReceiving(1);
00174     status = receiveByte();
00175     finishReceiving();
00176 
00177     end();
00178     return status;
00179 }
00180 
00181 uint8_t SakuraAlpha::getRxChannelStatus(uint8_t ch){
00182     uint8_t status=0xff;
00183 
00184     begin();
00185 
00186     sendByte(CMD_RXCH_STATUS);
00187     sendByte(ch);
00188     finishSending();
00189 
00190     startReceiving(1);
00191     status = receiveByte();
00192     finishReceiving();
00193 
00194     end();
00195     return status;
00196 }
00197 
00198 int SakuraAlpha::getUpdatedChannels(uint8_t *buff, uint8_t len){
00199     int num = 0;
00200 
00201     begin();
00202     sendByte(CMD_UPDATED);
00203     finishSending();
00204 
00205     wait_ms(100);
00206 
00207     startReceiving(len+1);
00208     num = receiveByte();
00209 
00210     for( int i=0; i<min(num,len); i++){
00211         buff[i]=receiveByte();
00212     }
00213 
00214     finishReceiving();
00215 
00216     end();
00217     return num;
00218 }
00219 
00220 int SakuraAlpha::getUntransmittedChannels(uint8_t *buff, uint8_t len){
00221     int num = 0;
00222 
00223     begin();
00224     sendByte(CMD_UNTRANSMITTED);
00225     finishSending();
00226 
00227     wait_ms(100);
00228 
00229     startReceiving(len+1);
00230     num = receiveByte();
00231 
00232     for( int i=0; i<min(num,len); i++){
00233         buff[i]=receiveByte();
00234     }
00235 
00236     finishReceiving();
00237 
00238     end();
00239     return num;
00240 }
00241 
00242 uint8_t SakuraAlpha::getNetworkStatus(){
00243     uint8_t status=0x00;
00244     
00245     begin();
00246     
00247     sendByte(CMD_NETWORKSTATUS);
00248     finishSending();
00249     
00250     startReceiving(1);
00251     status = receiveByte();
00252     finishReceiving();
00253     
00254     end();
00255     return status;
00256 }
00257 
00258 
00259 void SakuraAlphaI2C::begin(){
00260     _i2c.start();
00261     _i2c.write(SAKURACC_SLAVE_ADDR);
00262 }
00263 void SakuraAlphaI2C::end(){
00264 
00265 }
00266 
00267 void SakuraAlphaI2C::sendByte(uint8_t data){
00268     _i2c.write(data);
00269 }
00270 void SakuraAlphaI2C::finishSending(){
00271     _i2c.stop();
00272 }
00273 
00274 void SakuraAlphaI2C::startReceiving(uint16_t num_recieve){
00275     _num_recieve = num_recieve;
00276     _i2c.start();
00277     _i2c.write(SAKURACC_SLAVE_ADDR | 1);
00278 }
00279 uint8_t SakuraAlphaI2C::receiveByte(){
00280     if (_num_recieve <= 0) return 0;
00281     _num_recieve --;
00282     return _i2c.read(_num_recieve > 0 ? 1 : 0);
00283 }
00284 void SakuraAlphaI2C::finishReceiving(){
00285     while (_num_recieve > 0) {
00286         _num_recieve --;
00287         _i2c.read(_num_recieve > 0 ? 1 : 0);
00288     }
00289     _i2c.stop();
00290 }
00291 
00292 SakuraAlphaI2C::SakuraAlphaI2C (I2C &i2c) : _i2c(i2c) {
00293 }
00294 SakuraAlphaI2C::SakuraAlphaI2C (PinName sda, PinName scl) : _i2c(sda, scl) {
00295 }
00296 
00297 
00298 void SakuraAlphaSPI::begin(){
00299     _cs = 0;
00300 }
00301 void SakuraAlphaSPI::end(){
00302     _cs = 1;
00303     wait_ms(10);
00304 }
00305 
00306 void SakuraAlphaSPI::sendByte(uint8_t data){
00307     wait_ms(10);
00308     _spi.write(data);
00309 }
00310 void SakuraAlphaSPI::finishSending(){
00311 }
00312 
00313 void SakuraAlphaSPI::startReceiving(uint16_t num_recieve){
00314 }
00315 uint8_t SakuraAlphaSPI::receiveByte(){
00316     wait_ms(10);
00317     return _spi.write(0);
00318 }
00319 void SakuraAlphaSPI::finishReceiving(){
00320 }
00321 
00322 SakuraAlphaSPI::SakuraAlphaSPI(SPI &spi, PinName cs) : _spi(spi), _cs(cs) {
00323     _cs = 1;
00324 }
00325 SakuraAlphaSPI::SakuraAlphaSPI(PinName mosi, PinName miso, PinName sck, PinName cs) : _spi(mosi, miso, sck), _cs(cs) {
00326     _cs = 1;
00327 }