Status MrimConnection::setStatus(const Status &status) { bool isConnected = state() == MrimConnection::ConnectedToIMServer; bool isUnconnected = state() == MrimConnection::Unconnected; bool isConnecting = !isConnected && !isUnconnected; if (status.type() == Status::Offline) { p->status = status; // if (isConnecting) close(); return p->status; } else { p->status = status; if (isConnecting) { return Status::createConnecting(status, "mrim"); } else if (isUnconnected) { start(); return Status::createConnecting(status, "mrim"); } else { sendStatusPacket(); } } return p->status; }
int main() { sei(); timerInit(); ledInit(); ledOn(); delay(100); ledOff(); packet.s.nodeId = eeprom_read_byte(I2C_ADDRESS_EEPROM_LOCATION); SPI.begin(); laserInit(); adcInit(); detectorInit(); detectorCalibrate(); uint8_t radioInitialised = radio.initialize(RF69_868MHZ, packet.s.nodeId, 100); radio.encrypt(0); if(radioInitialised) { ledOn(); delay(1500); sendStatusPacket(); ledOff(); } else { ledRapidBlink(2); } laserOn(); while(1) { if(laserTripped) { ledOn(); sendStatusPacket(); delay(10); while((0 == (PINB & _BV(PB1)))){ //WAIT } sendLaserVisiblePacket(); detectorArm(); while(0 == (PINA & _BV(PA3))) { //WAIT } } else { ledOff(); } if(laserTripped || 0 == (PINA & _BV(PA3))) { sendStatusPacket(); delay(300); while(0 == (PINA & _BV(PA3))) { //WAIT } delay(300); } if(radio.receiveDone()) { switch((char)radio.DATA[0]) { case CMD_LASER_ON: laserOn(); break; case CMD_LASER_OFF: laserOff(); break; case CMD_CALIBRATE: detectorCalibrate(); break; case CMD_GET_STATUS: waitAndSendStatus(); break; } } if(isBatteryLow()) { laserOff(); radio.sleep(); while(1) { ledRapidBlink(10); delay(2000); } } } }
inline void waitAndSendStatus() { for(uint8_t i = 0; i < packet.s.nodeId; i++) { delay(10); } sendStatusPacket(); }
int main(int argc, char **argv) { int fd; int err; int sleepy = FALSE; telospacket *packet; init(argc,argv); printf("Starting\n"); sleep(1); #ifndef LOCALDBG system("modprobe usbserial"); system("modprobe ftdi_sio"); sleep(1); firststartup("localhost",9000); sleep(1); fd = open_telos_source("localhost", 9000); if (fd <= 0) { printf("o_t_s failed! (fd=%i)\n",fd); return 1; } sendStatusPacket(fd, 1, 0); sleep(1); //usleep(100000); //sendStatusPacket(fd, 1, 0); //usleep(100000); close(fd); usleep(500000); //sleep(1); printf("...going to sleep.\n"); err = stopasf(); if (err) { printf("stopasf failed (err=%i)\n",err); return err; } usleep(10000); system("modprobe -r ftdi_sio"); system("modprobe -r usbserial"); system("/sbin/sys_suspend"); #endif while(TRUE) { //wait for device to wake #ifndef LOCALDBG //sleep(2); system("modprobe usbserial"); system("modprobe ftdi_sio"); #endif sleep(1); err = startup("localhost",9000); if (err) { printf("Startup failed...\n"); return 1; } fd = open_telos_source("localhost", 9000); if (fd <= 0) { printf("o_t_s failed! (fd=%i)\n",fd); return 1; } printf("startup succeeded...\n"); //wait for sleep msg and send status while (!sleepy) { packet = read_telos_packet(fd); if (packet && packet->type == AM_SLEEP_MSG) { sleepy = TRUE; printf("Sleep Msg!"); sleep(1); } else { printf("."); } free_telos_packet(&packet); packet = NULL; } sleepy = FALSE; sleep(1); //send I'm sleeping packet sendStatusPacket(fd, 1, 0); usleep(10000); close(fd); err = closedown(); if (err) { printf("closedown failed...\n"); } else { printf("Going to sleep\n"); #ifdef LOCALDBG sleep(5); #else system("modprobe -r ftdi_sio"); system("modprobe -r usbserial"); system("/sbin/sys_suspend"); #endif } sleep(1); } return 0; }