Is this code ugly?
Jay Kickliter
Posts: 446
I wrote what I feel is my first piece of real code, and even though I stole ideas from other objects, it took my almost 2 hours to get this short method to work:
My problem is that although it works fine, that last repat loop looks funny to me. I've been staring at it for so long, I think I need someone else's opinion. Any suggestions are welcome.
Here the data I'm parsing:
I send "AT+CSQ" to the cell phone
It responds with:
"
+CSQ: 24,99
OK
"
What I'm interested in is the 24 in this example, as that corresponds to the signal strength.
PUB signalStrength bytefill(@commandBuffer, 0, 20) byteCounter := 0 _attention phone.str(@phone_signal_strength_command) repeat rxByte := phone.rx until rxByte == ":" repeat rxByte := phone.rx while rxByte == SPACE repeat if rxByte == "," commandBuffer[noparse][[/noparse]byteCounter++] := 0 else commandBuffer[noparse][[/noparse]byteCounter++] := rxByte rxByte := phone.rx until rxByte == "," return @commandBuffer
My problem is that although it works fine, that last repat loop looks funny to me. I've been staring at it for so long, I think I need someone else's opinion. Any suggestions are welcome.
Here the data I'm parsing:
I send "AT+CSQ" to the cell phone
It responds with:
"
+CSQ: 24,99
OK
"
What I'm interested in is the 24 in this example, as that corresponds to the signal strength.
Comments
Note that this doesn't check for invalid information, but then none of this code checks for correct format.
I was also wonder on weather I should rewrite these loops for all the phone commands, or make one central method to handle all commands.
At the beginning of your routine (when you want to start the timeout) put "startTime := cnt"
In your various loops, put:
When you call this routine, use "\" in front of the call (to "catch" the abort). Read the manual about the abort statement.
clkfreq/10 gives you a timeout of 100ms. Change this for some other time that would work better for you.
I tried two things:
and
The first one didn't work when I created an abort condition the loop, I never got the error message. The second one did, but since the '\' was inside the debug.dec(), debug.dec() got passed 0, which is not what I was looking for. I was looking for a way to kill the stack up to and including debug.str() (Which will be replaced with something more useful later).
I also ran into another problem. When I unplugged the phone, the abort never terminated the loop, I imaging because phone.rx was just sitting there waiting for a receive and never looped again to catch a byte. Is there no way to have an "external" abort?
Thanks again, Jay
You should also change the "abort" to "abort -1" so you have a unique return value on a timeout.
2) You can move your I/O to a separate cog and do a timeout on that. The easiest way to do this would be with FullDuplexSerial which already has a receive with timeout routine. Remember that the transmit routine returns when the character is put into the transmit buffer, not when the character is actually sent.