Subversion Repositories HomeAutomation

Rev

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

  1. /**
  2.  * CAN LCD.
  3.  * For HD44780- and KS0073-based LCD displays
  4.  * This is the early version that only prints temperature sensor data.
  5.  *
  6.  * @date    2006-12-11
  7.  * @author  Erik Larsson
  8.  *
  9.  * TODO Fix more types of output: relay status, mail recieved etc.
  10.  * Make it configurable to use UART, because it's not necessary on the final PCB
  11.  *
  12.  */
  13.  
  14. #define DEBUG 0
  15.  
  16. /*-----------------------------------------------------------------------------
  17.  * Includes
  18.  *---------------------------------------------------------------------------*/
  19. /* system files */
  20. #include <avr/io.h>
  21. #include <avr/interrupt.h>
  22. #include <avr/wdt.h>
  23. #include <stdio.h>
  24. /* lib files */
  25. #include <can.h>
  26. #include <serial.h>
  27. #include <timebase.h>
  28. #include <lcd_HD44780.h>
  29. #include <clcd20x4.h>
  30. /* funcdefs */
  31. #include <global_funcdefs.h>
  32. #include <eqlazer_funcdefs.h>
  33.  
  34. /* defines */
  35. #define BUFFER_SIZE 20
  36.  
  37. #define LCD_CMD_GET 0x1600
  38. #define VIEW_LEFT 0x01
  39. #define VIEW_RIGHT 0x02
  40. #define VIEW_EDIT 0x03
  41.  
  42.  
  43. uint8_t currentView = MAIN_VIEW;
  44.  
  45. // TODO ISR() för att använda optoencoders
  46. ISR( PCINT2_vect ){
  47.     // TODO do the magic stuff
  48.     // vriden medurs currentView++
  49.     // moturs currentView--
  50. } // TODO lägga denna koden i ett eget lib?
  51.  
  52. /*-----------------------------------------------------------------------------
  53.  * Main Program
  54.  *---------------------------------------------------------------------------*/
  55. int main(void) {
  56.  
  57.     char buffer[ BUFFER_SIZE ];
  58.     uint8_t m, temperatureData[8], servoPosition[4], relayStatus[4], dimmerStatus[4],
  59.             temperatureLength, servoLength;
  60.  
  61.     /*
  62.      * Initialize optoencoders and buttons
  63.      */
  64.     /* Set as input and enable pull-up */ // TODO fixa rätt ingångar
  65.     DDRD &= ~( (1<<DDD6)|(1<<DDD7) );
  66.     PORTD |= (1<<PD6)|(1<<PD7);
  67.     /* Enable IO-pin interrupt */
  68.     PCICR |= (1<<PCIE1);
  69.     /* Unmask PD6 and PD7 */
  70.     PCMSK2 |= (1<<PCINT22)|(1<<PCINT23);
  71.  
  72.  
  73.     Timebase_Init();
  74. #if DEBUG
  75.     Serial_Init();
  76. #endif
  77.     sei();
  78.     lcd_init( LCD_DISP_ON );
  79.     lcd_clrscr();
  80.     lcd_puts("CAN LCD test\n");
  81.     lcd_puts("CanInit... ");
  82.     if (Can_Init() != CAN_OK) {
  83.         lcd_puts("FAILED!\n");
  84.     }else{
  85.         lcd_puts("OK!\n");
  86.     }
  87.  
  88.     uint32_t timeStamp = 0;
  89.  
  90.     Can_Message_t txMsg;
  91.     Can_Message_t rxMsg;
  92.     txMsg.Id = 0;
  93.     txMsg.RemoteFlag = 0;
  94.     txMsg.ExtendedFlag = 1;
  95.  
  96.     lcd_clrscr();
  97.     printLCDview( MAIN_VIEW );
  98.  
  99.     /* main loop */
  100.     while (1) {
  101.         /* service the CAN routines */
  102.         Can_Service();
  103.  
  104.         /* check if any messages have been received and print to uart */
  105.         while (Can_Receive(&rxMsg) == CAN_OK) {
  106. #if DEBUG
  107.             printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
  108.             printf("data={ ");
  109.  
  110.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  111.                 printf("%x ", rxMsg.Data.bytes[i]);
  112.             }
  113.             printf("}\n");
  114. #endif
  115.             /* Get data from bus and store */
  116.             if( rxMsg.Id == 0x1502 ){
  117.                 /* Temperature sensor data */
  118.                 for(m=0; m<rxMsg.DataLength; m++){
  119.                     temperatureData[m] = rxMsg.Data.bytes[m];
  120.                     temperatureLength = rxMsg.DataLength;
  121.                 }
  122.             }else if( rxMsg.Id == 0x1201 ){
  123.                 /* Relay status (ON/OFF) */
  124.                 relayStatus[1] = rxMsg.Data.bytes[2];
  125.             }else if( rxMsg.Id == 0x1301){
  126.                 /* Servo and blinds status */
  127.                 for( m=3; m<rxMsg.DataLength; m++ ){
  128.                     servoPosition[m-3] = rxMsg.Data.bytes[m];
  129.                     servoLength=rxMsg.DataLength-3;
  130.                 }
  131.             }
  132.             /* TODO Add extra messages here */
  133.  
  134.             /* Incoming command to change view */
  135.             if(rxMsg.Id == LCD_CMD_GET){
  136.                 if(rxMsg.Data.bytes[0]== VIEW_LEFT){
  137.                     if(currentView>MAIN_VIEW){
  138.                         currentView--;
  139.                     }
  140.                 }else if(rxMsg.Data.bytes[0]== VIEW_RIGHT){
  141.                     if(currentView<SERVO_VIEW){ /* TODO increase value if you add views */
  142.                         currentView++;
  143.                     }
  144.                 }
  145.  
  146.                 /* Print views skeleton and if existing its data */
  147.                 printLCDview( currentView );
  148.                 if(currentView == TEMPSENS_VIEW){
  149.                     printLCDviewData( TEMPSENS_VIEW, temperatureData, temperatureLength);
  150.                 }else if(currentView == SERVO_VIEW){
  151.                     printLCDviewData( SERVO_VIEW, servoPosition, servoLength);
  152.                 }
  153.             }
  154.         }
  155.  
  156.         // TODO använd optoencoders för att växla view
  157.         // också med fjärrkontroll
  158.  
  159.     }
  160.  
  161.  
  162. }
  163.