Difference between revisions of "U23 2007/MateCrew"

From C4 Wiki
Jump to: navigation, search
(avr-portremote)
(avr-portremote: fix indent)
Line 9: Line 9:
 
   
 
   
 
  struct fnord_command{
 
  struct fnord_command{
        /*
+
/*
stores all needed protocol data
+
* stores all needed protocol data
*/
+
*/
 
  uint8_t ctype;  //command type (set,unset,additive,substractive)
 
  uint8_t ctype;  //command type (set,unset,additive,substractive)
 
  uint8_t ioport; //ioport  (PORTA/C/D)
 
  uint8_t ioport; //ioport  (PORTA/C/D)
Line 26: Line 26:
 
   
 
   
 
  void net_add_port(){
 
  void net_add_port(){
/*
+
/*
add incoming value to PORTX
+
* add incoming value to PORTX
*/
+
*/
 
  cfg.options.io_ddr[(incoming_command.ioport)] |= incoming_command.data;  
 
  cfg.options.io_ddr[(incoming_command.ioport)] |= incoming_command.data;  
 
  cfg.options.io[(incoming_command.ioport)] |= incoming_command.data;  
 
  cfg.options.io[(incoming_command.ioport)] |= incoming_command.data;  
Line 35: Line 35:
 
   
 
   
 
  void net_sub_port(){
 
  void net_sub_port(){
/*
+
/*
substract incoming value from PORTX
+
* substract incoming value from PORTX
*/
+
*/
 
  cfg.options.io[(incoming_command.ioport)] &= ~(incoming_command.data);  
 
  cfg.options.io[(incoming_command.ioport)] &= ~(incoming_command.data);  
 
   
 
   
Line 44: Line 44:
 
  void net_get_port(){
 
  void net_get_port(){
 
  /*
 
  /*
substract incoming value from PORTX
+
  * substract incoming value from PORTX
*/
+
  */
 
  ((uint8_t *)uip_appdata)[0]=NET_GET_PORT;
 
  ((uint8_t *)uip_appdata)[0]=NET_GET_PORT;
 
  ((uint8_t *)uip_appdata)[1]=incoming_command.ioport;
 
  ((uint8_t *)uip_appdata)[1]=incoming_command.ioport;

Revision as of 21:59, 6 June 2007

avr-portremote

test:

#include <string.h>

#include "../uip/uip.h"
#include "../debug.h"
#include "fnord.h"

struct fnord_command{
	/*
	 * stores all needed protocol data
	 */
	uint8_t ctype;  //command type (set,unset,additive,substractive)
	uint8_t ioport; //ioport  (PORTA/C/D)
	uint8_t data;   //data (1 byte)

} incoming_command;

void init_fnord(void){
       // we listen on port 2324 (defined in fnord.h)
	uip_listen(HTONS(FNORD_PORT));

}


void net_add_port(){
	/*
	 * add incoming value to PORTX
	 */
	cfg.options.io_ddr[(incoming_command.ioport)] |= incoming_command.data; 
	cfg.options.io[(incoming_command.ioport)] |= incoming_command.data; 

}

void net_sub_port(){
	/*
	 * substract incoming value from PORTX
	 */
	cfg.options.io[(incoming_command.ioport)] &= ~(incoming_command.data); 

}

void net_get_port(){
	 /*
	  * substract incoming value from PORTX
	  */
	((uint8_t *)uip_appdata)[0]=NET_GET_PORT;
	((uint8_t *)uip_appdata)[1]=incoming_command.ioport;
	((uint8_t *)uip_appdata)[2]=cfg.options.io[(incoming_command.ioport)];
	((uint8_t *)uip_appdata)[3]='\0';
	
	uip_send(uip_appdata,4);

}

void net_set_port(){
	cfg.options.io[(incoming_command.ioport)] = incoming_command.data;
}

void handle_fnord(void) {

	uint8_t logged_in=0;
	
	if(uip_connected())
		debug_print("New client connected\n");
		
	if(uip_newdata()) {
		((uint8_t *)uip_appdata)[uip_datalen()] = 0;
		
		
		uint8_t offset=0;
		char password[uip_len];
		
		for(offset;((uint8_t *)uip_appdata)[offset]!=' ' && offset<uip_len;offset++){
				password[offset]=((uint8_t *)uip_appdata)[offset];	
		}
		
		password[offset]='\0';
		
		debug_printf("Password is: '%s', user entered: '%s' \n",FNORD_PASS,password);
		
		if(!(strcmp(password,FNORD_PASS)))
			logged_in=1;
		else
			logged_in=0;
			
		if(logged_in){
			debug_print("Password correct, logged in!\n\n");
			
			incoming_command.ctype=((uint8_t *)uip_appdata)[++offset];
			incoming_command.ioport=((uint8_t *)uip_appdata)[++offset];
			incoming_command.data=((uint8_t *)uip_appdata)[++offset];
			
			debug_print("New data recieved:\n");
			debug_printf("ctype: %i\n",incoming_command.ctype);
			debug_printf("ioport: %i\n",incoming_command.ioport);
			debug_printf("data: %i\n\n\n",incoming_command.data);
			
			switch(incoming_command.ctype){
				case NET_ADD_PORT: net_add_port();
					  break;
				case NET_SUB_PORT: net_sub_port();
					  break;
				case NET_GET_PORT: net_get_port(); 	
					  break;
				case NET_SET_PORT: net_set_port();
					  break;
			}
		}
		else
			debug_print("Incorrect password!\n\n\n");
	}

}

LED-Dimmer

Für unseren Dimmer für LEDs benutzen wir insgesamt 3 Funktionen:

  • Die Main mit einer Endlosschleife für ewiges hell und dunkel Dimmen durch Aufruf von den andern Beiden Funktionen
  • Die Funktion my_wait(uint32_t time), welche einfach wartet
  • Die Funktion dim(uint8_t ratio), welche für den schnellen Ein-/Ausschaltvorgang der LEDs zuständig ist

Die Main:

 #include <avr/io.h>
 #include <util/delay.h>
 #define F_CPU 2000000000L

 void my_wait(uint32_t);
 void dim(uint8_t); 
 int main(){	
   DDRA=0xff;
 
   // LED ausschalten
   PORTA=0;
   uint16_t i=0;    
   PORTA|=0x02;
 
   //Die Endlosschleife
   for(;;){
     for(i=0;i<0xfff;i++){
 
       // ein bisschen warten
       my_wait(1);
 
       // ein- und ausschalten
       // je größer i wird desto größer wird auch der Quotient der an bla übergeben wird
       // dadurch verändert sich auch der zeitliche Abstand zwischen dem Ein- und Ausschalten
       // wodurch die LED wiederum heller oder dunkler erscheint
       dim(i/0xf);		
     }
   }
   PORTA|=0x03;	
   return 0;
 }

Die Wait:

 void my_wait(uint32_t time)
 {	
   uint32_t i=0;
   for(i;i<(time);i++)
 
   //vordefinierte Funktion welche einen Leerlauf in Dauer von n Milisekunden auslöst
   _delay_ms(1);
 }

Die bla:

 void dim(uint8_t ratio){
   uint32_t i=0;	
 
   // LED anschalten
   PORTA|=0x01;
   for(i;i<ratio;i++){}
 
   // LED ausschalten
   PORTA&=~0x01;
   for(i=ratio;i<255;i++){}
 }

Project

Team

  • Scout
  • F3lix
  • Toby