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
|
||||
* 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"
|
||||
|
@ -44,9 +44,6 @@ const struct simInterface rs232_interface;
|
|||
char simSerialReceivingData[SERIAL_BUF_SIZE];
|
||||
int simSerialReceivingLength;
|
||||
char simSerialReceivingFlag;
|
||||
char simSerialSendingData[SERIAL_BUF_SIZE];
|
||||
int simSerialSendingLength;
|
||||
char simSerialSendingFlag;
|
||||
|
||||
static int (* input_handler)(unsigned char) = NULL;
|
||||
|
||||
|
@ -62,40 +59,45 @@ rs232_set_input(int (*f)(unsigned char))
|
|||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void rs232_send(char c) {
|
||||
simSerialSendingData[simSerialSendingLength] = c;
|
||||
simSerialSendingLength += 1;
|
||||
simSerialSendingFlag = 1;
|
||||
printf("%c", c);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
void
|
||||
rs232_print(char *message)
|
||||
{
|
||||
memcpy(&simSerialSendingData[0] + simSerialSendingLength, &message[0], strlen(message));
|
||||
simSerialSendingLength += strlen(message);
|
||||
simSerialSendingFlag = 1;
|
||||
printf("%s", message);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
static void
|
||||
doInterfaceActionsBeforeTick(void)
|
||||
{
|
||||
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
|
||||
for (i=0; i < simSerialReceivingLength; i++)
|
||||
serial_line_input_byte(simSerialReceivingData[i]);
|
||||
|
||||
serial_line_input_byte(0x0a);
|
||||
|
||||
simSerialReceivingLength = 0;
|
||||
simSerialReceivingFlag = 0;
|
||||
if (!simSerialReceivingFlag) {
|
||||
return;
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* 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;
|
||||
|
@ -43,12 +43,9 @@ import se.sics.cooja.interfaces.PolledAfterActiveTicks;
|
|||
*
|
||||
* Contiki variables:
|
||||
* <ul>
|
||||
* <li>char simSerialSendingFlag (1=mote is sending data)
|
||||
* <li>int simSerialSendingLength (length of data being sent from mote)
|
||||
* <li>byte[] simSerialSendingData (data being sent from mote)
|
||||
* <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)
|
||||
* <li>char simSerialReceivingFlag (1=mote has incoming serial data)
|
||||
* <li>int simSerialReceivingLength
|
||||
* <li>byte[] simSerialReceivingData
|
||||
* </ul>
|
||||
* <p>
|
||||
*
|
||||
|
@ -117,24 +114,31 @@ public class ContikiRS232 extends SerialUI implements ContikiMoteInterface, Poll
|
|||
}
|
||||
|
||||
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 */
|
||||
int oldSize = moteMem.getIntValueOf("simSerialReceivingLength");
|
||||
int newSize = oldSize + dataToAppend.length;
|
||||
moteMem.setIntValueOf("simSerialReceivingLength", newSize);
|
||||
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
|
||||
byte[] newData = new byte[newSize];
|
||||
|
||||
byte[] oldData = moteMem.getByteArray("simSerialReceivingData", oldSize);
|
||||
byte[] newData = new byte[newSize];
|
||||
System.arraycopy(oldData, 0, newData, 0, oldData.length);
|
||||
System.arraycopy(dataToAppend, 0, newData, oldSize, dataToAppend.length);
|
||||
|
||||
System.arraycopy(oldData, 0, newData, 0, oldData.length);
|
||||
System.arraycopy(dataToAppend, 0, newData, oldSize, dataToAppend.length);
|
||||
moteMem.setByteArray("simSerialReceivingData", newData);
|
||||
|
||||
moteMem.setByteArray("simSerialReceivingData", newData);
|
||||
|
||||
mote.setState(Mote.State.ACTIVE);
|
||||
moteMem.setByteValueOf("simSerialReceivingFlag", (byte) 1);
|
||||
mote.setState(Mote.State.ACTIVE);
|
||||
}
|
||||
};
|
||||
mote.getSimulation().scheduleEvent(
|
||||
writeStringEvent,
|
||||
mote.getSimulation().getSimulationTime()
|
||||
);
|
||||
}
|
||||
|
||||
public double energyConsumption() {
|
||||
|
|
Loading…
Reference in a new issue