Subversion Repositories HomeAutomation

Rev

Rev 448 | Rev 563 | 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. Can_Message_t txMsg;
  53.     uint32_t act;
  54.     uint8_t m, act_type, lcd_action, lcd_size;
  55.     if( ((msg->Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){
  56.         act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
  57.         act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
  58.         lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS;
  59.         lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS;
  60.     }
  61.  
  62.     if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){
  63.         switch( lcd_action ){
  64.             case LCD_ACTION_CLR:
  65.                     // Clear LCD
  66.                     lcd_clrscr();
  67.                 break;
  68.             case LCD_ACTION_CURS:
  69.                     // Move cursor
  70.                     lcd_gotoxy( msg->Data.bytes[0],msg->Data.bytes[1] );
  71.                 break;
  72.             case LCD_ACTION_GET_SIZE:
  73.                     // Request LCD dimension
  74.                     send_dimensions();
  75.                 break;
  76.             case LCD_ACTION_TEXT:
  77.                 // Print text to LCD
  78.                 for(m=0;m<msg->DataLength;m++){
  79.                     lcd_putc( (char)msg->Data.bytes[m] );
  80.                 }
  81.                 break;
  82.             case LCD_ACTION_SET_CONT:
  83.                 // Set contrast
  84.                 if(msg->DataLength == 1){
  85.                     OCR0B = msg->Data.bytes[0];
  86.                 }
  87.                 break;
  88.             case LCD_ACTION_SET_BLIGHT:
  89.                 // Set backlight
  90.                 if(msg->DataLength == 1){
  91.                     OCR1AL = msg->Data.bytes[0];
  92.                 }
  93.                 break;
  94.             case LCD_ACTION_GET_CONT:
  95.                 // Get contrast
  96.                 txMsg.DataLength = 1;
  97.                 txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID;
  98.                 txMsg.Data.bytes[0] = OCR0B;
  99.                 BIOS_CanSend(&txMsg);
  100.                 break;
  101.             case LCD_ACTION_GET_BLIGHT:
  102.                 // Get backlight
  103.                 txMsg.DataLength = 1;
  104.                 txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID;
  105.                 txMsg.Data.bytes[0] = OCR1AL;
  106.                 BIOS_CanSend(&txMsg);
  107.                 break;
  108.             default:
  109.                 // Take over the world
  110.                 break;
  111.         }
  112.     }
  113. }
  114.  
  115. ISR( PCINT2_vect ){ // For ROTENC_A and ROTENC_B
  116. /*  uint8_t rot_data = 0;
  117.  
  118.     txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
  119.     txMsg.DataLength=2;
  120.  
  121.     if(PINB&(1<<PB0)){
  122.         rot_data |= 0x01;
  123.     }
  124.     if(PIND&(1<<PD2)){
  125.         rot_data |= 0x02;
  126.     }
  127.  
  128.     if( rot_data==0 || rot_data==3 ){
  129.         if( rot_data==0 && rot_laststate!=rot_data ){
  130.             if( rot_lastdir&0x01 ){
  131.                 // Moving right
  132.                 txMsg.Data.bytes[0]=RIGHT;
  133.                 txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
  134.                 BIOS_CanSend(&txMsg);
  135.                 // For testing TODO remove
  136.                 if(OCR1A<0xFF){
  137.                     OCR1A++;
  138.                 }
  139.                 if(OCR0B<0xFF){
  140.                     OCR0B++;
  141.                 }
  142.             }else{
  143.                 // Moving left
  144.                 txMsg.Data.bytes[0]=LEFT;
  145.                 txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
  146.                 BIOS_CanSend(&txMsg);
  147.                 // For testing TODO remove
  148.                 if(OCR1A>0){
  149.                     OCR1A--;
  150.                 }
  151.                 if(OCR0B>0){
  152.                     OCR0B--;
  153.                 }
  154.             }
  155.         }
  156.         rot_laststate = rot_data;
  157.     }else{
  158.         rot_lastdir = rot_data;
  159.     }*/
  160.     rotenc();
  161. }
  162.  
  163. ISR( PCINT0_vect ){ // For ROTENC_BTN
  164. //  txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x02<<SNS_ID_BITS )| NODE_ID;
  165. //  txMsg.Data.bytes[0] = (PINB&(1<<PB7))?0:1; // Check if buton is pressed or not
  166. //  txMsg.DataLength=1;
  167. //  BIOS_CanSend(&txMsg);
  168.     rotenc();
  169. }
  170.  
  171. void rotenc(){
  172.     uint8_t rot_data = 0;
  173. Can_Message_t txMsg;
  174.     txMsg.ExtendedFlag=1;
  175.     txMsg.RemoteFlag=0;
  176.     txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;
  177.     txMsg.DataLength=2;
  178.  
  179.     if(PINB&(1<<PB0)){
  180.         rot_data |= 0x01;
  181.     }
  182.     if(PIND&(1<<PD2)){
  183.         rot_data |= 0x02;
  184.     }
  185.  
  186.     if( rot_data==0 || rot_data==3 ){
  187.         if( rot_data==0 && rot_laststate!=rot_data ){
  188.             if( rot_lastdir&0x01 ){
  189.                 // Moving right
  190.                 txMsg.Data.bytes[0]=RIGHT;
  191.                 txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
  192.     txMsg.DataLength=2;
  193.                 BIOS_CanSend(&txMsg);
  194.                 // For testing TODO remove
  195.                 if(OCR1A<0xFF){
  196.                     OCR1A++;
  197.                 }
  198.                 if(OCR0B<0xFF){
  199.                     OCR0B++;
  200.                 }
  201.             }else{
  202.                 // Moving left
  203.                 txMsg.Data.bytes[0]=LEFT;
  204.                 txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
  205.     txMsg.DataLength=2;
  206.                 BIOS_CanSend(&txMsg);
  207.                 // For testing TODO remove
  208.                 if(OCR1A>0){
  209.                     OCR1A--;
  210.                 }
  211.                 if(OCR0B>0){
  212.                     OCR0B--;
  213.                 }
  214.             }
  215.         }
  216.         rot_laststate = rot_data;
  217.     }else{
  218.         rot_lastdir = rot_data;
  219.     }
  220. }
  221.  
  222.  
  223. /*-----------------------------------------------------------------------------
  224.  * Main Program
  225.  *---------------------------------------------------------------------------*/
  226. int main(void) {
  227.  
  228.     /*
  229.      * Initialize rotaryencoders and buttons
  230.      */
  231.     /* Set as input */
  232.     DDRD &= ~(1<<DDD2);
  233.     DDRB &= ~((1<<DDB7)|(1<<DDB0));
  234.     /* Enable pull-up */
  235.     PORTD |= (1<<PD2);
  236.     PORTB |= (1<<PB7)|(1<<PB0);
  237.     /* Enable IO-pin interrupt */
  238.     PCICR |= (1<<PCIE2)|(1<<PCIE0);
  239.     /* Unmask PB7, PB0 and PD2 */
  240.     PCMSK2 |= (1<<PCINT18);
  241.     PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
  242.  
  243.     /* Contrast */
  244.     TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
  245.     TCCR0B |= (1<<CS00);
  246.     OCR0B = 0x20;
  247.     DDRD |= (1<<DDD5);
  248.  
  249.     /* Backlight */
  250.     TCCR1A |= (1<<COM1A1)|(1<<WGM11);
  251.     TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
  252.     ICR1 = 0xFF; // Make it 8 bits
  253.     OCR1A = 0x20;
  254.     DDRB |= (1<<DDB1);
  255.  
  256.     Can_Message_t txMsg;
  257.     txMsg.ExtendedFlag=1;
  258.     txMsg.RemoteFlag=0;
  259.  
  260.     sei();
  261.     BIOS_CanCallback = &can_receive;
  262.  
  263.     lcd_init( LCD_DISP_ON );
  264.     lcd_clrscr();
  265.     lcd_puts("HomeAutomation\n");
  266.  
  267.     send_dimensions();
  268.     lcd_puts("Very nice!\n");
  269.  
  270.     /* main loop */
  271.     while(1){
  272.         /* check if any messages have been received */
  273.         if(msg_received){
  274.  
  275.             msg_received = FALSE;
  276.         }
  277.         lcd_puts("test");
  278.     }
  279. }
  280.  
  281. void send_dimensions(){
  282.     Can_Message_t txMsg;
  283.     txMsg.ExtendedFlag=1;
  284.     txMsg.RemoteFlag=0;
  285.     txMsg.Id=0xA000001;
  286.     txMsg.Data.bytes[0] = SIZE_X;
  287.     txMsg.Data.bytes[1] = SIZE_Y;
  288.     txMsg.DataLength = 2;
  289.     BIOS_CanSend(&txMsg);
  290. }
  291.