8000 created a piano, however discovered concurrency issues with socket.so… · neumond/python-computer-craft@25abd72 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 25abd72

Browse files
committed
created a piano, however discovered concurrency issues with socket.socket(socket.AF_INET, socket.SOCK_STREAM).accept().recv()
1 parent 6c9f9da commit 25abd72

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

computercraft/back-debug.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ local function fetch_fn()
9292
r = attempt_post(url..'gettask/'..os.getComputerID()..'/', "")
9393
if (r == nil) then
9494
print('Connection broken')
95-
return
95+
--return
9696
else
9797
local cmd = r.readAll()
9898
print(cmd)

computercraft/commander.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ def send_command(self, cmd):
5151

5252
def run():
5353
sm: SessionManager = SessionManager()
54-
sm.start()
54+
sm.run()
5555
# server.main()
56-
piano: Piano = Piano()
57-
piano.run()
56+
# piano: Piano = Piano()
57+
# piano.run()
5858

5959

6060
if __name__ == "__main__":

computercraft/subapis/peripheral.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,20 @@ async def runCommand(self):
147147
return bool_success(await self._send('runCommand'))
148148

149149

150+
class CCTSpeaker(CCPeripheral):
151+
async def playNote(self, instrument: str, volume: int, pitch: int) -> bool:
152+
return bool_success(await self._send('playNote', instrument, volume, pitch))
153+
154+
155+
150156
TYPE_MAP = {
151157
'drive': CCDrive,
152158
'monitor': CCMonitor,
153159
'computer': CCComputer,
154160
'modem': CCModem,
155161
'printer': CCPrinter,
156162
'command': CCCommandBlock,
163+
'speaker': CCTSpeaker,
157164
}
158165

159166

examples/radio.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def connect(self):
2222
"test1"
2323
]
2424
})
25-
self.toServer({"type": "newScreen"})
2625

2726
def producer(self, msg):
2827
self.msg = msg

examples/servant.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,56 @@
11
import asyncio
2+
from caduceussocket.connection import Client
23

3-
from .unit import Unit
4-
from .radio import Radio
4+
5+
class Radio(Client):
6+
def __init__(self, api):
7+
super().__init__()
8+
self.api = api
9+
10+
self.msg = "null"
11+
12+
self.white_list_functions += [
13+
"note"
14+
]
15+
16+
def connect(self):
17+
super().connect()
18+
self.send_data({
19+
"type": "register",
20+
"args": [
21+
"piano",
22+
"2077"
23+
]
24+
})
25+
26+
def note(self, msg, state):
27+
if state:
28+
self.msg = msg
29+
else:
30+
self.msg = False
31+
32+
# def toServer(self, msg):
33+
# self.send_data({
34+
# "type": "broadcast",
35+
# "args": [
36+
# msg,
37+
# "server"
38+
# ]
39+
# })
540

641

742
async def servant(api):
43+
speaker = await api.peripheral.wrap("bottom")
44+
845
print("New unit registering")
946
await api.print("Connecting...")
1047
radio = Radio(api)
1148
print("New unit online")
1249

1350
while True:
14-
radio.loop()
15-
await api.print(radio.msg)
16-
await asyncio.sleep(1)
17-
18-
19-
51+
radio.loop() # fixme non async function socket.socket(socket.AF_INET, socket.SOCK_STREAM).accept().recv()
52+
msg = radio.msg
53+
if msg != False:
54+
note = msg -43
55+
await api.print(note)
56+
await speaker.playNote("guitar", 3, note)

0 commit comments

Comments
 (0)
0