Fun with Electronics and Code

Just another WordPress.com site

Code Complete at last!!!

Well after a busy last week and a half, I finally finished the last chunk of code and implemented all the methods it needs and found the time to do a Blog post. This is the method in my application that is used to Process the serial Commands coming from the Computer the Netduino is connected to. So far the application is 1700 lines of code. Thanks to the guys at Netduino Forums (Specially Oz and AlfredBr), that saved me having to write ~400 lines of code for the Nokia LCD and did a great job.

I also have finished my second Fritzing diagram that shows how the whole box including the connections to the Ethernet connects will work. Then I started wiring them up. Will start to solder them tomorrow,  post a bunch of pictures, and start really testing things.

       static void ProcessSerialCommand(string Command)
        {

            /* Commands to Process:
             * "R" - reset/ready
             * "F" - force elapsed times to be returned immediately for any lane that has finished before race is completed.
             *       Unfinished lanes are returned as MaxTime plus .9 up to the TimerResolution. Default is 9.9999
             * "A" - return elapsed times again (if needed.) Normally the controller sends the race results after all four
             *       lanes are finished, or time out, or are otherwise accounted for (when one or more lanes are masked.)
             * "C" - check track status. Depending on what checks were selected at compile time, either finish line
             *       phototransistors, or both finish phototransistors and start gate status are checked. If a finish
             *       line phototransistor is blocked it returns "NG" to the computer (unless it was masked.) If the start
             *       gate it down, it returns "NG" to the computer.
             * "T" - trigger the start gate to drop. NOTE only works if survo is connected
             * "M1" - (or M2...M8) to mask an individual lane.
             * "X" - unmask all lanes.
             */

            if (Command.Length == 1)
            {
                // Process for a Single Digit command listed above
                switch (Command)
                {
                    case "R":
                        // reset the device and prepare to race. stop the timer if nessary
                        Reset();
                        LCD.Clear();
                        LCD.WriteText("Serial command" +
                                      "Recieved      " +
                                      "              " +
                                      "RESET complete" +
                                      "READY TO RACE ");
                        break;
                    case "F":
                        SentRaceTimes();
                        break;
                    case "A":
                        SentRaceTimes();
                        break;
                    case "C":
                        // do nothing for now
                        break;
                    case "T":
                        ServoTheStarGate();
                        break;
                    case "X":
                        Lane_1_Enabled = true;
                        Lane_2_Enabled = true;
                        Lane_3_Enabled = true;
                        Lane_4_Enabled = true;
                        Lane_5_Enabled = true;
                        Lane_6_Enabled = true;
                        Lane_7_Enabled = true;
                        Lane_8_Enabled = true;
                        break;
                    default:
                        // you should not be here, print error on LCD
                        Debug.Print(Command);
                        break;
                }
            }
            else if (Command.Length == 2)
            {
                /* Process the Command for lane masking */
                switch (Command)
                {
                    // because the command length was 2 characters we assume it was a lane masking request. If it was not then we ignore the command but let the user know we recieved it.
                    case "M1":
                        Lane_1_Enabled = false;
                        break;
                    case "M2":
                        Lane_2_Enabled = false;
                        break;
                    case "M3":
                        Lane_3_Enabled = false;
                        break;
                    case "M4":
                        Lane_4_Enabled = false;
                        break;
                    case "M5":
                        Lane_5_Enabled = false;
                        break;
                    case "M6":
                        Lane_6_Enabled = false;
                        break;
                    case "M7":
                        Lane_7_Enabled = false;
                        break;
                    case "M8":
                        Lane_8_Enabled = false;
                        break;
                    default:
                        // you should not be here, print error on LCD
                        Debug.Print(Command);
                        LCD.Clear();
                        LCD.WriteText("Serial command" +
                                      "Recieved      " +
                                      "UNKNOWN CMD   " +
                                      "length = 2    " +
                                      "|" + Command + "|");
                        break;
                }
            }
            else
            {
                // houston... we have a problem ... but we are going to ignore it
                // you should not be here, print error on LCD
                Debug.Print(Command);
                LCD.WriteText("Serial command" +
                              "Unknown L=" + Command.Length.ToString()  +
                              "|" + Command + "|");

            }

        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: