Subversion Repositories HomeAutomation

Rev

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

  1. /**
  2.  * CanSimpleCLCD. Simple version.
  3.  * For HD44780- and KS0073-based LCD displays
  4.  *
  5.  * Contains no intelligence. It just prints what it is told to.
  6.  *
  7.  * @date    2006-12-11
  8.  * @author  Erik Larsson
  9.  *
  10.  */
  11.  
  12. /*-----------------------------------------------------------------------------
  13.  * Includes
  14.  *---------------------------------------------------------------------------*/
  15. /* system files */
  16. #include <avr/io.h>
  17. #include <avr/interrupt.h>
  18. #include <stdio.h>
  19. /* lib files */
  20. #include <bios.h>
  21. #include <lcd_HD44780.h>
  22. /* funcdefs */
  23. #include <funcdefs.h>
  24.  
  25. /*-----------------------------------------------------------------------------
  26.  * Defines
  27.  *---------------------------------------------------------------------------*/
  28. #define SIZE_X  20
  29. #define SIZE_Y  4
  30. #define SIZE_LCD LCD_SIZE_20x4
  31.  
  32. #define BUFFER_SIZE SIZE_X
  33. #define TRUE 1
  34. #define FALSE 0
  35.  
  36. #define LEFT    0x01
  37. #define RIGHT   0x02
  38.  
  39. Can_Message_t txMsg;
  40.  
  41. char incoming_text[ BUFFER_SIZE ];
  42. uint8_t msg_received = FALSE;
  43.  
  44. uint8_t rot_lastdir = 0,
  45.         rot_laststate = 0;
  46.  
  47. char buffer[ BUFFER_SIZE ];
  48.  
  49. void send_dimensions();
  50.  
  51. void can_receive(Can_Message_t *msg){
  52.     uint32_t act;
  53.     uint8_t m, act_type, lcd_action, lcd_size;
  54.     if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){
  55.         act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
  56.         act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
  57.         lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS;
  58.         lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS;
  59.     }
  60.  
  61.     if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){
  62.         switch( lcd_action ){
  63.             case LCD_ACTION_CLR:
  64.                     // Clear LCD
  65.                     lcd_clrscr();
  66.                 break;
  67.             case LCD_ACTION_CURS:
  68.                     // Move cursor
  69.                     lcd_gotoxy( msg->Data.bytes[0],msg->Data.bytes[1] );
  70.                 break;
  71.             case LCD_ACTION_GET_SIZE:
  72.                     // Request LCD dimension
  73.                     send_dimensions();
  74.                 break;
  75.             case LCD_ACTION_TEXT:
  76.                 // Print text to LCD
  77.                 for(m=0;m<msg->DataLength;m++){
  78.                     lcd_putc( (char)msg->Data.bytes[m] );
  79.                 }
  80.                 break;
  81.             case LCD_ACTION_SET_CONT:
  82.                 // Set contrast
  83.                 if(msg->DataLength == 1){
  84.                     OCR0B = msg->Data.bytes[0];
  85.                 }
  86.                 break;
  87.             case LCD_ACTION_SET_BLIGHT:
  88.                 // Set backlight
  89.                 if(msg->DataLength == 1){
  90.                     OCR1AL = msg->Data.bytes[0];
  91.                 }
  92.                 break;
  93.             case LCD_ACTION_GET_CONT:
  94.                 // Get contrast
  95.                 txMsg.DataLength = 1;
  96.                 txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID;
  97.                 txMsg.Data.bytes[0] = OCR0B;
  98.                 bios->can_send(&txMsg);
  99.                 break;
  100.             case LCD_ACTION_GET_BLIGHT:
  101.                 // Get backlight
  102.                 txMsg.DataLength = 1;
  103.                 txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID;
  104.                 txMsg.Data.bytes[0] = OCR1AL;
  105.                 bios->can_send(&txMsg);
  106.                 break;
  107.             default:
  108.                 // Take over the world
  109.                 break;
  110.         }
  111.     }
  112. }
  113.  
  114. ISR( PCINT0_vect ){ // For ROTENC_A and ROTENC_B
  115.     uint8_t rot_data = 0;
  116.  
  117.     txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
  118.     txMsg.DataLength=2;
  119.  
  120.     if(PINB&(1<<PB7)){
  121.         rot_data |= 0x01;
  122.     }
  123.     if(PINB&(1<<PB0)){
  124.         rot_data |= 0x02;
  125.     }
  126.  
  127.     if( rot_data==0 || rot_data==3 ){
  128.         if( rot_data==0 && rot_laststate!=rot_data ){
  129.             if( rot_lastdir&0x01 ){
  130.                 // Moving right
  131.                 txMsg.Data.bytes[0]=RIGHT;
  132.                 txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
  133.                 bios->can_send(&txMsg);
  134.                 // For testing TODO remove
  135.                 if(OCR1A<0xFF){
  136.                     OCR1A++;
  137.                 }
  138.             }else{
  139.                 // Moving left
  140.                 txMsg.Data.bytes[0]=LEFT;
  141.                 txMsg.Data.bytes[1] = (PIND&(1<<PD2))?0:1;
  142.                 bios->can_send(&txMsg);
  143.                 // For testing TODO remove
  144.                 if(OCR1A>0){
  145.                     OCR1A--;
  146.                 }
  147.             }
  148.         }
  149.         rot_laststate = rot_data;
  150.     }else{
  151.         rot_lastdir = rot_data;
  152.     }
  153. }
  154.  
  155. ISR( PCINT2_vect ){ // For ROTENC_BTN
  156.     txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x02<<SNS_ID_BITS )| NODE_ID;
  157.     txMsg.Data.bytes[0] = (PIND&(1<<PD2))?0:1; // Check if buton is pressed or not
  158.     txMsg.DataLength=1;
  159.     bios->can_send(&txMsg);
  160. }
  161.  
  162. /*-----------------------------------------------------------------------------
  163.  * Main Program
  164.  *---------------------------------------------------------------------------*/
  165. int main(void) {
  166.  
  167.     /*
  168.      * Initialize rotaryencoders and buttons
  169.      */
  170.     /* Set as input */
  171.     DDRD &= ~(1<<DDD2);
  172.     DDRB &= ~((1<<DDB7)|(1<<DDB0));
  173.     /* Enable pull-up */
  174.     PORTD |= (1<<PD2);
  175.     PORTB |= (1<<PB7)|(1<<PB0);
  176.     /* Enable IO-pin interrupt */
  177.     PCICR |= (1<<PCIE2)|(1<<PCIE0);
  178.     /* Unmask PB7, PB0 and PD2 */
  179.     PCMSK2 |= (1<<PCINT18);
  180.     PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
  181.  
  182.     /* Contrast */
  183.     TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
  184.     TCCR0B |= (1<<CS00);
  185.     OCR0B = 0x20;
  186.     DDRD |= (1<<DDD5);
  187.  
  188.     /* Backlight */
  189.     TCCR1A |= (1<<COM1A1)|(1<<WGM11);
  190.     TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
  191.     ICR1 = 0xFF; // Make it 8 bits
  192.     OCR1A = 0x20;
  193.     DDRB |= (1<<DDB1);
  194.  
  195.     txMsg.ExtendedFlag=1;
  196.     txMsg.RemoteFlag=0;
  197.  
  198.     sei();
  199.     bios->can_callback = &can_receive;
  200.  
  201.     lcd_init( LCD_DISP_ON );
  202.     lcd_clrscr();
  203.     lcd_puts("HomeAutomation\n");
  204.  
  205.     send_dimensions();
  206.     lcd_puts("Very nice!\n");
  207.  
  208.     /* main loop */
  209.     while(1){
  210.         /* check if any messages have been received */
  211.         if(msg_received){
  212.  
  213.             msg_received = FALSE;
  214.         }
  215.     }
  216. }
  217.  
  218. void send_dimensions(){
  219.     txMsg.Id=0xA000001;
  220.     txMsg.Data.bytes[0] = SIZE_X;
  221.     txMsg.Data.bytes[1] = SIZE_Y;
  222.     txMsg.DataLength = 2;
  223.     bios->can_send(&txMsg);
  224. }
  225.