|
1 | 1 | # Import the basic framework components. |
2 | | -from softioc import softioc, builder |
| 2 | +from softioc import softioc, builder, asyncio_dispatcher |
3 | 3 | from aioca import caget, caput |
4 | 4 | import asyncio |
5 | 5 |
|
| 6 | +# Create an asyncio dispatcher, the event loop is now running |
| 7 | +dispatcher = asyncio_dispatcher.AsyncioDispatcher() |
| 8 | + |
6 | 9 | # Set the record prefix |
7 | 10 | builder.SetDeviceName("MY-DEVICE-PREFIX") |
8 | 11 |
|
9 | 12 | # Create some records |
10 | 13 | ai = builder.aIn('AI', initial_value=5) |
11 | | - |
12 | | -def update(val): |
13 | | - print("got here") |
14 | | - print(val) |
15 | | - ai.set(val) |
16 | | - |
17 | | -ao = builder.aOut('AO', initial_value=12.45, on_update=update) |
| 14 | +ao = builder.aOut('AO', initial_value=12.45, always_update=True, |
| 15 | + on_update=lambda v: ai.set(v)) |
18 | 16 |
|
19 | 17 | # Boilerplate get the IOC started |
20 | 18 | builder.LoadDatabase() |
21 | | -softioc.iocInit() |
22 | | - |
23 | | -# Perform some reading/writing to the PVs |
24 | | -async def do_read_write(): |
25 | | - print(await caget("MY-DEVICE-PREFIX:AO")) |
26 | | - print(await caget("MY-DEVICE-PREFIX:AI")) |
27 | | - await caput("MY-DEVICE-PREFIX:AO", "999") |
28 | | - await asyncio.sleep(10) |
29 | | - print(await caget("MY-DEVICE-PREFIX:AI")) |
30 | | - print(await caget("MY-DEVICE-PREFIX:AO")) |
31 | | - |
32 | | -#asyncio.run(do_read_write()) |
33 | | - |
34 | | -print(asyncio.run(caget("MY-DEVICE-PREFIX:AI"))) |
35 | | -print(asyncio.run(caget("MY-DEVICE-PREFIX:AO"))) |
36 | | -print(asyncio.run(caput("MY-DEVICE-PREFIX:AO","999"))) |
| 19 | +softioc.iocInit(dispatcher) |
37 | 20 |
|
38 | | -print(asyncio.run(caget("MY-DEVICE-PREFIX:AI"))) |
39 | | -print(asyncio.run(caget("MY-DEVICE-PREFIX:AO"))) |
| 21 | +# Start processes required to be run after iocInit |
| 22 | +async def update(): |
| 23 | + while True: |
| 24 | + ai.set(ai.get() + 1) |
| 25 | + await asyncio.sleep(1) |
40 | 26 |
|
| 27 | +asyncio.run_coroutine_threadsafe(update(), dispatcher.loop) |
41 | 28 |
|
| 29 | +# Finally leave the IOC running with an interactive shell. |
42 | 30 | softioc.interactive_ioc(globals()) |
0 commit comments