void
SecurityLampApplication::
boot() {

    os().debug("ON");

    //add receiver
    os().dispatcher().add_receiver(this);
    //change channel
    os().radio().hardware_radio().set_channel(17);

    // create LisAccelerometer instance
    accelerometer_ = new LisAccelerometer(os());


    if ((accelerometer_ != NULL)) {
        // ----- configure accelerometer ------
        // set threshold mode, i.e. the sensor will start to
        // deliver data after the set threshold was exceeded
        accelerometer_->set_mode(MODE_THRESHOLD);

        // set the threshold to 5 mg
        accelerometer_->set_threshold(180);

        // set this application as the sensor event handler
        // --> handle_buffer_data will be called if
        // sensor data is available
        accelerometer_->set_handler(this);

        // switch accelerometer
        accelerometer_->enable();
    } else
        os().fatal("Could not allocate accelerometer");



    //Create the beacon payload
    echomsg[0] = 0x7f; //mksense
    echomsg[1] = 0x69; //mksense
    echomsg[2] = 100; //mksense
    echomsg[3] = 42; //echo beacon


    //Create the command payload
    mess[0] = 0x7f; //mksense
    mess[1] = 0x69; //mksense
    mess[2] = 105; //mksense
    for (int i = 3; i < 12; i++) {
        mess[i] = 99; //movement payload
    }

    //start periodic tasks
    os().add_task_in(Time(1, 0), this, NULL);
}
void WISEBEDApplication::init_sensors()
{
	#if defined (ISENSE_PACEMATE)

	#endif
	#if defined (ISENSE_MSP430)
		telos_->init();
	#endif
	#if defined (ISENSE_JENNIC_JN513xR1)

			if (em_->temp_sensor() != NULL)
			{
				em_->temp_sensor()->enable();
			} else
				os().fatal("Could not allocate temp sensor");

			em_->enable(true);

			temp_ = em_->temp_sensor()->temperature();

			if (temp_ == -127 )	// node with PIR and ACC
			{
				em_active_ = false;
			}

			if(em_active_ == true)
			{
				if (em_->light_sensor() != NULL)
				{
					em_->light_sensor()->enable();
				} else
					os().fatal("Could not allocate light sensor");
			}
			else
			{
				acc_ = new LisAccelerometer(os());

				if ((acc_ != NULL) && (pir_!= NULL))
				{
					acc_->set_mode(MODE_THRESHOLD);
					acc_->set_threshold(100);
					acc_->set_handler(this);
					acc_->enable();

					pir_->set_sensor_handler(this);
					pir_->set_pir_sensor_int_interval( 2000 );
					pir_->enable();
				}
			}
	#endif
}
void
SecurityLampApplication::
handle_buffer_data(BufferData* buf_data) {
    //note the movement
    moved = true;

    //return from continuous mode to threshold mode
    accelerometer_->set_mode(MODE_THRESHOLD);
}
void WISEBEDApplication::handle_buffer_data( BufferData* buf_data )
{
	if(app_running_ == true)
	{
		int16 x = 0;
		int16 y = 0;
		int16 z = 0;
		int16 mean[3] = {0,0,0};
		for (uint8 i=0; i<buf_data->count; i++)
		{
			mean[0] += buf_data->buf[i*(buf_data->dim)+0];
			mean[1] += buf_data->buf[i*(buf_data->dim)+1];
			mean[2] += buf_data->buf[i*(buf_data->dim)+2];
		}
		x = (x + (mean[0] / buf_data->count)) / 2;
		y = (y + (mean[1] / buf_data->count)) / 2;
		z = (z + (mean[2] / buf_data->count)) / 2;

		if (x <= 0)
			x = x * (-1);
		if (y <= 0)
			y = y * (-1);
		if (z <= 0)
			z = z * (-1);

		os().debug("wiseml;5;%x;%i;%i;%i",os().id(), x, y, z);
		spyglass_->paintNodeWithACC(os().id(), x, y, z, x+y+z);

		if (acc_active_ == true)
				os().remove_timeout(acc_timeout_, this);
		acc_timeout_ = os().add_timeout_in(5000, this, (void*)ACC_TIMEOUT);
		acc_active_ = true;
	}
	//return from continuous mode to threshold mode
	acc_->set_mode(MODE_THRESHOLD);

}