Subversion Repositories HomeAutomation

Rev

Rev 335 | Rev 348 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * CanCLCD.
  3.  * For HD44780- and KS0073-based LCD displays
  4.  * Prints sensor data and other interesting things.
  5.  *
  6.  * @date    2006-12-11
  7.  * @author  Erik Larsson
  8.  *
  9.  * TODO Fix more types of output: relay status, mail recieved etc.
  10.  *
  11.  */
  12.  
  13. /*-----------------------------------------------------------------------------
  14.  * Includes
  15.  *---------------------------------------------------------------------------*/
  16. /* system files */
  17. #include <avr/io.h>
  18. #include <avr/interrupt.h>
  19. #include <stdio.h>
  20. /* lib files */
  21. #include <bios.h>
  22. #include <lcd_HD44780.h>
  23. /* funcdefs */
  24. //#include <eqlazer_funcdefs.h>
  25.  
  26. /* defines */
  27. #define SIZE_X  20
  28. #define SIZE_Y  4
  29.  
  30. //#define BUFFER_SIZE 20
  31. #define TRUE 1
  32. #define FALSE 0
  33.  
  34. #define LEFT    0x01
  35. #define RIGHT   0x02
  36.  
  37. Can_Message_t rxMsg;
  38. Can_Message_t txMsg;
  39.  
  40.  
  41. uint8_t     msg_received = FALSE;
  42.  
  43. uint8_t rot_lastdir = 0,
  44.         rot_laststate = 0;
  45.  
  46.  
  47. void can_receive(Can_Message_t *msg){
  48. // FIXME
  49. }
  50.  
  51.  
  52. ISR( PCINT0_vect ){ // For ROTENC_A and ROTENC_B
  53.     uint8_t rot_data = 0;
  54.  
  55.     txMsg.Id=0xA000000;
  56.     txMsg.DataLength=2;
  57.  
  58.     if(PINB&(1<<PB7)){
  59.         rot_data |= 0x01;
  60.     }
  61.     if(PINB&(1<<PB0)){
  62.         rot_data |= 0x02;
  63.     }
  64.  
  65.     if( rot_data==0 || rot_data==3 ){
  66.         if( rot_data==0 && rot_laststate!=rot_data ){
  67.             if( rot_lastdir&0x01 ){
  68.                 // Moving right
  69.                 txMsg.Data.bytes[0]=RIGHT;
  70.                 txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
  71.                 bios->can_send(&txMsg);
  72.             }else{
  73.                 // Moving left
  74.                 txMsg.Data.bytes[0]=LEFT;
  75.                 txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
  76.                 bios->can_send(&txMsg);
  77.             }
  78.         }
  79.         rot_laststate = rot_data;
  80.     }else{
  81.         rot_lastdir = rot_data;
  82.     }
  83. }
  84.  
  85. ISR( PCINT2_vect ){ // For ROTENC_BTN
  86.     lcd_puts("a");
  87. }
  88.  
  89.     void send_dimensions();
  90.  
  91. /*-----------------------------------------------------------------------------
  92.  * Main Program
  93.  *---------------------------------------------------------------------------*/
  94. int main(void) {
  95.  
  96. //    char buffer[ BUFFER_SIZE ];
  97.     /*
  98.      * Initialize rotaryencoders and buttons
  99.      */
  100.     /* Set as input */
  101.     DDRD &= ~(1<<DDD2);
  102.     DDRB &= ~((1<<DDB7)|(1<<DDB0));
  103.     /* Enable pull-up */
  104.     PORTD |= (1<<PD2);
  105.     PORTB |= (1<<PB7)|(1<<PB0);
  106.     /* Enable IO-pin interrupt */
  107.     PCICR |= (1<<PCIE2)|(1<<PCIE0);
  108.     /* Unmask PB7, PB0 and PD2 */
  109.     PCMSK2 |= (1<<PCINT18);
  110.     PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
  111.  
  112.     txMsg.ExtendedFlag=1;
  113.     txMsg.RemoteFlag=0;
  114.  
  115.     sei();
  116.     bios->can_callback = &can_receive;
  117.  
  118.     lcd_init( LCD_DISP_ON );
  119.     lcd_clrscr();
  120.  
  121.     send_dimensions();
  122.  
  123.     /* main loop */
  124.     while(1){
  125.         /* check if any messages have been received */
  126.         if(msg_received){
  127.  
  128.             msg_received = FALSE;
  129.         }
  130.     }
  131. }
  132.  
  133. void send_dimensions(){
  134.     txMsg.Id=0xA000001;
  135.     txMsg.Data.bytes[0] = SIZE_X;
  136.     txMsg.Data.bytes[1] = SIZE_Y;
  137.     txMsg.DataLength = 2;
  138.     bios->can_send(&txMsg);
  139. }
  140.