wifi babelfish released

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
razorrifh
Posts: 23
Joined: Sat Jun 04, 2005 2:15 am
Location: Penn State
Contact:

wifi babelfish released

Post by razorrifh »

hey, just thought someone might find this interesting. its a program that uses the wifi connection of the psp to translate text via babelfish.

it's very unpolished, but since i tend to program to figure out how to do stuff, i never actually finish many of my programs once i figure out how to get the main part working as it should :\ i like this one so i probably will, but who knows. plus it shows a good way to grab some text from a website.

here's my readme:
wifi babelfish proof-of-concept
coded by razorrifh
-------------------------------

i got bored about 20 minutes into installing the psp toolchain (it ended up taking 6 hours, 52 minutes) so i decided to code something up with lua. i saw the coder of palib (i'm assuming a nds programming library? dunno) wrote a language translation program based on a dictionary.

i liked the idea, but i thought it would be a bit better to grab the translation from somewhere online, because then you could have an already built translation system with support for multiple languages. instantly i thought of babelfish. i loaded it up in the browser and viewed the source to see what i could see, and lo and behold... well there was the source. i typed up a querry string based on the variable names and it worked. yay.

i hadn't messed with wifi in lua before, but that's how you learn right? the hardest part was the string processing because I HATE STRING MANIPULATION IT IS THE DEVIL INDEED. so yeah, but after i figured out how to scroll through the string character by character it was cake. (its not string.char(). its string.sub(), you just have to make the substring 1 character long :D)

so anyways, here's the file. to use it (only for firmware 1.5 i'm pretty sure) just throw it in your x:/PSP/GAME/ folder. it's called "wifi babelfish poc". run it (with the wifi switch on!) and it will show you a list of your wifi connections. this is just the proof-of-concept, so it will automatically choose the first connection, and then start trying to get an ip address from the access point. please note that it may freeze if the signal strength isn't strong enough, or if it looses connection. hold start to reset, and if that doesnt work, use the home button to do so and try again.

the screenshot function DOES NOT WORK. i dont know why. anyone wanna clue me in?

i plan on making this a full little app including text input, and file saving. if i dont get around to it, someone else may. just lemme peek at your source if you do :)

ps, we are... PENN STATE :)

RELEASE NOTES:

RELEASE 1
NOTES
- initial release - it does what it should do

TODO
- add a better gui -- check the photoshop file in the WLAN directory to see what i'm thinking about doing. have at it if you'd like to use it.
- add text input
- get the screenshot function to work
- work out some bugs :D

BUGS
- may freeze, not really my fault. its how lua handles wifi. i'll see what i can do about it, but i'm not too sure :\
- email your bugs to [email protected], thanks!

THANKS TO:
- the ps2dev comunity
- everyone who's contributed to the programming of lua player
- shine for the wifi sample
- paradox, because without them, i wouldnt even know what i do about game programming (dev.paradogs.com)
- and DARPA for putting out such a kick ass network :D without it i'd be watching tv instead of learning how to do such cool things. woot.

DONATIONS:
ok, so lots of devs ask for money or whatever for what they program. i know it takes time, and i'm certainly not rich (debt sucks :|) but if you like this enough to donate, here are some links to good causes to donate to. these are probably all in america, but just find one in your area and give :)

http://www.worldvision.org
http://www.unitedway.org
http://www.goodwill.org
http://www.thehungersite.com




NOTE TO MY GF:
love you lindsey marie! sorry i sit on the computer debugging when you want me to be holding you watching tv. i appreciate ya :)

my girlfriend > *
download for 1.5fw: here
download source (no lua player): here

and goto my website while you're at it. it gets lonely without visitors (pun intended, har)
Remember, for all your psp dev needs you could do worse than pspdev.org ;)
-John_K
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

Post by be2003 »

screenshot doesnt work because the path you save it in has to be relative to the current working directory...

dont do:

Code: Select all

screen:save("ms0:/translation.png")
do:

Code: Select all

screen:save("translation.png")
or
screen:save("../translation.png")
make sure the screen is flipped properly before saving the shot.

