summaryrefslogtreecommitdiff
path: root/maximus/python/py/script_example.py
blob: aa0c643fd59500e079946d669d8b4613d2a0603c (plain)
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
#! usr/bin/env python

print "\n*** " + __file__ + " ***\n"

import sys
sys.path.append('./test')
sys.path.append('../test')
import startup


# SCRIPT EXAMPLE
# To be run with "stationtest.elf" station executable
# (to be compiled under "/trunk/maximus/stationtest").

from interface import *
from struct import pack, unpack

def my_cb (msg):
	print "=>",my_cb.func_name
	if msg.is_param("result_2"):
		result2 = unpack('q',msg.bind_param("result_2"))
		print "result 2 =",result2[0]
	receive_fc1_rsp = True

# Instantiate a Maximus object and initialize it.
maximus = Maximus()
maximus.init(sys.argv)

# Create three stations.
stationA = maximus.create_sta()
stationB = maximus.create_sta()
stationC = maximus.create_sta()

# Launch a debugger attached to station B.
stationB.debug()

# Send an asynchronous function message to station A.
#

# Create a function message and set the name of the function to call of station A.
fc1 = maximus.create_fcall("function_1")

# Add  a first parameter to the created message.
param1 = pack('iiB',1,2,True)
fc1.add_param("param_1", param1)

# Add a second parameter to the created message.
param2 = pack('iiiiiiiiii',0,1,2,3,4,5,6,7,8,9)
fc1.add_param("param_2", param2)

# Add a third parameter to the created message.
param3 = "hello"
fc1.add_param("param_3", param3)

# Add a fourth parameter to the created message.
param4 = pack('q',123)
fc1.add_param("param_4", param4)

# Register a callback function which will be called at message response reception.
fc1.set_cb(my_cb)

# Set destination station of message 1.
fc1.set_sta(stationA)

# Send the configured message in an asynchronous mode.
receive_fc1_rsp = False
fc1.send_async()

# Send a synchronous function message to station B.

# Create a function message and set the name of the function to call into station B.
fc2 = maximus.create_fcall("function_2")

# Add a first parameter to the created message.
fc2.add_param_bool("param_5", True)

# Send the configured message to station B in a synchronous mode.
fc2.send(stationB)

# Get the function result.
result1 = fc2.bind_param_string("result_1")
print "result1 =",result1

# Set a parameter value of station B.
probe1 = maximus.create_probe()
param6 = 789
probe1.add_param_ulong("param_6", param6)
probe1.send(stationB)

# Get a parameter value from station B.
probe2 = maximus.create_probe().add_param("param_7")
probe2.send(stationB)
param7 = probe2.bind_param_ulong("param_7")
print "param 7 =",param7

# Get the same parameter value from station C, re-using existing message.
probe2.send(stationC)

# Send asynchronous function message(s) to station B.
if 456 == param7:
	fc3 = maximus.create_fcall("function_3")
	fc3.send_async(stationC)
fc4 = maximus.create_fcall("function_4")
fc4.send_async(stationC)

# Send an asynchronous function message to station C.
fc5 = maximus.create_fcall("function_5")
fc5.send_async(stationC)

# Wait for responses to all sent messages in asynchronous mode.
maximus.wait()

# Get list of all registered parameters of stations C.
probe3 = maximus.create_probe()
probe3.send(stationC)
if probe3.is_param("param_8"):
	# Get parameter 8 value and set parameter 9 value of station C.
	probe4 = maximus.create_probe()
	probe4.add_param("param_8")
	probe4.add_param_bool("param_9", False)
	probe4.send(stationC)
	param8 = probe4.bind_param_bool("param_8")
	print "param 8 =",param8

# Wait during 1000000 ticks before creating another station.
maximus.wait(1000000)

# Create a fourth station.
stationD = maximus.create_sta()

# Wait during 1000000 ticks before terminating the program.
maximus.wait(1000000)

# Remove stations.
stationA.remove()
stationB.remove()
stationC.remove()
stationD.remove()

print "\n*** END ***\n"