Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. ############################################################################
  2. #    Copyright (C) 2008 by Mattias Runge   #
  3. #    mattias@runge.se   #
  4. #                                                                          #
  5. #    This program is free software; you can redistribute it and#or modify  #
  6. #    it under the terms of the GNU General Public License as published by  #
  7. #    the Free Software Foundation; either version 2 of the License, or     #
  8. #    (at your option) any later version.                                   #
  9. #                                                                          #
  10. #    This program is distributed in the hope that it will be useful,       #
  11. #    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
  12. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
  13. #    GNU General Public License for more details.                          #
  14. #                                                                          #
  15. #    You should have received a copy of the GNU General Public License     #
  16. #    along with this program; if not, write to the                         #
  17. #    Free Software Foundation, Inc.,                                       #
  18. #    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
  19. ############################################################################
  20.  
  21. import ServiceBase
  22. import BackendCan
  23. import CanModuleTypes
  24.  
  25. class Service (ServiceBase.ServiceBase):
  26.     Width = 20
  27.     Height = 4
  28.     Backlight = 255
  29.    
  30.     def __init__(self, id):
  31.         ServiceBase.ServiceBase.__init__(self, id, "LCD")
  32.        
  33.         self.events = {"onGetSize":[], "onGetBacklight":[]}
  34.  
  35.         self.canBackend = BackendCan.GetBackendCan()
  36.         self.canBackend.AddCallback(self.HandleMsg)
  37.        
  38.     def SetOnline(self):
  39.         if ServiceBase.ServiceBase.SetOnline(self):
  40.             self.RequestSize()
  41.             self.Clear()
  42.             self.SetBacklight(self.Backlight)
  43.        
  44.     def HandleMsg(self, canMsg):
  45.         if not self.Online:
  46.             return False
  47.  
  48.         try:
  49.             if self.ModuleType == canMsg.ModuleType and self.ModuleId == canMsg.ModuleId:
  50.                 if canMsg.Command == 4: # Size
  51.                     if len(canMsg.Data) >= 2:
  52.                         self.Width = canMsg.Data[0]
  53.                         self.Height = canMsg.Data[1]
  54.                         self.Log("Size received: " + str(self.Width) + "x" + str(self.Height))
  55.                         self.CallEvent("onGetSize", 0)
  56.                     else:
  57.                         self.Log("Malformed data received")
  58.                 elif canMsg.Command == 5: # Backlight
  59.                     if len(canMsg.Data) >= 1:
  60.                         self.Backlight = canMsg.Data[0]
  61.                         self.Log("Backlight level received: " + str(self.Backlight))
  62.                         self.CallEvent("onGetBacklight", 0)
  63.                     else:
  64.                         self.Log("Malformed data received")
  65.                 else:
  66.                     self.Log("Unknown command " + str(canMsg.Command))
  67.         except KeyError:
  68.             pass
  69.    
  70.     def SetCursorPosition(self, x, y):
  71.         if not self.Online:
  72.             return False
  73.  
  74.         canMsg = BackendCan.CanMessage()
  75.         canMsg.ClassName = 12;
  76.         canMsg.ModuleType = self.ModuleType
  77.         canMsg.ModuleId = self.ModuleId
  78.         canMsg.Direction = 0
  79.         canMsg.Command = 2
  80.        
  81.         canMsg.Data.append(x)
  82.         canMsg.Data.append(y)
  83.        
  84.         self.canBackend.QueueMsg(canMsg.Compile())
  85.    
  86.     def Clear(self):
  87.         if not self.Online:
  88.             return False
  89.  
  90.         canMsg = BackendCan.CanMessage()
  91.         canMsg.ClassName = 12;
  92.         canMsg.ModuleType = self.ModuleType
  93.         canMsg.ModuleId = self.ModuleId
  94.         canMsg.Direction = 0
  95.         canMsg.Command = 1
  96.        
  97.         self.canBackend.QueueMsg(canMsg.Compile())
  98.    
  99.     def Print(self, text):
  100.         if not self.Online:
  101.             return False
  102.  
  103.         canMsg = BackendCan.CanMessage()
  104.         canMsg.ClassName = 12;
  105.         canMsg.ModuleType = self.ModuleType
  106.         canMsg.ModuleId = self.ModuleId
  107.         canMsg.Direction = 0
  108.         canMsg.Command = 3
  109.        
  110.         for char in text:
  111.             canMsg.Data.append(ord(char))
  112.            
  113.             if len(canMsg.Data) == 8:
  114.                 self.canBackend.QueueMsg(canMsg.Compile())
  115.                 del canMsg.Data
  116.                 canMsg.Data = []
  117.                
  118.         if len(canMsg.Data) > 1:
  119.             self.canBackend.QueueMsg(canMsg.Compile())
  120.    
  121.     def SetBacklight(self, level):
  122.         if not self.Online:
  123.             return False
  124.  
  125.         canMsg = BackendCan.CanMessage()
  126.         canMsg.ClassName = 12;
  127.         canMsg.ModuleType = self.ModuleType
  128.         canMsg.ModuleId = self.ModuleId
  129.         canMsg.Direction = 0
  130.         canMsg.Command = 5
  131.    
  132.         canMsg.Data.append(level)
  133.        
  134.         self.canBackend.QueueMsg(canMsg.Compile())
  135.  
  136.         self.Backlight = level
  137.    
  138.     def RequestSize(self):
  139.         if not self.Online:
  140.             return False
  141.  
  142.         canMsg = BackendCan.CanMessage()
  143.         canMsg.ClassName = 12;
  144.         canMsg.ModuleType = self.ModuleType
  145.         canMsg.ModuleId = self.ModuleId
  146.         canMsg.Direction = 0
  147.         canMsg.Command = 4
  148.        
  149.         self.canBackend.QueueMsg(canMsg.Compile())
  150.        
  151.     def RequestBacklight(self):
  152.         if not self.Online:
  153.             return False
  154.  
  155.         canMsg = BackendCan.CanMessage()
  156.         canMsg.ClassName = 12;
  157.         canMsg.ModuleType = self.ModuleType
  158.         canMsg.ModuleId = self.ModuleId
  159.         canMsg.Direction = 0
  160.         canMsg.Command = 5
  161.  
  162.         self.canBackend.QueueMsg(canMsg.Compile())
  163.  
  164.