Subversion Repositories HomeAutomation

Rev

Rev 563 | Rev 607 | 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 <inttypes.h>
  18. #include <avr/interrupt.h>
  19. #include <stdio.h>
  20. /* lib files */
  21. #include <bios.h>
  22. #include <config.h>
  23. #include <drivers/lcd/clcd/lcd_HD44780.h>
  24.  
  25. #include <string.h> //for memcpy
  26.  
  27.  
  28. /* funcdefs */
  29. //#include <funcdefs/funcdefs.h>
  30.  
  31.  
  32. //a lot of crap from previous funcdefs
  33. /* ACT LCD 12 bits <ACTION><SIZE> 0xFFFL
  34.  *
  35.  * <ACTION>, 4 bits = Type och action.
  36.  * <SIZE>, 8 bits = Size of display.
  37.  */
  38. #define LCD_ACTION_MASK 0xF00L /* bit[12..8] */
  39. #define LCD_SIZE_MASK 0x0FFL /* bit[7..0] */
  40.  
  41. #define LCD_ACTION_BITS 8
  42. #define LCD_SIZE_BITS 0
  43.  
  44.  
  45. /* <ACTION> 4 bits 0xFL */
  46.  
  47. #define LCD_ACTION_CLR          0x0L
  48. #define LCD_ACTION_CURS         0x1L
  49. #define LCD_ACTION_TEXT         0x2L
  50. #define LCD_ACTION_GET_SIZE     0x3L
  51. #define LCD_ACTION_SET_CONT     0x4L
  52. #define LCD_ACTION_SET_BLIGHT   0x5L
  53. #define LCD_ACTION_GET_CONT     0x6L
  54. #define LCD_ACTION_GET_BLIGHT   0x7L
  55.  
  56. #define LCD_ACTION_SEND_CONT    0x8L
  57. #define LCD_ACTION_SEND_BLIGHT  0x9L
  58.  
  59.  
  60.  
  61. /* <SIZE> 8 bits 0xFFL */
  62. #define LCD_SIZE_ALL    0x00L
  63. #define LCD_SIZE_16x2   0x02L
  64. #define LCD_SIZE_20x4   0x10L
  65.  
  66.  
  67.  
  68. /*-----------------------------------------------------------------------------
  69.  * Defines
  70.  *---------------------------------------------------------------------------*/
  71. #define SIZE_X  20
  72. #define SIZE_Y  4
  73. #define SIZE_LCD LCD_SIZE_20x4
  74.  
  75. #define BUFFER_SIZE SIZE_X
  76. #define TRUE 1
  77. #define FALSE 0
  78.  
  79. #define LEFT    0x01
  80. #define RIGHT   0x02
  81.  
  82. volatile Can_Message_t rxMsg;
  83.  
  84. char incoming_text[ BUFFER_SIZE ];
  85. uint8_t rxMsgFull;
  86.  
  87.  
  88. char buffer[ BUFFER_SIZE ];
  89.  
  90. void send_dimensions(void);
  91. void rotenc(void);
  92.  
  93. // CAN message reception callback
  94. // This function runs with interrupts disabled, keep it as short as possible
  95. void can_receive(Can_Message_t *msg){
  96.     if(!rxMsgFull){
  97.         memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
  98.         rxMsgFull = 1;
  99.     }
  100. }
  101.  
  102. ISR( PCINT2_vect ){
  103. // For ROTENC_A and ROTENC_B FIXME är inte riktigt rätt
  104.     rotenc();
  105. }
  106.  
  107. ISR( PCINT0_vect ){
  108. // For ROTENC_BTN
  109. rotenc();
  110. }
  111.  
  112. /*-----------------------------------------------------------------------------
  113.  * Main Program
  114.  *---------------------------------------------------------------------------*/
  115. int main(void) {
  116.     uint32_t act;
  117.     uint8_t m, act_type, lcd_action, lcd_size;
  118.  
  119.     sei();
  120.  
  121.     /*
  122.      * Initialize rotaryencoders and buttons
  123.      */
  124.     // Set as input
  125.     DDRD &= ~(1<<DDD2);
  126.     DDRB &= ~((1<<DDB7)|(1<<DDB0));
  127.     // Enable pull-up
  128.     PORTD |= (1<<PD2);
  129.     PORTB |= (1<<PB7)|(1<<PB0);
  130.     // Enable IO-pin interrupt
  131.     PCICR |= (1<<PCIE2)|(1<<PCIE0);
  132.     // Unmask PB7, PB0 and PD2
  133.     PCMSK2 |= (1<<PCINT18);
  134.     PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
  135.  
  136.     // Contrast
  137.     TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
  138.     TCCR0B |= (1<<CS00);
  139.     OCR0B = 0x10;
  140.     DDRD |= (1<<DDD5);
  141.  
  142.     // Backlight
  143.     TCCR1A |= (1<<COM1A1)|(1<<WGM11);
  144.     TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
  145.     ICR1 = 0xFF; // Make it 8 bits
  146.     OCR1A = 0x80;
  147.     DDRB |= (1<<DDB1);
  148.  
  149.     Can_Message_t txMsg;
  150.     txMsg.ExtendedFlag=1;
  151.     txMsg.RemoteFlag=0;
  152. // FIXME skicka app startad
  153.     BIOS_CanCallback = &can_receive;
  154.  
  155.     lcd_init( LCD_DISP_ON );
  156.     lcd_clrscr();
  157.     lcd_puts("HomeAutomation\n");
  158.  
  159.     send_dimensions();
  160.     lcd_puts("Very nice!\n");
  161.  
  162.     /* main loop */
  163.     while(1){
  164.         /* check if any messages have been received */
  165.         if(rxMsgFull){
  166. /* FIXME noddan kommenterar bort allt
  167.             if( ((rxMsg.Id & CLASS_MASK)>> CLASS_MASK_BITS) == CLASS_ACT ){ // FIXME ett jäkla herk, städa upp
  168.                 act = (rxMsg.Id & TYPE_MASK) >> TYPE_MASK_BITS;
  169.                 act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
  170.                 lcd_action = (act & LCD_ACTION_MASK) >> LCD_ACTION_BITS;
  171.                 lcd_size = (act & LCD_SIZE_MASK) >> LCD_SIZE_BITS;
  172.  
  173.                 if( act_type == ACT_TYPE_LCD && (lcd_size == SIZE_LCD || lcd_size == LCD_SIZE_ALL)){
  174.                     switch( lcd_action ){
  175.                     case LCD_ACTION_CLR:
  176.                         // Clear LCD
  177.                         lcd_clrscr();
  178.                     break;
  179.                     case LCD_ACTION_CURS:
  180.                         // Move cursor
  181.                         lcd_gotoxy( rxMsg.Data.bytes[0],rxMsg.Data.bytes[1] );
  182.                     break;
  183.                     case LCD_ACTION_GET_SIZE:
  184.                         // Request LCD dimension
  185.                         send_dimensions();
  186.                     break;
  187.                     case LCD_ACTION_TEXT:
  188.                         // Print text to LCD
  189.                         for(m=0;m<rxMsg.DataLength;m++){
  190.                             lcd_putc( (char)rxMsg.Data.bytes[m] );
  191.                         }
  192.                     break;
  193.                     case LCD_ACTION_SET_CONT:
  194.                         // Set contrast
  195.                         if(rxMsg.DataLength == 1){
  196.                             OCR0B = rxMsg.Data.bytes[0];
  197.                         }
  198.                     break;
  199.                     case LCD_ACTION_SET_BLIGHT:
  200.                         // Set backlight
  201.                         if(rxMsg.DataLength == 1){
  202.                             OCR1AL = rxMsg.Data.bytes[0];
  203.                         }
  204.                     break;
  205.                     case LCD_ACTION_GET_CONT:
  206.                         // Get contrast
  207.                         txMsg.DataLength = 1;
  208.                         txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_CONT<<LCD_ACTION_BITS )|NODE_ID;
  209.                         txMsg.Data.bytes[0] = OCR0B;
  210.                         BIOS_CanSend(&txMsg);
  211.                     break;
  212.                     case LCD_ACTION_GET_BLIGHT:
  213.                         // Get backlight
  214.                         txMsg.DataLength = 1;
  215.                         txMsg.Id = ( CLASS_ACT<<CLASS_MASK_BITS )|( ACT_TYPE_LCD<<ACT_TYPE_BITS )|( LCD_ACTION_SEND_BLIGHT<<LCD_ACTION_BITS )|NODE_ID;
  216.                         txMsg.Data.bytes[0] = OCR1AL;
  217.                         BIOS_CanSend(&txMsg);
  218.                     break;
  219.                     default:
  220.                     // Take over the world
  221.                     break;
  222.                     }
  223.                 }
  224.             }
  225.             rxMsgFull = 0;
  226.         */}
  227.     }
  228. }
  229.  
  230. void send_dimensions(){
  231.     Can_Message_t txMsg;
  232.  
  233.     txMsg.ExtendedFlag=1;
  234.     txMsg.RemoteFlag=0;
  235.     txMsg.Id=0xA000001;
  236.     txMsg.Data.bytes[0] = SIZE_X;
  237.     txMsg.Data.bytes[1] = SIZE_Y;
  238.     txMsg.DataLength = 2;
  239.  
  240.     BIOS_CanSend(&txMsg);
  241. }
  242.  
  243. void rotenc(){
  244.     uint8_t rot_data = 0;
  245.     static uint8_t rot_lastdir = 0, rot_laststate = 0; // FIXME måste flyttas ut igen?
  246.  
  247.     Can_Message_t txMsg;
  248.     txMsg.ExtendedFlag=1;
  249.     txMsg.RemoteFlag=0;
  250.     //txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;//FIXME: borttaget
  251.     txMsg.DataLength=2;
  252.  
  253.     if(PINB&(1<<PB0)){
  254.         rot_data |= 0x01;
  255.     }
  256.     if(PIND&(1<<PD2)){
  257.         rot_data |= 0x02;
  258.     }
  259.  
  260.     if( rot_data==0 || rot_data==3 ){
  261.         if( rot_data==0 && rot_laststate!=rot_data ){
  262.             if( rot_lastdir&0x01 ){
  263.                 // Moving right
  264.                 txMsg.Data.bytes[0]=RIGHT;
  265.                 txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
  266.                 txMsg.DataLength=2;
  267.                 BIOS_CanSend(&txMsg);
  268.                 // For testing TODO remove
  269.                 if(OCR1A<0xFF){
  270.                     OCR1A++;
  271.                 }
  272.                 if(OCR0B>0){
  273.                     OCR0B--;
  274.                 }
  275.             }else{
  276.                 // Moving left
  277.                 txMsg.Data.bytes[0]=LEFT;
  278.                 txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
  279.                 txMsg.DataLength=2;
  280.                 BIOS_CanSend(&txMsg);
  281.                 // For testing TODO remove
  282.                 if(OCR1A>0){
  283.                     OCR1A--;
  284.                 }
  285.                 if(OCR0B<0xFF){
  286.                     OCR0B++;
  287.                 }
  288.             }
  289.         }
  290.         rot_laststate = rot_data;
  291.     }else{
  292.         rot_lastdir = rot_data;
  293.     }
  294. }
  295.  
  296.