Example #1
0
void shackbus_main(void)
{
	if ( fifo_get_count(&can_outfifo) > 0  )
	{
		uint8_t cur_nr = fifo_read (&can_outfifo);
		if ( can_send_message(&framestorage_data[cur_nr]) )
		{
			fifo_get (&can_outfifo);
			framestorage_get(cur_nr);
		}
	}

	// Check if a new messag was received
	if (can_check_message())
	{
		can_t msg;

		// Try to read the message
		if (can_get_message(&msg))
		{
			shackbus_id_t shackbus_id;
			shackbus_id2sb(&shackbus_id,&msg);

			//prot=10 = Basis IO data[0]=10 = Ping
			if (shackbus_id.prot == 10 && shackbus_id.dst == 8 && msg.data[0]==10)
			{
				shackbus_id.prio = 3;
				shackbus_id.vlan = 4;
				shackbus_id.dst  = shackbus_id.src;
				shackbus_id.src  = 8;
				shackbus_id.prot = 10;

				msg.id = shackbus_sb2id(&shackbus_id);
				msg.length = 2;
				msg.data[0] = 11;
				msg.data[1] = msg.data[1];

				//Send the new message
				can_send_message_fifo(&msg);
			}

			//prot=10 = Basis IO data[0]=04 = Jump2Bootloader
			if (shackbus_id.prot == 10 && shackbus_id.dst == 8 && msg.data[0]==04)
			{
				cli();
				wdt_enable(WDTO_15MS );
				while (1);
			}

			/* prot=11 = PowerManagement data[0]=1 =on/off data[1]=channel data[2]=state */
			/* channel 140 0x8c = DLE Dusche    */
			/* channel 141 0x8d = DLE E-Lab     */
			/* channel 142 0x8e = Optionsraeume  */
			/* channel 143 0x8f = Kueche         */
			if (shackbus_id.prot == 11 && shackbus_id.dst == 6 && msg.data[0]==1 \
				&& msg.data[1] >= 140 && msg.data[1] <= 143)
			{
				shackbus_id.prio = 3;
				shackbus_id.vlan = 4;
				shackbus_id.dst  = shackbus_id.src;
				shackbus_id.src  = 8;
				shackbus_id.prot = 11;

				msg.id = shackbus_sb2id(&shackbus_id);
				msg.length = 3;
				msg.data[0] = 1;
				msg.data[1] = msg.data[1];
				msg.data[2] = msg.data[2];

				/* Send the message */
				can_send_message_fifo(&msg);

				if (msg.data[2]==1) enocean_state_set(msg.data[1],ENOCEAN_CHANNEL_SE_SA_SS);
				if (msg.data[2]==0) enocean_state_set(msg.data[1],ENOCEAN_CHANNEL_SE_SA_CS);
			}



			/* prot=11 = PowerManagement data[0]=0 =on/off data[1]=channel data[2]=state */
			/* channel 140 0x8c = DLE Dusche    */
			/* channel 141 0x8d = DLE E-Lab     */
			/* channel 142 0x8e = Optionsraeume  */
			/* channel 143 0x8f = Kueche         */
			if (shackbus_id.prot == 11 && shackbus_id.dst == 6 && msg.data[0]==0 \
				&& msg.data[1] >= 140 && msg.data[1] <= 143)
			{
				shackbus_id.prio = 3;
				shackbus_id.vlan = 4;
				shackbus_id.dst  = shackbus_id.src;
				shackbus_id.src  = 8;
				shackbus_id.prot = 11;

				msg.id = shackbus_sb2id(&shackbus_id);
				msg.length = 3;
				msg.data[0] = 0;
				msg.data[1] = msg.data[1];
				msg.data[2] = msg.data[2];

				/* Send the message */
				can_send_message_fifo(&msg);

				void power_mgt_set_input_1(uint8_t _channel, uint8_t _state);
				void power_mgt_set_wait_off(uint8_t _channel, uint16_t _value);
				power_mgt_set_input_1(msg.data[1]-140, msg.data[2]);
				power_mgt_set_wait_off(msg.data[1]-140, 20);
			}

			/* prot=13 = Hauptschalterstate length=1 data[0]=state */
			/* 0 => OFF    */
			/* 1 => ON     */
			if (shackbus_id.prot == 13 && shackbus_id.dst == 255 && msg.length == 1)
			{
				shackbus_id.prio = 3;
				shackbus_id.vlan = 4;
				shackbus_id.dst  = shackbus_id.src;
				shackbus_id.src  = 8;
				shackbus_id.prot = 13;

				msg.id = shackbus_sb2id(&shackbus_id);
				msg.length = 3;
				msg.data[1] = 2;
				msg.data[2] = 3;

				/* Send the message */
//				can_send_message(&msg);

				if (msg.data[0] > 1)
					msg.data[0] = 0;

				//power_mgt_set_input_1 (1, msg.data[0]);
				power_mgt_set_wait_on (1, 4); //DLE Kueche
				power_mgt_set_wait_off(1, 5);
				power_mgt_set_input_1 (2, msg.data[0]);
				power_mgt_set_wait_on (2, 2); //OR
				power_mgt_set_wait_off(2, 4);
				power_mgt_set_input_1 (3, msg.data[0]);
				power_mgt_set_wait_on (3, 3); //Kueche
				power_mgt_set_wait_off(3, 90 * 60); //Sekunden
			}



		}
	}

	if (shackbus_system_time_send_flag >= 60)
	{
		shackbus_system_time_send_flag = 0;

		shackbus_id_t ka_id;
		ka_id.prio = 1;
		ka_id.vlan = 3;
		ka_id.dst  = 255;
		ka_id.src  = 8;
		ka_id.prot = 2;

		can_t ka;
		memset(&ka,0,sizeof(can_t));
		ka.id = shackbus_sb2id(&ka_id);
		ka.length = 8;

		ka.flags.extended = 1;
		ka.flags.rtr = 0;

		uint8_t sreg = SREG;
		cli();

		for (uint8_t i = 0;i<8;i++)
			ka.data[i] = system_time.type8[i];

		SREG = sreg;

		can_send_message_fifo(&ka);
	}

}
Example #2
0
void shackbus_main(void)
{
	if ( fifo_get_count(&can_outfifo) > 0  )
	{
		uint8_t cur_nr = fifo_read (&can_outfifo);
		if ( can_send_message(&framestorage_data[cur_nr]) )
		{
			fifo_get (&can_outfifo);
			framestorage_get(cur_nr);
		}
	}

	if (shackbus_msg_send_flag)
	{
		can_t msg;
		shackbus_id_t shackbus_id;


		shackbus_msg_send_flag = 0;

		shackbus_id.prio = 3;
		shackbus_id.vlan = 4;
		shackbus_id.dst  = 255;
		shackbus_id.src  = 6;
		shackbus_id.prot = 13;

		memset(&msg,0,sizeof(can_t));
		msg.id = shackbus_sb2id(&shackbus_id);
		msg.length = 1;
		msg.flags.extended = 1;
		msg.flags.rtr = 0;

		if (MAINSWITCH_STATE)
			msg.data[0] = 1;
		else
			msg.data[0] = 0;

		//Send the new message
		can_send_message_fifo(&msg);
	}


	// Check if a new messag was received
	if (can_check_message())
	{
		can_t msg;

		// Try to read the message
		if (can_get_message(&msg))
		{
			if (msg.id == 0x1FF && msg.flags.extended == 1)
			{
				led_set(99,0);
			}
			shackbus_id_t shackbus_id;
			shackbus_id2sb(&shackbus_id,&msg);

			if (shackbus_id.prot == 9 && shackbus_id.dst == 6 && msg.data[0]<12)
			{
				if (msg.data[1]==1) enocean_state_set(msg.data[0],ENOCEAN_CHANNEL_SE_SA_SS);
				if (msg.data[1]==0) enocean_state_set(msg.data[0],ENOCEAN_CHANNEL_SE_SA_CS);

				//Send the new message
				//shackbus_send_msg(msg.data[0], msg.data[1]);
			}

			//prot=10 = Basis IO data[0]=10 = Ping
			if (shackbus_id.prot == 10 && shackbus_id.dst == 6 && msg.data[0]==10)
			{
				shackbus_id.prio = 3;
				shackbus_id.vlan = 4;
				shackbus_id.dst  = shackbus_id.src;
				shackbus_id.src  = 6;
				shackbus_id.prot = 10;

				msg.id = shackbus_sb2id(&shackbus_id);
				msg.length = 2;
				msg.data[0] = 11;
				msg.data[1] = msg.data[1];

				//Send the new message
				can_send_message_fifo(&msg);
			}

			//prot=10 = Basis IO data[0]=04 = Jump2Bootloader
			if (shackbus_id.prot == 10 && shackbus_id.dst == 6 && msg.data[0]==04)
			{
				cli();
				wdt_enable(WDTO_15MS);
				while (1);
			}

			/* prot=11 = PowerManagement data[0]=1 =on/off data[1]=channel data[2]=state */
			/* channel 120 = Hauptschalter      */
			/* channel 140 0x8c = DLE Dusche    */
			/* channel 141 0x8d = DLE E-Lab     */
			/* channel 142 0x8e = Optionsraeume  */
			/* channel 143 0x8f = Kueche         */
			if (shackbus_id.prot == 11 && shackbus_id.dst == 6 && msg.data[0]==1)
			{
				shackbus_id.prio = 3;
				shackbus_id.vlan = 4;
				shackbus_id.dst  = shackbus_id.src;
				shackbus_id.src  = 6;
				shackbus_id.prot = 11;

				msg.id = shackbus_sb2id(&shackbus_id);
				msg.length = 3;
				msg.data[0] = 1;
				msg.data[1] = msg.data[1];
				msg.data[2] = msg.data[2];

				/* Send the message */
				can_send_message_fifo(&msg);
				enocean_packet_send(msg.data[1], msg.data[2]);
			}
		}
	}

	if (shackbus_system_time_send_flag >= 60)
	{
		shackbus_system_time_send_flag = 0;

		shackbus_id_t ka_id;
		ka_id.prio = 1;
		ka_id.vlan = 3;
		ka_id.dst  = 255;
		ka_id.src  = 6;
		ka_id.prot = 2;

		can_t ka;
		memset(&ka,0,sizeof(can_t));
		ka.id = shackbus_sb2id(&ka_id);
		ka.length = 8;

		ka.flags.extended = 1;
		ka.flags.rtr = 0;

		uint8_t sreg = SREG;
		cli();

		for (uint8_t i = 0;i<8;i++)
			ka.data[i] = system_time.type8[i];

		SREG = sreg;

		can_send_message_fifo(&ka);
	}

}