Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

#!/usr/bin/env python3 

# -*- coding: utf-8 -*- 

from typing import Dict, Any 

import socket 

 

 

class LuckyClient: 

"""Allows to remote control the LuckyLoop 

 

args 

---- 

 

host: str 

defaults to 127.0.0.1, but should be set in production to the ip-adress of the LuckyLoop device 

port: int 

defaults to 1219 because LuckyServer has the acronym LS and L is 12th character in alphabet and S is the 19th. 

 

.. note:: 

 

For safety reasons, parameters like target frequency and phase are not set during initialization of the client, as this could interrupt an ongoing process on LuckyLoop when an user tries to reconnect. All parameters have be set explicitly after initialization. 

 

 

Example 

------- 

 

.. code-block:: python 

 

luckyloop = LuckyClient() 

luckyloop.frequency = 20 sets the target frequency to 20 

luckyloop.phase = 2 # sets the target phase to 2 

luckyloop.trigger() # triggers immediatly 

 

""" 

 

buffer_size = 8192 

 

def __init__(self, host: str = "127.0.0.1", port: int = 1219): 

self.host = host 

self.port = port 

self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

self.enquire() 

 

@property 

def phase(self) -> int: 

"get the current phase set on LuckyLoop" 

status = self.enquire() 

return status["target_phase"] 

 

@phase.setter 

def phase(self, target_phase: int): 

"""set the target_phase on LuckyLoop 

 

args 

---- 

target_phase 

an integer between 0 and 360°  

""" 

if target_phase < 0 or target_phase > 360: 

raise ValueError("Target phase should be between 0 and 360") 

if type(target_phase) != int: 

raise ValueError("Target phase must be an integer") 

 

condition = { 

"threshold": "=", 

"target_phase": target_phase, 

"target_frequency": "=", 

"trigger": "=", 

} 

self.set_condition(condition) 

 

@property 

def threshold(self) -> int: 

"get the current threshold set on LuckyLoop" 

status = self.enquire() 

return status["threshold"] 

 

@threshold.setter 

def threshold(self, threshold: int): 

"""set the target_phase on LuckyLoop 

 

args 

---- 

target_phase 

an integer between 0 and 360°  

""" 

if type(threshold) != int or threshold < 0: 

raise ValueError("threshold must be an integer larger or equal to 0") 

 

condition = { 

"threshold": threshold, 

"target_phase": "=", 

"target_frequency": "=", 

"trigger": "=", 

} 

self.set_condition(condition) 

 

@property 

def frequency(self) -> int: 

"get the current frequency set on LuckyLoop" 

status = self.enquire() 

return status["target_frequency"] 

 

@frequency.setter 

def frequency(self, target_frequency: int): 

"""set the target_frequency on LuckyLoop 

 

LuckyLoop calculates phase and amplitude continually for a specific frequency. Change this target_frequency 

 

args 

---- 

target_frequency 

an integer of at least 1 Hz 

""" 

if target_frequency <= 0: 

raise ValueError("Target frequency must be higher than 0 Hz") 

if type(target_frequency) != int: 

raise ValueError("Target frequency must be an integer") 

 

condition = { 

"threshold": "=", 

"target_phase": "=", 

"target_frequency": target_frequency, 

"trigger": "=", 

} 

self.set_condition(condition) 

 

def trigger(self): 

"""trigger LuckyLoop 

 

Immediatly pulls the specified TRIGOUT to HIGH. LuckyLoop handles from then on, and by default it pulls it LOW after one cycle. 

 

""" 

 

condition = { 

"threshold": "=", 

"target_phase": "=", 

"target_frequency": "=", 

"trigger": 1, 

} 

return self.set_condition(condition) 

 

def set_condition(self, condition: Dict[str, Any]) -> Dict[str, Any]: 

return self.query(self.encode(condition)) 

 

def shutdown(self): 

"shuts LuckyLoop down gracefully" 

message = "[shutdown:]\0".encode("ascii") 

self.query(message) 

 

def enquire(self) -> Dict[str, int]: 

"enquire about the current status and version of LuckyLoop" 

message = "[send_status:]\0".encode("ascii") 

return self.query(message) 

 

def query(self, message: bytes) -> Dict[str, Any]: 

"send and receive a message to LuckyLoop" 

self.connect() 

self.socket.sendall(message) 

status = self.socket.recv(self.buffer_size) 

self.close() 

return self.decode(status) 

 

def connect(self): 

"connect with LuckyLoop" 

self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

try: 

self.socket.connect((self.host, self.port)) 

self.socket.settimeout(1) 

except ConnectionRefusedError: 

raise ConnectionError( 

"It is not possible to reach LuckyLoop. Restart the device and make sure that IP and port are correct." 

) 

 

def close(self): 

"close the connection to LuckyLoop" 

# self.socket.shutdown(1) 

self.socket.close() 

 

@staticmethod 

def encode(condition: Dict[str, Any]) -> bytearray: 

"serialize a status dictionary for sending" 

message = f"[set_condition:{condition['threshold']},{condition['target_phase']},{condition['target_frequency']},{condition['trigger']}]".encode( 

"ascii" 

) 

return message 

 

@staticmethod 

def decode(status: bytearray) -> Dict[str, int]: 

"deserialize a message from LuckyServer and return it as a dictionary" 

items = status.decode("ascii").replace(":", "").strip().split(" ") 

cond = dict() 

for k, v in zip(items[0::2], items[1::2]): 

cond[k.lower()] = int(v) 

return cond