Author: remi
Date: 2008-12-13 20:19:55 +0100 (Sat, 13 Dec 2008)
New Revision: 3090
Modified:
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/TuxDriver.py
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/__init__.py
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/version.py
Log:
* updated encoding format.
* updated comments
* updated version
Modified:
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/TuxDriver.py
===================================================================
---
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/TuxDriver.py
2008-12-13 17:40:21 UTC (rev 3089)
+++
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/TuxDriver.py
2008-12-13 19:19:55 UTC (rev 3090)
@@ -1,4 +1,4 @@
-# -*- coding: latin1 -*-
+# -*- coding: utf-8 -*-
import version
__author__ = version.author
@@ -7,14 +7,15 @@
__licence__ = version.licence
del version
-# Copyright (C) 2008 C2ME Sa
-# R�mi Jocaille <[email protected]>
+# Copyleft (C) 2008 C2ME Sa
+# Remi Jocaille <[email protected]>
# Distributed under the terms of the GNU General Public License
# http://www.gnu.org/copyleft/gpl.html
from ctypes import *
import os
+# Error codes list
E_TUXDRV_BEGIN = 256
E_TUXDRV_NOERROR = 0
E_TUXDRV_PARSERISDISABLED = E_TUXDRV_BEGIN
@@ -28,6 +29,7 @@
E_TUXDRV_BUSY = E_TUXDRV_BEGIN + 8
E_TUXDRV_WAVSIZEEXCEDED = E_TUXDRV_BEGIN + 9
+# Identifiers list of the statuses
SW_ID_FLIPPERS_POSITION = 0
SW_ID_FLIPPERS_REMAINING_MVM = 1
SW_ID_SPINNING_DIRECTION = 2
@@ -70,6 +72,7 @@
SW_ID_SPIN_RIGHT_MOTOR_ON = 39
SW_ID_FLASH_SOUND_COUNT = 40
+# Names list of the statuses
SW_NAME_DRIVER = [
"flippers_position",
"flippers_remaining_movements",
@@ -114,21 +117,39 @@
"sound_flash_count"
]
+# Log levels
LOG_LEVEL_DEBUG = 0
LOG_LEVEL_INFO = 1
LOG_LEVEL_WARNING = 2
LOG_LEVEL_ERROR = 3
LOG_LEVEL_NONE = 4
+# Log targets
LOG_TARGET_TUX = 0
LOG_TARGET_SHELL = 1
+# Callback type defines
TUX_DRIVER_STATUS_CALLBACK = CFUNCTYPE(None, c_char_p)
TUX_DRIVER_SIMPLE_CALLBACK = CFUNCTYPE(None)
+#
==============================================================================
+# Public class
+#
==============================================================================
+
+#
------------------------------------------------------------------------------
+# libtuxdriver wrapper class.
+#
------------------------------------------------------------------------------
class TuxDrv(object):
+ """Tuxdriver wrapper class.
+ """
+ #
--------------------------------------------------------------------------
+ # Constructor.
+ #
--------------------------------------------------------------------------
def __init__(self, library_path = None):
+ """Constructor.
+ @param library_path: Path of libtuxdriver library. (Default None)
+ """
if library_path == None:
mPath, mFile = os.path.split(__file__)
if os.name == 'nt':
@@ -142,265 +163,392 @@
self.tux_driver_lib = CDLL(library_path)
except:
self.tux_driver_lib = None
- print "Path (%s) not found ! Tuxdriver is not handled in your
application." % library_path
+ txt = "Path (%s) not found ! " % library_path
+ print txt + "Tuxdriver is not handled in your application."
else:
- print "Path (%s) not found ! Tuxdriver is not handled in your
application." % library_path
-
+ txt = "Path (%s) not found ! " % library_path
+ print txt + "Tuxdriver is not handled in your application."
+
+ #
--------------------------------------------------------------------------
+ # Register a callback function to the "status" event.
+ #
--------------------------------------------------------------------------
def SetStatusCallback(self, funct = None):
+ """Register a callback function to the "status" event.
+ @param funct: Pointer to the function.
+ """
if self.tux_driver_lib == None:
return
-
+
if funct == None:
return
-
+
cb = TUX_DRIVER_STATUS_CALLBACK(funct)
self.__callback_container.append(cb)
self.tux_driver_lib.TuxDrv_SetStatusCallback(cb)
return
-
+
+ #
--------------------------------------------------------------------------
+ # Register a callback function to the "end of cycle" event.
+ #
--------------------------------------------------------------------------
def SetEndCycleCallback(self, funct = None):
+ """Register a callback function to the "end of cycle" event.
+ @param funct: Pointer to the function.
+ """
if self.tux_driver_lib == None:
return
-
+
if funct == None:
return
-
+
cb = TUX_DRIVER_SIMPLE_CALLBACK(funct)
self.__callback_container.append(cb)
self.tux_driver_lib.TuxDrv_SetEndCycleCallback(cb)
return
-
+
+ #
--------------------------------------------------------------------------
+ # Register a callback function to the "dongle connected" event.
+ #
--------------------------------------------------------------------------
def SetDongleConnectedCallback(self, funct = None):
+ """Register a callback function to the "dongle connected" event.
+ @param funct: Pointer to the function.
+ """
if self.tux_driver_lib == None:
return
-
+
if funct == None:
return
-
+
cb = TUX_DRIVER_SIMPLE_CALLBACK(funct)
self.__callback_container.append(cb)
self.tux_driver_lib.TuxDrv_SetDongleConnectedCallback(cb)
return
-
+
+ #
--------------------------------------------------------------------------
+ # Register a callback function to the "dongle disconnected" event.
+ #
--------------------------------------------------------------------------
def SetDongleDisconnectedCallback(self, funct = None):
+ """Register a callback function to the "dongle disconnected" event.
+ @param funct: Pointer to the function.
+ """
if self.tux_driver_lib == None:
return
-
+
if funct == None:
return
-
+
cb = TUX_DRIVER_SIMPLE_CALLBACK(funct)
self.__callback_container.append(cb)
self.tux_driver_lib.TuxDrv_SetDongleDisconnectedCallback(cb)
return
-
+
+ #
--------------------------------------------------------------------------
+ # Start libtuxdriver.
+ #
--------------------------------------------------------------------------
def Start(self):
+ """Start libtuxdriver.
+ """
if self.tux_driver_lib == None:
return
-
+
self.tux_driver_lib.TuxDrv_Start()
-
- def Stop(self):
+
+ #
--------------------------------------------------------------------------
+ # Stop libtuxdriver.
+ #
--------------------------------------------------------------------------
+ def Stop(self):
+ """Stop libtuxdriver.
+ """
if self.tux_driver_lib == None:
return
-
+
self.tux_driver_lib.TuxDrv_Stop()
-
+ #
--------------------------------------------------------------------------
+ # Perform a command through libtuxdriver.
+ #
--------------------------------------------------------------------------
def PerformCommand(self, delay, command):
+ """Perform a command through libtuxdriver.
+ @param delay: Delay before the command execution.
+ @param command: Command to execute.
+ @return: The success of the command performing.
+ """
if self.tux_driver_lib == None:
return E_TUXDRV_PARSERISDISABLED
-
- ret = self.tux_driver_lib.TuxDrv_PerformCommand(c_double(delay),
+
+ ret = self.tux_driver_lib.TuxDrv_PerformCommand(c_double(delay),
c_char_p(command))
-
+
return ret
-
+
+ #
--------------------------------------------------------------------------
+ # Perform the content of a macro commands file through libtuxdriver.
+ #
--------------------------------------------------------------------------
def PerformMacroFile(self, file_path = ""):
+ """Perform the content of a macro commands file through libtuxdriver.
+ @param file_path: Macro commands file path.
+ @return: The success of the performing.
+ """
if self.tux_driver_lib == None:
return E_TUXDRV_PARSERISDISABLED
-
+
ret = self.tux_driver_lib.TuxDrv_PerformMacroFile(c_char_p(file_path))
-
+
return ret
-
+
+ #
--------------------------------------------------------------------------
+ # Perform a macro commands through libtuxdriver.
+ #
--------------------------------------------------------------------------
def PerformMacroText(self, macro = ""):
+ """Perform a macro commands through libtuxdriver.
+ @param macro: Macro commands text.
+ @return: The success of the performing.
+ """
if self.tux_driver_lib == None:
return E_TUXDRV_PARSERISDISABLED
-
+
ret = self.tux_driver_lib.TuxDrv_PerformMacroText(c_char_p(macro))
-
+
return ret
-
+
+ #
--------------------------------------------------------------------------
+ # Clear the stack of delayed commands.
+ #
--------------------------------------------------------------------------
def ClearCommandStack(self):
+ """Clear the stack of delayed commands.
+ """
if self.tux_driver_lib == None:
return
-
+
self.tux_driver_lib.TuxDrv_ClearCommandStack()
-
+
return
-
+
+ #
--------------------------------------------------------------------------
+ # Write a tracks list in the sound flash memory.
+ #
--------------------------------------------------------------------------
def SoundReflash(self, tracks = ""):
+ """Write a tracks list in the sound flash memory.
+ @param tracks: Tracks list.
+ @return: The success of the writing.
+ """
if self.tux_driver_lib == None:
return E_TUXDRV_BUSY
-
+
ret = self.tux_driver_lib.TuxDrv_SoundReflash(c_char_p(tracks))
-
+
return ret
-
+
+ #
--------------------------------------------------------------------------
+ # Get the identifier of a status.
+ #
--------------------------------------------------------------------------
def GetStatusId(self, name = "battery_level"):
+ """Get the identifier of a status.
+ @param name: Status name.
+ @return: The identifier as Integer.
+ """
if self.tux_driver_lib == None:
return -1
-
+
idc = c_int(0)
idcp = pointer(idc)
ret = self.tux_driver_lib.TuxDrv_GetStatusId(c_char_p(name), idcp)
-
+
if ret != E_TUXDRV_NOERROR:
idc.value = -1
-
+
return idc.value
-
+
+ #
--------------------------------------------------------------------------
+ # Get the name of a status.
+ #
--------------------------------------------------------------------------
def GetStatusName(self, id = 0):
+ """Get the name of a status.
+ @param id: Identifier of the status.
+ @return: The name of the status or "UNDEFINED".
+ """
if self.tux_driver_lib == None:
return "UNDEFINED"
-
+
result = " " * 256
- ret = self.tux_driver_lib.TuxDrv_GetStatusName(c_int(id),
+ ret = self.tux_driver_lib.TuxDrv_GetStatusName(c_int(id),
c_char_p(result))
result = result.replace(" ", "")
-
+
if ret == E_TUXDRV_NOERROR:
return result
else:
return "UNDEFINED"
-
+
+ #
--------------------------------------------------------------------------
+ # Set the level of the internal logger of the libtuxdriver library.
+ #
--------------------------------------------------------------------------
def SetLogLevel(self, level = LOG_LEVEL_INFO):
+ """Set the level of the internal logger of the libtuxdriver library.
+ @param level: (LOG_LEVEL_DEBUG|LOG_LEVEL_INFO|LOG_LEVEL_WARNING
+ LOG_LEVEL_ERROR|LOG_LEVEL_NONE)
+ """
if self.tux_driver_lib == None:
return
-
+
self.tux_driver_lib.TuxDrv_SetLogLevel(c_uint8(level))
-
+
+ #
--------------------------------------------------------------------------
+ # Set the target of the internal logger of the libtuxdriver library.
+ #
--------------------------------------------------------------------------
def SetLogTarget(self, target = LOG_TARGET_SHELL):
+ """Set the target of the internal logger of the libtuxdriver library.
+ @param target: (LOG_TARGET_TUX|LOG_TARGET_SHELL)
+ """
if self.tux_driver_lib == None:
return
-
+
self.tux_driver_lib.TuxDrv_SetLogTarget(c_uint8(target))
-
+
+ #
--------------------------------------------------------------------------
+ # Get the state of a status.
+ #
--------------------------------------------------------------------------
def GetStatusState(self, id = 0):
+ """Get the state of a status.
+ @param id: Identifier of the status.
+ @return: The state of the status or "UNDEFINED".
+ """
if self.tux_driver_lib == None:
return "UNDEFINED"
-
+
result = " " * 256
- ret = self.tux_driver_lib.TuxDrv_GetStatusState(c_int(id),
+ ret = self.tux_driver_lib.TuxDrv_GetStatusState(c_int(id),
c_char_p(result))
result = result.replace(" ", "")
-
+
if ret == E_TUXDRV_NOERROR:
return result
else:
return "UNDEFINED"
-
+
+ #
--------------------------------------------------------------------------
+ # Get the value of a status.
+ #
--------------------------------------------------------------------------
def GetStatusValue(self, id = 0):
+ """Get the value of a status.
+ @param id: Identifier of the status.
+ @return: The value of the status or "UNDEFINED".
+ """
if self.tux_driver_lib == None:
return "UNDEFINED"
-
+
result = " " * 256
- ret = self.tux_driver_lib.TuxDrv_GetStatusValue(c_int(id),
+ ret = self.tux_driver_lib.TuxDrv_GetStatusValue(c_int(id),
c_char_p(result))
result = result.replace(" ", "")
-
+
if ret == E_TUXDRV_NOERROR:
return result
else:
return "UNDEFINED"
-
+
+ #
--------------------------------------------------------------------------
+ # Get the state of all statuses.
+ #
--------------------------------------------------------------------------
def GetAllStatusState(self):
+ """Get the state of all statuses.
+ @return: The state of all statuses.
+ """
if self.tux_driver_lib == None:
return ""
-
+
result = " " * 8182
self.tux_driver_lib.TuxDrv_GetAllStatusState(c_char_p(result))
result = result.replace(" ", "")
-
+
return result
-
+
+ #
--------------------------------------------------------------------------
+ # Tokenize the state of a status.
+ #
--------------------------------------------------------------------------
def TokenizeStatus(self, status = ""):
+ """Tokenize the state of a status.
+ @param status: The state of the status.
+ @return: A tokens list.
+ """
if self.tux_driver_lib == None:
return []
-
+
result = status.split(":")
if len(result) == 1:
if result[0] == '':
result = []
return result
-
+
+ #
--------------------------------------------------------------------------
+ # Reset the motor positions of Tux.
+ #
--------------------------------------------------------------------------
def ResetPositions(self):
+ """Reset the motor positions of Tux.
+ """
if self.tux_driver_lib == None:
return
-
+
self.tux_driver_lib.TuxDrv_ResetPositions()
-
+
return
-
+
+ #
--------------------------------------------------------------------------
+ # Reset the dongle state.
+ #
--------------------------------------------------------------------------
def ResetDongle(self):
+ """Reset the dongle state.
+ """
if self.tux_driver_lib == None:
return
-
+
self.tux_driver_lib.TuxDrv_ResetDongle()
-
+
return
-
+
+ #
--------------------------------------------------------------------------
+ # Get the structure of a status.
+ #
--------------------------------------------------------------------------
def GetStatusStruct(self, status = ""):
+ """Get the structure of a status.
+ @param status: The state of the status.
+ @return: The structure of a status.
+ """
result = {
'name' : "None",
'value' : None,
'delay' : 0.0,
'type' : 'string'
}
-
+
if self.tux_driver_lib == None:
return result
-
+
status_s = self.TokenizeStatus(status)
if len(status_s) == 0:
return result
-
+
result['name'] = status_s[0]
result['delay'] = status_s[3]
result['type'] = status_s[1]
-
+
if status_s[1] in ['uint8', 'int8', 'int', 'float', 'bool']:
result['value'] = eval(status_s[2])
elif status_s[1] == 'string':
result['value'] = status_s[2]
-
+
return result
-
+
+ #
--------------------------------------------------------------------------
+ # Get the explanation of an error code.
+ #
--------------------------------------------------------------------------
def StrError(self, error_code):
+ """Get the explanation of an error code.
+ @param error_code: Error code.
+ @return: The explanation as String.
+ """
if self.tux_driver_lib == None:
return "Shared library not found"
-
+
result = self.tux_driver_lib.TuxDrv_StrError(c_int(error_code))
-
+
return c_char_p(result).value
-
-if __name__ == "__main__":
-
- def on_status_event(status):
- status_struct = tux_drv.GetStatusStruct(status)
- print status_struct
-
- def on_dongle_connected():
- tux_drv.ResetPositions()
- print tux_drv.GetAllStatusState()
- print tux_drv.GetStatusName(0)
- print tux_drv.GetStatusValue(0)
- print tux_drv.GetStatusState(0)
-
- tux_drv = TuxDrv()
-
- tux_drv.SetLogLevel(LOG_LEVEL_INFO)
- tux_drv.SetStatusCallback(on_status_event)
- tux_drv.SetDongleConnectedCallback(on_dongle_connected)
- tux_drv.Start()
Modified:
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/__init__.py
===================================================================
---
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/__init__.py
2008-12-13 17:40:21 UTC (rev 3089)
+++
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/__init__.py
2008-12-13 19:19:55 UTC (rev 3090)
@@ -1,4 +1,4 @@
-# -*- coding: latin1 -*-
+# -*- coding: utf-8 -*-
"""
LibTuxDriver
@@ -17,8 +17,8 @@
__licence__ = version.licence
del version
-# Copyright (C) 2008 C2ME Sa
-# R�mi Jocaille <[email protected]>
+# Copyleft (C) 2008 C2ME Sa
+# Remi Jocaille <[email protected]>
# Distributed under the terms of the GNU General Public License
# http://www.gnu.org/copyleft/gpl.html
Modified:
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/version.py
===================================================================
---
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/version.py
2008-12-13 17:40:21 UTC (rev 3089)
+++
software_suite_v2/tuxware/pytuxisalive/branches/complete_refactoring/src/tuxisalive/lib/driver/version.py
2008-12-13 19:19:55 UTC (rev 3090)
@@ -1,19 +1,19 @@
-# -*- coding: latin1 -*-
+# -*- coding: utf-8 -*-
"""Version data for LibTuxDriver"""
__author__ = "Remi Jocaille ([email protected])"
-# Copyright (C) 2008 C2ME Sa
-# R�mi Jocaille <[email protected]>
+# Copyleft (C) 2008 C2ME Sa
+# Remi Jocaille <[email protected]>
# Distributed under the terms of the GNU General Public License
# http://www.gnu.org/copyleft/gpl.html
name = 'tuxisalive.lib.driver'
-version = '0.0.3'
+version = '0.0.4'
author = "Remi Jocaille ([email protected])"
-description = "Wrapper to libtuxdriver shared library."
+description = "Wrapper of libtuxdriver."
licence = "GPL"
-date = "May 2008"
\ No newline at end of file
+date = "December 2008"
\ No newline at end of file
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn