api.py
153 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
|
r51 | ''' | ||
Created on Dec 2, 2014 | ||||
@author: Miguel Urco | ||||
eth_device decorator is used to implement an api to ethernet devices. | ||||
When eth_device decorator is used it adds two parameters to any function (ip and port) | ||||
#Definition | ||||
@eth_device | ||||
def enable_rf() | ||||
cmd = "xxxxx" | ||||
payload = "xxxxxx" | ||||
|
r172 | |||
|
r51 | return cmd, payload | ||
|
r172 | |||
|
r51 | #How to call this function: | ||
answer = enable_rf(ip, port) | ||||
''' | ||||
|
r172 | from devices.dds import data | ||
|
r51 | |||
from devices.jro_device import eth_device, IdClass | ||||
ID_CLASS = IdClass["dds"] | ||||
CMD_RESET =0X01 | ||||
CMD_ENABLE =0X02 | ||||
CMD_CHANGEIP =0X03 | ||||
CMD_STATUS =0X04 | ||||
CMD_ECHO =0XFE | ||||
DDS_CMD_RESET =0X10 | ||||
DDS_CMD_ENABLE_RF =0x11 | ||||
# DDS_CMD_MULTIPLIER =0X12 | ||||
# DDS_CMD_MODE =0x13 | ||||
# DDS_CMD_FREQUENCY_A =0X14 | ||||
# DDS_CMD_FREQUENCY_B =0x15 | ||||
# DDS_CMD_PHASE_A =0X16 | ||||
# DDS_CMD_PHASE_B =0x17 | ||||
# DDS_CMD_AMPLITUDE_1 =0X19 #Se han invertido la posicion de los canales | ||||
# DDS_CMD_AMPLITUDE_2 =0x18 #en el PCB | ||||
DDS_CMD_WRITE =0x50 | ||||
DDS_CMD_READ =0x8000 | ||||
@eth_device(ID_CLASS) | ||||
def reset(): | ||||
|
r172 | |||
|
r51 | cmd = CMD_RESET | ||
payload = "" | ||||
|
r172 | |||
|
r51 | return cmd, payload | ||
@eth_device(ID_CLASS) | ||||
def change_ip(ip, mask="255.255.255.0", gateway="0.0.0.0"): | ||||
|
r172 | |||
|
r51 | cmd = CMD_CHANGEIP | ||
payload = ip + '/' + mask + '/' + gateway | ||||
|
r172 | |||
|
r51 | return cmd, payload | ||
@eth_device(ID_CLASS) | ||||
def status(): | ||||
|
r172 | |||
|
r51 | cmd = CMD_STATUS | ||
payload = "" | ||||
|
r172 | |||
|
r51 | return cmd, payload | ||
@eth_device(ID_CLASS) | ||||
def echo(): | ||||
|
r172 | |||
|
r51 | cmd = CMD_ECHO | ||
payload = "" | ||||
|
r172 | |||
|
r51 | return cmd, payload | ||
@eth_device(ID_CLASS) | ||||
def enable_rf(): | ||||
|
r172 | |||
|
r51 | cmd = DDS_CMD_ENABLE_RF | ||
payload = chr(0x01) | ||||
|
r172 | |||
|
r51 | return cmd, payload | ||
@eth_device(ID_CLASS) | ||||
def disable_rf(): | ||||
|
r172 | |||
|
r51 | cmd = DDS_CMD_ENABLE_RF | ||
payload = chr(0x00) | ||||
|
r172 | |||
|
r51 | return cmd, payload | ||
@eth_device(ID_CLASS) | ||||
def read_all_device(): | ||||
|
r172 | |||
|
r51 | payload = "" | ||
|
r172 | |||
|
r51 | return DDS_CMD_READ, payload | ||
@eth_device(ID_CLASS) | ||||
def write_all_device(payload): | ||||
|
r172 | |||
|
r51 | return DDS_CMD_WRITE, payload | ||
def read_config(ip, port): | ||||
""" | ||||
Output: | ||||
parms : Dictionary with keys | ||||
multiplier : | ||||
frequencyA : | ||||
frequencyB : | ||||
frequencyA_Mhz : | ||||
frequencyB_Mhz : | ||||
modulation : | ||||
phaseA_degrees : | ||||
phaseB_degrees : | ||||
amplitudeI : | ||||
amplitudeQ : | ||||
|
r172 | |||
|
r51 | """ | ||
payload = read_all_device(ip, port) | ||||
|
r172 | |||
|
r51 | return data.dds_str_to_dict(payload) | ||
|
r172 | |||
|
r51 | def write_config(ip, port, parms): | ||
""" | ||||
Input: | ||||
ip : | ||||
port : | ||||
parms : Dictionary with keys | ||||
multiplier : 4 to 20 | ||||
frequencyA : 0 to (2**48-1) equivalent to: 0 - "Master clock" | ||||
frequencyB : 0 to (2**48-1) equivalent to: 0 - "Master clock" | ||||
modulation : 0 to 3 | ||||
phaseA_degrees : 0 - 360 degrees | ||||
phaseB_degrees : 0 - 360 degrees | ||||
amplitudeI : 0 to (2**12-1) equivalent to: 0 - 100% | ||||
amplitudeQ : 0 to (2**12-1) equivalent to: 0 - 100% | ||||
|
r172 | |||
|
r51 | """ | ||
|
r172 | |||
|
r51 | payload = data.dict_to_dds_str(parms) | ||
|
r172 | |||
|
r51 | answer = write_all_device(ip, port, payload) | ||
|
r172 | |||
|
r51 | return answer | ||
if __name__ == '__main__': | ||||
|
r172 | |||
print(read_config("127.0.0.1", 2000)) | ||||