Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MachineCloud.cpp
00001 #include <string.h> 00002 #include "MachineCloud.h" 00003 00004 MachineCloud::MachineCloud() { 00005 init(0x1000); 00006 } 00007 00008 MachineCloud::MachineCloud(uint16_t router) { 00009 init(router); 00010 } 00011 00012 void MachineCloud::init(uint16_t router) { 00013 m_router = router; 00014 } 00015 00016 int MachineCloud::connect() { 00017 m_xbee = new XBee(p9, p10); 00018 return 0; 00019 } 00020 00021 int MachineCloud::disconnect() { 00022 m_xbee = NULL; 00023 return 0; 00024 } 00025 00026 int MachineCloud::on_receive(MachineCloudCallback *function) { 00027 m_receiverFunction = function; 00028 m_receiverObject = NULL; 00029 return 0; 00030 } 00031 00032 int MachineCloud::on_receive(MachineCloudReceiver *object) { 00033 m_receiverFunction = NULL; 00034 m_receiverObject = object; 00035 return 0; 00036 } 00037 00038 int MachineCloud::send(char *key, char *value) { 00039 m_lock.lock(); 00040 00041 int size = strlen(key) + strlen(value) + 1; 00042 char *message = (char *)malloc(size); 00043 sprintf(message, "%s=%s", key, value); 00044 00045 uint8_t payload[size]; 00046 for (int i=0; i<size; i++) { 00047 payload[i] = message[i]; 00048 } 00049 00050 free(message); 00051 00052 TxStatusResponse txStatus = TxStatusResponse(); 00053 Tx16Request tx = Tx16Request(m_router, payload, sizeof(payload)); 00054 m_xbee->send(tx); 00055 00056 m_lock.unlock(); 00057 00058 return 0; 00059 } 00060 00061 void MachineCloud::receive(char *key, char *value) { 00062 if (m_receiverFunction) (*m_receiverFunction)(key, value); 00063 if (m_receiverObject) m_receiverObject->receive(key, value); 00064 } 00065 00066 Rx16Response rx16 = Rx16Response(); 00067 00068 int MachineCloud::loop() { 00069 m_xbee->readPacket(); 00070 00071 if (m_xbee->getResponse().isAvailable()) { 00072 switch (m_xbee->getResponse().getApiId()) { 00073 case RX_16_RESPONSE: 00074 Rx16Response rx16 = Rx16Response(); 00075 m_xbee->getResponse().getRx16Response(rx16); 00076 char *data = (char *)malloc(rx16.getDataLength() + 1); 00077 for (int i=0; i<rx16.getDataLength(); i++) { 00078 data[i] = rx16.getData(i); 00079 } 00080 data[rx16.getDataLength()] = 0; 00081 char *key = strtok(data, "="); 00082 char *val = strtok(NULL, "="); 00083 receive(key, val); 00084 free(data); 00085 } 00086 } 00087 00088 00089 return 0; 00090 }
Generated on Thu Jul 14 2022 16:30:17 by
1.7.2