i also dont understand why you are creating a server socket, it isnt necessary when you are simply downloading.
- be2003
blog
razorrifh
Posts: 23
Joined: Sat Jun 04, 2005 2:15 am
Location: Penn State
Contact:

Post by razorrifh »

i probably left the code in there from the wlan sample. also, i'll try changing the screenshot to make sure it's flipped but i dont understand why that would stop it from working... wouldnt it just be 1 frame behind if it wasnt flipped?

edit: added a profuse amount of comments and removed some superfluous code. it's functionally the same.

Code: Select all

-----------------------------------------
-- wifi babelfish proof of concept     --
-- by razorrifh                        --
-----------------------------------------
-- based on the wifi sample code that  --
-- comes with lua player .20           --
--(code by shine i think)              --
-----------------------------------------

-- activate usb mode
System.usbDiskModeActivate()

-- define the color white
white = Color.new(255, 255, 255)

-- define an image that we will draw to (so we're not drawing of the front or back buffer)
offscreen = Image.createEmpty(480, 272)
offscreen:clear(Color.new(0, 0, 0))

-- these are the x and y values for the text
y = 0
x = 0

-- this is the string that we will translate from (original text)
translatethis = "i love my psp!"

-- this variable contains the two languages that the transaction will take place on. right now it will go from english to french (en_fr)
to_from = "en_fr"

-- these are the variables that i needed to use to get the text parsing function to work.
newstr = ""
drewtext = 0
endofstr = 0

-- this loads the images
logo = Image.load("babelfish.png")
hackersgonewild = Image.load("hackersgonewild.png")



-- function to remove spaces and replace them with the "+" sign so it can form a valid url. accepts string input.
function toHTML(text)
        tempstr = ""

	for i = 1, string.len(text) do
		char = string.sub(text, i, i)
		if char == " " then
			tempstr = tempstr .. "+"
		else
			tempstr = tempstr .. char
		end
	end

	return tempstr
end


-- this code (by shine) prints the text passed to it to the screen and parses \n and \r characters
function graphicsPrint(text)
	for i = 1, string.len(text) do
		char = string.sub(text, i, i)
		if char == "\n" then
			y = y + 8
			x = 0
		elseif char ~= "\r" then
			offscreen:print(x, y, char, white)
			x = x + 8
		end
	end
	screen:blit(0, 0, offscreen)
	screen:blit(280, 0, logo)
        screen:blit(0, 258, hackersgonewild)
	screen.waitVblankStart()
	screen.flip()
end

-- this function (by shine) prints the text and adds a newline to the end. accepts string input.
function graphicsPrintln(text)
	graphicsPrint(text .. "\n")
end

-- init WLAN and choose connection config
Wlan.init()

-- loads the connections into the configs variable. i'm pretty sure this is a table.
configs = Wlan.getConnectionConfigs()

-- displays output to let the user know whats goin on
graphicsPrintln("available connections:")
graphicsPrintln("")

-- cycles through the configs table to print out the list of connections
for key, value in ipairs(configs) do
	graphicsPrintln(key .. ": " .. value)
end

-- this prints a new line and lets the user know we're going to use the first connection found
graphicsPrintln("")
graphicsPrintln("using first connection...")

-- sets up the wireless system to use the first connection found
Wlan.useConnectionConfig(1)

-- start connection and wait until it is connected
graphicsPrintln("waiting for WLAN init and determining IP address...")

-- this loop gets the ip addy from the access point. this is one of the main reasons the program seems to freeze. if it doesnt get an ip
-- it will just keep sleeping. not sure how to fix this one...
while true do
        -- define a var to be the ip address from the ap
	ipAddress = Wlan.getIPAddress()
        
        -- i tried to get this to break out of the loop if start was pressed but it doesnt seem to work :(
	if Controls.read():start() then break end

        -- if the ipaddress is found, then break out of the loop
	if ipAddress then break end
	
	-- if the ipaddress isnt found, then sleep for a tenth of a second and re-run the loop
	System.sleep(100)
end

-- let the user know we got an ip and what it is
graphicsPrintln("the PSP IP address is: " .. ipAddress)
graphicsPrintln("")

-- prompt the user to let them know whats going on
graphicsPrintln("connecting to babelfish (babelfish.altavista.com)")

-- create the socket to babelfish on port 80 (the error variable is returned if there is an error. i should probably check this somewhere, huh...
socket, error = Socket.connect("babelfish.altavista.com", 80)

-- this loop waits for the socked to connect, otherwise it will sleep for a tenth of a second and restart
while not socket:isConnected() do System.sleep(100) end

-- more prompting.
graphicsPrintln("connected to " .. tostring(socket))

-- send request to the remote server
graphicsPrintln("downloading translation...")

-- we're already connected to babelfish.altavista.com, so now we need to querry the translation script
bytesSent = socket:send("GET /tr?Submit=Translate&doit=done&int1=1&tt=urltext&lp=" .. to_from .. "&trtext=" .. toHTML(translatethis) .." HTTP/1.0\r\n")

-- i suppose this is part of the http protocol, i'll learn more about this later
bytesSent = socket:send("host: babelfish.altavista.com\r\n\r\n")

-- read and display result
requestCount = 0
header = ""
headerFinished = false
while true do
	-- read from the babelfish script
	buffer = socket:recv()
	
	-- if we've received bytes...
	if string.len(buffer) > 0 then

                -- not sure what this does :|
		if headerFinished then
			graphicsPrint(buffer)
		else
                        -- keep adding more to the buffer if more is coming in
			header = header .. buffer
			
			-- this is the 'magic' code that always appears before the translated text so we search for this in the text
			startIndex, endIndex = string.find&#40;header, "<td bgcolor=white class=s><div style=padding&#58;10px;>"&#41;
			
			-- if it's found then...
			if endIndex then
                                -- this defines the second character in the string. for some reason there is always a ">" character in the first position. i think its just the way string.find works
                                findstrlen = 2

                                -- do this loop until you've reached the end of the string
                                while endofstr == 0 do
                                
                                      -- if this character == '<' then...
                                      if string.sub&#40;string.sub&#40;header, endIndex&#41;, findstrlen, findstrlen&#41; == "<" then
                                           -- we've reached the end of the string
                                           endofstr = 1
                                      else
                                           -- add the character to our buffer and...
                                           newstr = newstr .. string.sub&#40;string.sub&#40;header, endIndex&#41;, findstrlen, findstrlen&#41;

                                           -- increment the character position
                                           findstrlen = findstrlen + 1
                                      end
                                 end
			end
		end
	end
	
	-- check to see if the select button has been pushed
        if Controls.read&#40;&#41;&#58;select&#40;&#41; then
           screen&#58;save&#40;"translation.png"&#41;
        end
        
        -- check to see if the start button has been pushed
	if Controls.read&#40;&#41;&#58;start&#40;&#41; then break end
	
	-- prompts the user to to let them know the translated text, and to let them know what to push
	-- the drewtext variable is needed because we only want to draw this once, and this is in a loop
	-- so we wait until we havent drawn the text &#40;drewtext == 0&#41; and we've reached the end of the
	-- string &#40;endofstr == 1&#41;
        if drewtext == 0 and endofstr == 1 then
           graphicsPrint&#40;"\n\n\n\n\n\n\n"&#41;
           graphicsPrint&#40;"The original text is&#58; " .. translatethis .. "\n"&#41;
           graphicsPrint&#40;"Your translated text is&#58; " .. newstr .. "\n"&#41;
           graphicsPrint&#40;"\nPress START to restart, or SELECT for a screenshot."&#41;
           drewtext = 1
        end
	screen.waitVblankStart&#40;&#41;
end
           -- close the networking code. this is just for completeness i'm pretty sure. i cant see the psp firmware not handling this if it's left open.
           Wlan.term&#40;&#41;
Remember, for all your psp dev needs you could do worse than pspdev.org ;)
-John_K
Post Reply