summaryrefslogtreecommitdiff
path: root/lc_examples/memory.lua
blob: cf38121dc991048b467019496b60c954de2e2731 (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
--Digilines Memory Example

--Connect an EEPROM or SRAM chip on channel "memory"
--Enter "help" in the terminal for a command list

local function runcmd(command)
	if command ~= "" then
		print(" "..command,true)
		print()
	end
	if string.sub(command,1,4) == "read" then
		local address = string.sub(command,6,7)
		if string.sub(address,2,2) == " " then address = string.sub(address,1,1) end
		address = tonumber(address)
		if (not address) or address > 31 or address < 0 or math.floor(address) ~= address then
			print("Invalid address - address must be an integer from 0 to 31")
		else
			digiline_send(mem.channel,{command = "read",address = address})
			mem.readsuccess = false
			interrupt(1,"readtimeout")
			return --Suppress printing a new prompt
		end
	elseif string.sub(command,1,5) == "write" then
		local address = string.sub(command,7,8)
		local data = string.sub(command,10,-1)
		if string.sub(address,2,2) == " " then
			address = string.sub(command,7,7)
			data = string.sub(command,9,-1)
		end
		address = tonumber(address)
		if (not address) or address > 31 or address <0 or math.floor(address) ~= address then
			print("Invalid address - address must be an integer from 0 to 31")
		elseif data == "" then
			print("No data specified")
		else
			print(string.format("Wrote to address %d",address))
			digiline_send(mem.channel,{command = "write",address = address,data = data})
		end
	elseif string.sub(command,1,7) == "channel" then
		local channel = string.sub(command,9,-1)
		if channel and channel ~= "" then
			mem.channel = channel
			print("Channel changed")
		else
			print("No channel specified")
		end
	elseif command == "clear" then
		clearterm()
		print(">")
		return --Suppress printing a new prompt - it was done already to omit the blank line
	elseif command == "help" then
		print("Available commands:")
		print("channel <channel>: Changes the digilines channel the memory device is attached to, default is \"memory\"")
		print("clear: Clears the screen")
		print("read <address>: Reads from the specified address and displays the received data")
		print("write <address> <data>: Writes the specified data to the specified address")
		print("help: Shows this help message")
	elseif command == "" then
		--Do nothing
	else
		print("Unknown command - use \"help\" to see a list of valid commands.")
	end
	print()
	print(">")
end

if event.type == "program" then
	mem.channel = "memory"
	runcmd("clear")
elseif event.iid == "readtimeout" and not mem.readsuccess then
	print("No response received. Is there a memory device connected on the channel \""..mem.channel.."\"?")
	print()
	print(">")
elseif event.channel == mem.channel then
	mem.readsuccess = true
	print(event.msg)
	print()
	print(">")
elseif event.type == "terminal" then
	runcmd(event.text)
end