removed unused variables + writing serial data in timeevent
This commit is contained in:
parent
dda6de8438
commit
5f1fda0406
2 changed files with 51 additions and 45 deletions
|
@ -26,7 +26,7 @@
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* $Id: rs232.c,v 1.2 2009/03/17 15:56:32 adamdunkels Exp $
|
* $Id: rs232.c,v 1.3 2009/03/26 16:23:48 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "lib/sensors.h"
|
#include "lib/sensors.h"
|
||||||
|
@ -44,9 +44,6 @@ const struct simInterface rs232_interface;
|
||||||
char simSerialReceivingData[SERIAL_BUF_SIZE];
|
char simSerialReceivingData[SERIAL_BUF_SIZE];
|
||||||
int simSerialReceivingLength;
|
int simSerialReceivingLength;
|
||||||
char simSerialReceivingFlag;
|
char simSerialReceivingFlag;
|
||||||
char simSerialSendingData[SERIAL_BUF_SIZE];
|
|
||||||
int simSerialSendingLength;
|
|
||||||
char simSerialSendingFlag;
|
|
||||||
|
|
||||||
static int (* input_handler)(unsigned char) = NULL;
|
static int (* input_handler)(unsigned char) = NULL;
|
||||||
|
|
||||||
|
@ -62,40 +59,45 @@ rs232_set_input(int (*f)(unsigned char))
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
void rs232_send(char c) {
|
void rs232_send(char c) {
|
||||||
simSerialSendingData[simSerialSendingLength] = c;
|
printf("%c", c);
|
||||||
simSerialSendingLength += 1;
|
|
||||||
simSerialSendingFlag = 1;
|
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
rs232_print(char *message)
|
rs232_print(char *message)
|
||||||
{
|
{
|
||||||
memcpy(&simSerialSendingData[0] + simSerialSendingLength, &message[0], strlen(message));
|
printf("%s", message);
|
||||||
simSerialSendingLength += strlen(message);
|
|
||||||
simSerialSendingFlag = 1;
|
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
doInterfaceActionsBeforeTick(void)
|
doInterfaceActionsBeforeTick(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
// Check if this mote has received data on RS232
|
|
||||||
if (simSerialReceivingFlag && simSerialReceivingLength > 0) {
|
|
||||||
// Tell user specified poll function
|
|
||||||
if(input_handler != NULL)
|
|
||||||
for (i=0; i < simSerialReceivingLength; i++)
|
|
||||||
input_handler(simSerialReceivingData[i]);
|
|
||||||
|
|
||||||
// Tell serial process
|
if (!simSerialReceivingFlag) {
|
||||||
for (i=0; i < simSerialReceivingLength; i++)
|
return;
|
||||||
serial_line_input_byte(simSerialReceivingData[i]);
|
|
||||||
|
|
||||||
serial_line_input_byte(0x0a);
|
|
||||||
|
|
||||||
simSerialReceivingLength = 0;
|
|
||||||
simSerialReceivingFlag = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (simSerialReceivingLength == 0) {
|
||||||
|
/* Error, should not be zero */
|
||||||
|
simSerialReceivingFlag = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Notify rs232 handler */
|
||||||
|
if(input_handler != NULL) {
|
||||||
|
for (i=0; i < simSerialReceivingLength; i++) {
|
||||||
|
input_handler(simSerialReceivingData[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Notify serial process */
|
||||||
|
for (i=0; i < simSerialReceivingLength; i++) {
|
||||||
|
serial_line_input_byte(simSerialReceivingData[i]);
|
||||||
|
}
|
||||||
|
serial_line_input_byte(0x0a);
|
||||||
|
|
||||||
|
simSerialReceivingLength = 0;
|
||||||
|
simSerialReceivingFlag = 0;
|
||||||
}
|
}
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* $Id: ContikiRS232.java,v 1.6 2009/03/21 15:41:42 fros4943 Exp $
|
* $Id: ContikiRS232.java,v 1.7 2009/03/26 16:23:47 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package se.sics.cooja.contikimote.interfaces;
|
package se.sics.cooja.contikimote.interfaces;
|
||||||
|
@ -43,12 +43,9 @@ import se.sics.cooja.interfaces.PolledAfterActiveTicks;
|
||||||
*
|
*
|
||||||
* Contiki variables:
|
* Contiki variables:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>char simSerialSendingFlag (1=mote is sending data)
|
* <li>char simSerialReceivingFlag (1=mote has incoming serial data)
|
||||||
* <li>int simSerialSendingLength (length of data being sent from mote)
|
* <li>int simSerialReceivingLength
|
||||||
* <li>byte[] simSerialSendingData (data being sent from mote)
|
* <li>byte[] simSerialReceivingData
|
||||||
* <li>char simSerialRecevingFlag (1=mote is receving data)
|
|
||||||
* <li>int simSerialReceivingLength (length of data being received at mote)
|
|
||||||
* <li>byte[] simSerialReceivingData (data being received at mote)
|
|
||||||
* </ul>
|
* </ul>
|
||||||
* <p>
|
* <p>
|
||||||
*
|
*
|
||||||
|
@ -117,24 +114,31 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeString(String message) {
|
public void writeString(String message) {
|
||||||
moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1);
|
final byte[] dataToAppend = message.getBytes();
|
||||||
|
|
||||||
byte[] dataToAppend = message.getBytes();
|
TimeEvent writeStringEvent = new TimeEvent(0) {
|
||||||
|
public void execute(long t) {
|
||||||
|
/* Append to existing buffer */
|
||||||
|
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
|
||||||
|
int newSize = oldSize + dataToAppend.length;
|
||||||
|
moteMem.setIntValueOf("simSerialReceivingLength", newSize);
|
||||||
|
|
||||||
/* Append to existing buffer */
|
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
|
||||||
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
|
byte[] newData = new byte[newSize];
|
||||||
int newSize = oldSize + dataToAppend.length;
|
|
||||||
moteMem.setIntValueOf("simSerialReceivingLength", newSize);
|
|
||||||
|
|
||||||
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
|
System.arraycopy(oldData, 0, newData, 0, oldData.length);
|
||||||
byte[] newData = new byte[newSize];
|
System.arraycopy(dataToAppend, 0, newData, oldSize, dataToAppend.length);
|
||||||
|
|
||||||
System.arraycopy(oldData, 0, newData, 0, oldData.length);
|
moteMem.setByteArray("simSerialReceivingData", newData);
|
||||||
System.arraycopy(dataToAppend, 0, newData, oldSize, dataToAppend.length);
|
|
||||||
|
|
||||||
moteMem.setByteArray("simSerialReceivingData", newData);
|
moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1);
|
||||||
|
mote.setState(Mote.State.ACTIVE);
|
||||||
mote.setState(Mote.State.ACTIVE);
|
}
|
||||||
|
};
|
||||||
|
mote.getSimulation().scheduleEvent(
|
||||||
|
writeStringEvent,
|
||||||
|
mote.getSimulation().getSimulationTime()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double energyConsumption() {
|
public double energyConsumption() {
|
||||||
|
|
Loading…
Reference in a new issue