From 4e72fd697fe05010370e705d29fcdaf8cb77b59f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sat, 4 Jun 2011 08:11:35 +0200 Subject: rewrite pyk8055/libk8055.i --- pyk8055/libk8055.i | 389 +++++++++++++++++++++-------------------------------- 1 file changed, 157 insertions(+), 232 deletions(-) diff --git a/pyk8055/libk8055.i b/pyk8055/libk8055.i index b0d31e9..b6ecd70 100644 --- a/pyk8055/libk8055.i +++ b/pyk8055/libk8055.i @@ -27,81 +27,53 @@ %module pyk8055 %include "typemaps.i" -/* for the ReadAllAnalog and ReadAllValues function */ -%apply long *OUTPUT { long int *data1, long int *data2, long int *data3, long int *data4, long int *data5 }; %inline %{ -/* For direct access to the DEBUG variable and global IO strings */ -extern int DEBUG; -extern unsigned char data_in[9]; -extern unsigned char data_out[9]; -/* - Extended functions for debugging the IO -*/ - -char strval[30]; - -/* returns 16byte hex string - 8 bytes */ -char * RawInput() - { - int i; - for(i=0; i< 8; i++) { - sprintf((char *)&strval[i*2],"%02X",data_in[i]); - } - return (char *)strval; - } - -/* More print friendly format */ -char * DumpInput() - { - int i; - for(i=0; i< 8; i++) { - sprintf((char *)&strval[i*3],"%02X:",data_in[i]); - } - strval[23] = '\0'; - return (char *)strval; - } - -char * DumpOutput() - { - int i; - for(i=0; i< 8; i++) { - sprintf((char *)&strval[i*3],"%02X:",data_out[i]); - } - strval[23] = '\0'; - return (char *)strval; - } %} %{ -extern int OpenDevice (long int BoardAddress); -extern int CloseDevice (void); -extern long int ReadAnalogChannel (long int Channelno); -extern int ReadAllAnalog (long int *data1, long int *data2); -extern int OutputAnalogChannel (long int Channel, long int data); -extern int OutputAllAnalog (long int data1, long int data2); -extern int ClearAllAnalog (void); -extern int ClearAnalogChannel (long int Channel); -extern int SetAnalogChannel (long int Channel); -extern int SetAllAnalog (void); -extern int WriteAllDigital (long int data); -extern int ClearDigitalChannel (long int Channel); -extern int ClearAllDigital (void); -extern int SetDigitalChannel (long int Channel); -extern int SetAllDigital (void); -extern int ReadDigitalChannel (long int Channel); -extern long int ReadAllDigital (void); -extern int ResetCounter (long int counternr); -extern long int ReadCounter (long int CounterNo); -extern int SetCounterDebounceTime (long int CounterNo, long int DebounceTime); -extern int ReadAllValues (long int *data1, long int *data2, long int *data3, long int *data4, long int *data5); -extern int SetAllValues(int digitaldata, int addata1, int addata2); -extern int SetCounterDebounceTime(long CounterNo, long DebounceTime); -extern long SetCurrentDevice(long deviceno); -extern long SearchDevices(void); -extern char *Version(void); - +extern struct k8055_dev* k8055_alloc( void ); +extern void k8055_free( struct k8055_dev* dev ); +/* +extern int k8055_read( struct k8055_dev* dev ); +extern int k8055_write( struct k8055_dev* dev ); +extern int k8055_digital_1( struct k8055_dev* dev ); +extern int k8055_digital_2( struct k8055_dev* dev ); +extern int k8055_digital_3( struct k8055_dev* dev ); +extern int k8055_digital_4( struct k8055_dev* dev ); +extern int k8055_digital_5( struct k8055_dev* dev ); +extern int k8055_analog_1( struct k8055_dev* dev ); +extern int k8055_analog_2( struct k8055_dev* dev ); +extern int k8055_counter_1( struct k8055_dev* dev ); +extern int k8055_counter_2( struct k8055_dev* dev ); +*/ +/*extern char* k8055_version( void );*/ +extern void k8055_set_debug_on( void ); +extern void k8055_set_debug_off( void ); +extern int k8055_search_devices( void ); +extern int k8055_open_device( struct k8055_dev* dev, int board_address ); +extern int k8055_close_device( struct k8055_dev* dev ); +extern int k8055_set_analog_channel( struct k8055_dev* dev, int channel ); +extern int k8055_clear_analog_channel( struct k8055_dev* dev, int channel ); +extern int k8055_read_analog_channel( struct k8055_dev* dev, int channel ); +extern int k8055_write_analog_channel( struct k8055_dev* dev ,int channel, int data ); +extern int k8055_set_all_analog( struct k8055_dev* dev ); +extern int k8055_clear_all_analog( struct k8055_dev* dev ); +extern int k8055_read_all_analog( struct k8055_dev* dev, int* data1, int* data2 ); +extern int k8055_write_all_analog( struct k8055_dev* dev, int data1, int data2 ); +extern int k8055_set_digital_channel( struct k8055_dev* dev, int channel ); +extern int k8055_clear_digital_channel( struct k8055_dev* dev, int channel ); +extern int k8055_read_digital_channel( struct k8055_dev* dev, int channel ); +extern int k8055_set_all_digital( struct k8055_dev* dev ); +extern int k8055_clear_all_digital( struct k8055_dev* dev ); +extern int k8055_read_all_digital( struct k8055_dev* dev ); +/*extern int k8055_write_all_digital( struct k8055_dev* dev, int data );*/ +extern int k8055_set_all_values( struct k8055_dev* dev, int d_data, int a_data1, int a_data2 ); +extern int k8055_read_all_values( struct k8055_dev* dev, int* data1, int* data2, int* data3, int* data4, int* data5 ); +extern int k8055_reset_counter( struct k8055_dev* dev, int counter ); +extern int k8055_read_counter( struct k8055_dev* dev, int counter ); +extern int k8055_set_counter_debounce_time( struct k8055_dev* dev, int counter, int debounce_time ); %} /* @@ -110,7 +82,6 @@ And here we create the class interface to the library */ %pythoncode %{ K8055_ERROR = -1 -_K8055_CLOSED = -1 class k8055: "Class interface to the libk8055 library" @@ -122,35 +93,29 @@ class k8055: k=k8055(1,True) # connect to board 0 and enable debugging """ - self.Buttons = 0 - self.dev = _K8055_CLOSED - self.Address = BoardAddress + self.dev = _pyk8055.k8055_alloc() # TODO how to free ?? if debug == False: self.DebugOff() else: self.DebugOn() if BoardAddress != None: - try: - self.dev = self.OpenDevice(BoardAddress) - # print "Open OK " + str(self.dev) - except: - self.dev = _K8055_CLOSED - raise IOError, "Could not open device" - # print "Open error" - - def __str__(self): - """String format (almost) as from K8055 program""" - if self.__opentest() == True: # Device open - all = self.ReadAllValues() - return str(all[1])+";"+str(all[2])+";"+str(all[3])+";"+str(all[4])+";"+str(all[5]) - else: - return "" + self.OpenDevice(BoardAddress) - def __opentest(self): - if self.dev == _K8055_CLOSED: - return False - else: - return True + def __finalize__(self): + """Destructor""" + _pyk8055.k8055_free(self.dev) + + #def Version(self): + # return _pyk8055.k8055_version(); + + def DebugOn(self): + return _pyk8055.k8055_set_debug_on(); + + def DebugOff(self): + return _pyk8055.k8055_set_debug_off(); + + def SearchDevices(self): + return _pyk8055.k8055_search_devices() def OpenDevice(self,BoardAddress): """Open the connection to K8055 @@ -161,14 +126,12 @@ class k8055: except IOError: ... returns 0 if OK, - Throws IOError if invalid board or not accessible + Throws IOError if invalid board or not accessible """ - if self.__opentest() == False: # Not open yet - self.dev = _pyk8055.OpenDevice(BoardAddress) - if self.dev == K8055_ERROR: - raise IOError, "Could not open device" - # print "Open OK " + str(self.dev) - return self.dev + if _pyk8055.open_device(self.dev,BoardAddress) == K8055_ERROR: + raise IOError, "Could not open device" + # print "Open OK " + str(self.dev) + return 0 def CloseDevice(self): """Close the connection to K8055 @@ -177,84 +140,62 @@ class k8055: retuns 0 if OK else -1 """ - if self.dev != _K8055_CLOSED: - ret = _pyk8055.CloseDevice() - self.dev = _K8055_CLOSED - return ret + return _pyk8055.k8055_close_device(self.dev) - def OutputAnalogChannel(self,Channel,value=0): - """Set analog output channel value, default 0 (0-255)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.OutputAnalogChannel(Channel,value) + def SetAnalogChannel(self,Channel): + """Set analog output channel value to 255""" + return _pyk8055.k8055_set_analog_channel(self.dev,Channel) + + def ClearAnalogChannel(self,Channel): + """Set analog output channel value to 0""" + return _pyk8055.k8055_clear_analog_channel(self.dev,Channel) def ReadAnalogChannel(self,Channel): """Read data from analog input channel (1/2)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ReadAnalogChannel(Channel) + return _pyk8055.k8055_read_analog_channel(self.dev,Channel) + + def WriteAnalogChannel(self,Channel,value=0): + """Set analog output channel value, default 0 (0-255)""" + return _pyk8055.k8055_write_analog_channel(self.dev,Channel,value) + + def SetAllAnalog(self): + """Set both analog output channels at once to 255""" + return _pyk8055.k8055.set_all_analog(self.dev) + + def ClearAllAnalog(self): + """Set both analog output channels at once to 0""" + return _pyk8055.k8055.clear_all_analog(self.dev) def ReadAllAnalog(self): """Read data from both analog input channels at once Returns list, [return-value,channel_data1, channel_data2] - """ - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ReadAllAnalog() + return _pyk8055.k8055_read_all_analog(self.dev) - def OutputAllAnalog(self,data1,data2): + def WriteAllAnalog(self,data1,data2): """Set both analog output channels at once (0-255,0-255)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.OutputAllAnalog(data1,data2) - - def ClearAllAnalog(self): - """Set all (both) analog output channels to 0 (low)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ClearAllAnalog() - - def ClearAnalogChannel(self,Channel): - """Set analog output channel (1/2)to 0 (low)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ClearAnalogChannel(Channel) - - def SetAnalogChannel(self,Channel): - """Set analog output channel (1/2) to 0xFF (high)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.SetAnalogChannel(Channel) - - def SetAllAnalog(self): - """Set all (both) analog output channels to 0xFF (high)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.SetAllAnalog() + return _pyk8055.k8055_write_all_analog(self.dev,data1,data2) - def WriteAllDigital(self,data): - """Write digital output channel bitmask (0-255)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.WriteAllDigital(data) + def SetDigitalChannel(self,Channel): + """Set digital output channel (1-8) high (1)""" + return _pyk8055.k8055_set_digital_channel(self.dev,Channel) def ClearDigitalChannel(self,Channel): - """Clear digital output channel (1-8)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ClearDigitalChannel(Channel) + """Set digital output channel (1-8) low (0)""" + return _pyk8055.k8055_clear_digital_channel(self.dev,Channel) - def ClearAllDigital(self): - """Set all digital output channels low (0)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ClearAllDigital() - - def SetDigitalChannel(self,Channel): - """Set digital output channel (1-8)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.SetDigitalChannel(Channel) + def ReadDigitalChannel(self,Channel): + """Read digital input channel (1-5), returns 0/1 (-1 on error)""" + return _pyk8055.k8055_read_digital_channel(self.dev,Channel) def SetAllDigital(self): """Set all digital output channels high (1)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.SetAllDigital() + return _pyk8055.k8055_set_all_digital(self.dev) - def ReadDigitalChannel(self,Channel): - """Read digital input channel (1-5), returns 0/1 (-1 on error)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ReadDigitalChannel(Channel) + def ClearAllDigital(self): + """Set all digital output channels low (0)""" + return _pyk8055.k8055_clear_all_digital(self.dev) def ReadAllDigital(self): """Read all digital input channels - bitmask @@ -263,39 +204,15 @@ class k8055: retuns -1 on error """ - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ReadAllDigital() + return _pyk8055.k8055_read_all_digital(self.dev) - def ResetCounter(self,CounterNo): - """Reset input counter (1/2), input channel 1/2""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ResetCounter(CounterNo) + #def WriteAllDigital(self,data): + # """Write digital output channel bitmask (0-255)""" + # return _pyk8055.k8055_write_all_digital(self.dev,data) - def ReadCounter(self,CounterNo): - """Read input counter (1/2), input channel 1/2""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ReadCounter(CounterNo) - - def SetCounterDebounceTime(self,CounterNo, DebounceTime): - """Set counter debounce time on counter 1/2 (1-7450 ms)""" - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.SetCounterDebounceTime(CounterNo,DebounceTime) - - # Makes no sense to switch to another class here - def SetCurrentDevice(self): - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.SetCurrentDevice(self.Address) - - # This function makes no sense in this context as - # the device is already open - #def SearchDevices(self): - #return _pyk8055.SearchDevices() - - def DeviceAddress(self): - return self.Address - - def IsOpen(self): - return self.__opentest() + def SetAllValues(self,ddata, adata1, adata2): + """Write digital output channel bitmask (0-255) and both analog channels data (0-255,0-255)""" + return _pyk8055.k8055_set_all_values(self.dev,ddata, adata1, adata2) def ReadAllValues(self): """Read data from all input channels at once @@ -303,50 +220,58 @@ class k8055: Returns list, [return-value,digital input data, analog channel_data1, analog channel_data2, counter1, counter2] """ - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.ReadAllValues() - def SetAllValues(self,digitaldata, addata1, addata2): - if self.__opentest() == False : return K8055_ERROR - return _pyk8055.SetAllValues(digitaldata, addata1, addata2) - - def Version(self): - return _pyk8055.Version(); + return _pyk8055.k8055_read_all_values(self.dev) - def DebugOn(self): - _pyk8055.cvar.DEBUG = 1 - def DebugOff(self): - _pyk8055.cvar.DEBUG = 0 + def ResetCounter(self,CounterNo): + """Reset input counter (1/2), input channel 1/2""" + return _pyk8055.k8055_reset_Counter(self.dev,CounterNo) - def RawInput(self): - return _pyk8055.RawInput() - def DumpInput(self): - return "In <-"+_pyk8055.DumpInput() - def DumpOutput(self): - return "Out->"+_pyk8055.DumpOutput() + def ReadCounter(self,CounterNo): + """Read input counter (1/2), input channel 1/2""" + return _pyk8055.k8055_read_counter(self.dev,CounterNo) + def SetCounterDebounceTime(self,CounterNo, DebounceTime): + """Set counter debounce time on counter 1/2 (1-7450 ms)""" + return _pyk8055.k8055_set_counter_debounce_time(self.dev,CounterNo,DebounceTime) %} -extern int OpenDevice (long int BoardAddress); -extern int CloseDevice (void); -extern long int ReadAnalogChannel (long int Channelno); -extern int ReadAllAnalog (long int *data1, long int *data2); -extern int OutputAnalogChannel (long int Channel, long int data); -extern int OutputAllAnalog (long int data1, long int data2); -extern int ClearAllAnalog (void); -extern int ClearAnalogChannel (long int Channel); -extern int SetAnalogChannel (long int Channel); -extern int SetAllAnalog (void); -extern int WriteAllDigital (long int data); -extern int ClearDigitalChannel (long int Channel); -extern int ClearAllDigital (void); -extern int SetDigitalChannel (long int Channel); -extern int SetAllDigital (void); -extern int ReadDigitalChannel (long int Channel); -extern long int ReadAllDigital (void); -extern int ResetCounter (long int counternr); -extern long int ReadCounter (long int CounterNo); -extern int SetCounterDebounceTime (long int CounterNo, long int DebounceTime); -extern int ReadAllValues (long int *data1, long int *data2, long int *data3, long int *data4, long int *data5); -extern int SetAllValues(int digitaldata, int addata1, int addata2); -extern long SetCurrentDevice(long deviceno); -extern long SearchDevices(void); -extern char *Version(void); +extern struct k8055_dev* k8055_alloc( void ); +extern void k8055_free( struct k8055_dev* dev ); +/* +extern int k8055_read( struct k8055_dev* dev ); +extern int k8055_write( struct k8055_dev* dev ); +extern int k8055_digital_1( struct k8055_dev* dev ); +extern int k8055_digital_2( struct k8055_dev* dev ); +extern int k8055_digital_3( struct k8055_dev* dev ); +extern int k8055_digital_4( struct k8055_dev* dev ); +extern int k8055_digital_5( struct k8055_dev* dev ); +extern int k8055_analog_1( struct k8055_dev* dev ); +extern int k8055_analog_2( struct k8055_dev* dev ); +extern int k8055_counter_1( struct k8055_dev* dev ); +extern int k8055_counter_2( struct k8055_dev* dev ); +*/ +/*extern char* k8055_version( void );*/ +extern void k8055_set_debug_on( void ); +extern void k8055_set_debug_off( void ); +extern int k8055_search_devices( void ); +extern int k8055_open_device( struct k8055_dev* dev, int board_address ); +extern int k8055_close_device( struct k8055_dev* dev ); +extern int k8055_set_analog_channel( struct k8055_dev* dev, int channel ); +extern int k8055_clear_analog_channel( struct k8055_dev* dev, int channel ); +extern int k8055_read_analog_channel( struct k8055_dev* dev, int channel ); +extern int k8055_write_analog_channel( struct k8055_dev* dev ,int channel, int data ); +extern int k8055_set_all_analog( struct k8055_dev* dev ); +extern int k8055_clear_all_analog( struct k8055_dev* dev ); +extern int k8055_read_all_analog( struct k8055_dev* dev, int* data1, int* data2 ); +extern int k8055_write_all_analog( struct k8055_dev* dev, int data1, int data2 ); +extern int k8055_set_digital_channel( struct k8055_dev* dev, int channel ); +extern int k8055_clear_digital_channel( struct k8055_dev* dev, int channel ); +extern int k8055_read_digital_channel( struct k8055_dev* dev, int channel ); +extern int k8055_set_all_digital( struct k8055_dev* dev ); +extern int k8055_clear_all_digital( struct k8055_dev* dev ); +extern int k8055_read_all_digital( struct k8055_dev* dev ); +/*extern int k8055_write_all_digital( struct k8055_dev* dev, int data );*/ +extern int k8055_set_all_values( struct k8055_dev* dev, int d_data, int a_data1, int a_data2 ); +extern int k8055_read_all_values( struct k8055_dev* dev, int* data1, int* data2, int* data3, int* data4, int* data5 ); +extern int k8055_reset_counter( struct k8055_dev* dev, int counter ); +extern int k8055_read_counter( struct k8055_dev* dev, int counter ); +extern int k8055_set_counter_debounce_time( struct k8055_dev* dev, int counter, int debounce_time ); -- cgit v1.1-2-g2b99