Subversion Repositories HomeAutomation

Rev

Rev 334 | Rev 344 | 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. #include <clcd20x4.h>
  24. /* funcdefs */
  25. //#include <global_funcdefs.h>
  26. #include <eqlazer_funcdefs.h>
  27.  
  28. /* defines */
  29. #define BUFFER_SIZE 20
  30. #define TRUE 1
  31. #define FALSE 0
  32.  
  33. Can_Message_t rxMsg;
  34.  
  35. uint8_t currentView = MAIN_VIEW,
  36.         msg_received = FALSE;
  37.  
  38. uint8_t temperatureData[8],
  39.         servoPosition[4],
  40.         //relayStatus[4],
  41.         //dimmerStatus[4],
  42.         temperatureLength = 8,
  43.         servoLength = 0;
  44.         //, relayLength=0; //TODO mer generaliserat
  45.  
  46. uint8_t rot_lastdir = 0,
  47.         rot_laststate = 0;
  48.  
  49. ISR( PCINT2_vect ){
  50.     uint8_t rot_data = 0;
  51.  
  52.     if(PIND&(1<<PD6)){
  53.         rot_data |= 0x01;
  54.     }
  55.     if(PIND&(1<<PD7)){
  56.         rot_data |= 0x02;
  57.     }
  58.  
  59.     if( rot_data==3 && rot_laststate!=3 ){
  60.         if( rot_lastdir&0x01 ){
  61.             // Moving left
  62.             if(currentView>0){
  63.                 currentView--;
  64.                 msg_received = TRUE;
  65.             }
  66.         }else{
  67.             // Moving right
  68.             if(currentView<LAST_VIEW){
  69.                 currentView++;
  70.                 msg_received = TRUE;
  71.             }
  72.         }
  73.         rot_laststate=3;
  74.     }else if( rot_data==0 && rot_laststate!=0 ){
  75.         rot_laststate = 0;
  76.     }else if( rot_data==1 || rot_data==2 ){
  77.         rot_lastdir = rot_data;
  78.     }
  79. }
  80.  
  81. void can_receive(Can_Message_t *msg){
  82.     uint8_t temp, m;
  83.     /* Check for incoming messages from IR-receiver node */
  84.     if( CLASS_SNS == ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) ){
  85.         /* FIXME detta är väldigt fult och borde fixas
  86.          * knapp 4 på fjärren bläddrar åt vänster
  87.          * knapp 6 åt höger
  88.          */
  89.         if( SNS_IR_RECEIVE == (msg->Id & TYPE_MASK)>>TYPE_MASK_BITS){
  90.             if(msg->Data.bytes[0] == 0 && msg->Data.bytes[1] == 0 && msg->Data.bytes[2] == 0){
  91.                 if(msg->Data.bytes[3] == 4 && currentView>0){
  92.                     currentView--;
  93.                     msg_received = TRUE;
  94.                 }else if(msg->Data.bytes[3] == 6 && currentView<LAST_VIEW){
  95.                     currentView++;
  96.                     msg_received = TRUE;
  97.                 }
  98.             }
  99.         }
  100.         /* Other sensor messages should be placed here */
  101.  
  102.         /* DS18S20 sensors */
  103.         if( SNS_TEMP_DS18S20 == (msg->Id & SNS_TYPE_MASK)>>SNS_TYPE_BITS ){
  104.             temp = (msg->Id & SNS_ID_MASK)>>SNS_ID_BITS;
  105.             temperatureData[ (temp-1)*2 ] = msg->Data.bytes[0];
  106.             temperatureData[ (temp-1)*2+1 ] = msg->Data.bytes[1];
  107.         }
  108.  
  109.         /* Servo controllers */
  110.         if( SNS_ACT_STATUS_SERVO == (msg->Id & TYPE_MASK)>>TYPE_MASK_BITS ){ // TODO annan struktur på statusmeddelanden från servo
  111.             temp = msg->DataLength - 3;
  112.             for(m=0;m<temp;m++){
  113.                 servoPosition[ m ] = msg->Data.bytes[m+3];
  114.             }
  115.             servoLength = temp;
  116.         }
  117.  
  118.     }
  119. }
  120.  
  121.  
  122. /*-----------------------------------------------------------------------------
  123.  * Main Program
  124.  *---------------------------------------------------------------------------*/
  125. int main(void) {
  126.  
  127.     char buffer[ BUFFER_SIZE ];
  128.     /*
  129.      * Initialize rotaryencoders and buttons
  130.      */
  131.     /* Set as input */
  132.     DDRD &= ~( (1<<DDD6)|(1<<DDD7) );
  133.     /* Enable IO-pin interrupt */
  134.     PCICR |= (1<<PCIE2);
  135.     /* Unmask PD6 and PD7 */
  136.     PCMSK2 |= (1<<PCINT22)|(1<<PCINT23);
  137.  
  138.     sei();
  139.     bios->can_callback = &can_receive;
  140.  
  141.     lcd_init( LCD_DISP_ON );
  142.     lcd_clrscr();
  143.  
  144.     lcd_clrscr();
  145.     printLCDview( MAIN_VIEW );
  146.  
  147.     /* main loop */
  148.     while(1){
  149.         /* check if any messages have been received */
  150.         if(msg_received){
  151.                 /* Print views skeleton and if existing its data */
  152.             printLCDview( currentView );
  153.             if(currentView == TEMPSENS_VIEW){
  154.                 printLCDviewData( TEMPSENS_VIEW, temperatureData, temperatureLength);
  155.             }else if(currentView == SERVO_VIEW){
  156.                 printLCDviewData( SERVO_VIEW, servoPosition, servoLength);
  157.             }
  158.             msg_received = FALSE;
  159.         }
  160.     }
  161. }
  162.