private void Looper_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { while (SerPort.BytesToRead > 0) { int cint = SerPort.ReadByte(); char c = Convert.ToChar(cint); str += c; if (c == '\n') { LogList.Items.Insert(0, str.Substring(0, str.Length - 2)); str = ""; } } } else { try { SerPort.Open(); LogList.Items.Insert(0, "Port Open"); } catch { LogList.Items.Insert(0, "Cannot Open Port"); } } }
private void ProcTimer_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { PortStatus.Text = "Opened"; //ProcessSend(); } else { try { SerPort.PortName = PortList.Items[PortList.SelectedIndex].ToString(); SerPort.Open(); Log("Port Opened"); } catch { PortStatus.Text = "Error Opening"; bootload_enabled = false; ButtonsSet(false, true, false); } } }
public Form1() { workdir = Directory.GetCurrentDirectory(); InitializeComponent(); flash_data = new short[65536]; bootload_enabled = false; CancelBtn.Enabled = false; stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < 32; i++) { SerPort.PortName = "COM" + Convert.ToString(i); try { SerPort.Open(); PortList.Items.Add(SerPort.PortName); } catch { } try { SerPort.Close(); } catch { } } try { PortList.SelectedIndex = 0; } catch { } if (File.Exists("savefile.txt")) { StreamReader sr = new StreamReader("savefile.txt"); AddToList(ref PortList, sr.ReadLine()); for (int i = 0; i < 10; i++) { AddToList(ref FilePathTxt, sr.ReadLine()); } sr.Close(); } DataProcessor.RunWorkerAsync(); }
private void StopPlayButton_Click(object sender, EventArgs e) { if (SerPort.IsOpen) { bot_busy = true; byte[] b = new byte[1]; b[0] = 3; SerPort.Write(b, 0, 5); } }
private void SendCmd(int cmd) { if (SerPort.IsOpen) { byte[] b = new byte[1] { Convert.ToByte(cmd) }; SerPort.Write(b, 0, 1); } }
private void PortChecker_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { if (SerPort.BytesToRead > 0) { bot_status = (int)SerPort.ReadByte(); } BotCtrl(); } }
private void AbortButton_Click(object sender, EventArgs e) { is_playing = false; byte[] b = new byte[5]; b[0] = 0; b[1] = 3; b[2] = 0; b[3] = 0; b[4] = 0xFF; SerPort.Write(b, 0, 5); }
private void Checker_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { PortStatusLabel.Text = "Port Status: " + SerPort.PortName + " is Opened at " + Convert.ToString(SerPort.BaudRate) + " Baud"; } else { PortStatusLabel.Text = "Port Status: Closed"; is_playing = false; try { SerPort.PortName = "COM6"; SerPort.BaudRate = 57600; SerPort.Open(); } catch { PortStatusLabel.Text = "Port Status: Error"; } } if (is_playing) { PlayButton.Enabled = false; CaliButton.Enabled = false; AbortButton.Enabled = true; FileListBox.Enabled = false; } else { CaliButton.Enabled = true; PlayButton.Enabled = true; AbortButton.Enabled = false; FileListBox.Enabled = true; stop_watch.Stop(); } PlayProgBar.Minimum = 0; PlayProgBar.Maximum = event_length; PlayProgBar.Value = event_cnt; SongLengthLabel.Text = Convert.ToString(length_of_song); TimeTakenLabel.Text = Convert.ToString(time_taken); TimeTakenToPlayLabel.Text = Convert.ToString(time_taken_tp); PercentAdjLabel.Text = Convert.ToString(percent_adj * 100); }
private void Player_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { if (SerPort.BytesToRead > 0) { bot_status = (int)SerPort.ReadByte(); if (bot_status == 1 && is_playing) { event_cnt = 0; SendNextInstruct(); stop_watch.Reset(); } else if (bot_status == 0 && is_playing) { SendNextInstruct(); } else if (bot_status == 2) { is_playing = false; event_cnt = 0; } else if (bot_status == 3) { stop_watch.Reset(); stop_watch.Start(); } else if (bot_status == 4) { stop_watch.Stop(); if (is_cali) { CalcAutoAdj(); ApplyToList(); } } } } if (is_cali) { time_taken = Convert.ToDouble(stop_watch.ElapsedMilliseconds) / (double)1000; } else { time_taken_tp = Convert.ToDouble(stop_watch.ElapsedMilliseconds) / (double)1000; } }
private void PortList_SelectedIndexChanged(object sender, EventArgs e) { Log("Port Changed"); try { SerPort.Close(); SerPort.PortName = PortList.Items[PortList.SelectedIndex].ToString(); SerPort.Open(); Log("Port Opened"); } catch { Log("Port Cannot be Opened"); bootload_enabled = false; ButtonsSet(true, true, false); } }
private void Checker_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { PortStatusLabel.Text = "Port Status: " + SerPort.PortName + " is Opened at " + Convert.ToString(SerPort.BaudRate) + " Baud"; } else { PortStatusLabel.Text = "Port Status: Closed"; try { SerPort.PortName = "COM6"; SerPort.BaudRate = 57600; SerPort.Open(); } catch { PortStatusLabel.Text = "Port Status: Error"; } } }
private void SendNextInstruct() { if (SerPort.BytesToWrite < 8) { byte[] b = new byte[5]; b[0] = 0; b[1] = (byte)dev_event[event_cnt].sp_code; int delay = dev_event[event_cnt].delay; delay += dev_event[event_cnt].delay_manualadj; if (is_cali == false) { delay += dev_event[event_cnt].delay_autoadj; } delay = Convert.ToInt32(Math.Round((double)delay * percent_adj)); b[3] = Convert.ToByte(delay % 256); b[2] = Convert.ToByte((delay - Convert.ToInt32(b[3])) / 256); b[4] = (byte)dev_event[event_cnt].bitmask; SerPort.Write(b, 0, 5); event_cnt++; if (b[1] == 3) { is_playing = false; if (is_cali) { CalcAutoAdj(); ApplyToList(); } } } }
private void StartBtn_Click(object sender, EventArgs e) { LoadProgress.Value = 0; if (!File.Exists(FilePathTxt.Text)) { Log("File Does Not Exist"); bootload_enabled = false; ButtonsSet(true, true, false); return; } AddToList(ref FilePathTxt, FilePathTxt.Text); for (int i = 0; i < 65536; i++) { flash_data[i] = 0xFF; } StreamReader sr; sr = new StreamReader(FilePathTxt.Text); string str = ""; bool fail_flag = false; do { try { str = sr.ReadLine(); if (str == null) { Log("End of File Reached"); } int record_type = Convert.ToInt32(str.Substring(7, 2), 16); if (record_type == 0) { int byte_cnt = Convert.ToInt32(str.Substring(1, 2), 16); int addr = Convert.ToInt32(str.Substring(3, 4), 16); for (int i = 0, j = 9; i < byte_cnt; i++, j += 2) { flash_data[addr + i] = Convert.ToInt16(str.Substring(j, 2), 16); if ((addr + i) > last_addr) { last_addr = (short)(addr + i); } } } else if (record_type == 1) { Log("End of Flash Reached"); break; } } catch { fail_flag = true; } }while (str != null); if (fail_flag) { Log("File Not Read"); bootload_enabled = false; ButtonsSet(true, true, false); } else { Log("File Successfully Read"); Log("Waiting for MCU Ready Signal"); bootload_enabled = true; ButtonsSet(false, false, true); previous_addr = -1; retry_cnt = 0; page_num = (short)((last_addr + 256) / 256); LoadProgress.Maximum = page_num * 256; page_num++; if (SerPort.IsOpen) { while (SerPort.BytesToRead > 0) { SerPort.ReadByte(); } } } sr.Close(); }
private void ProcessSend() { if (SerPort.IsOpen == false || bootload_enabled == false) { Thread.Sleep(250); return; } while (SerPort.BytesToRead >= 2) { while (SerPort.BytesToRead != 2) { SerPort.ReadByte(); } int c0 = SerPort.ReadByte(); int c1 = SerPort.ReadByte(); int addr = c0 + (c1 << 8); if (addr == 0xFFFF) { stopwatch.Start(); byte[] b = new byte[2] { (byte)(page_num % 256), (byte)(page_num >> 8), }; SerPort.Write(b, 0, 2); Log("MCU Sent Ready Signal"); Log("Replied Number of Pages: " + Convert.ToString(page_num, 10)); } else if (addr == page_num) { stopwatch.Stop(); long time_ms = stopwatch.ElapsedMilliseconds; stopwatch.Reset(); double time_s = (double)time_ms / 1000.0; Log("Bootloader Finished in " + Convert.ToString(time_s) + " Seconds"); bootload_enabled = false; ButtonsSet(true, true, false); } else { if (previous_addr == addr) { Log("Page Re-requested: " + Convert.ToString(addr, 10)); retry_cnt++; } else { retry_cnt = 0; } if ((previous_addr == addr || previous_addr + 1 == addr) && retry_cnt < 3) { previous_addr = (short)addr; checksum = 0; byte[] b = new byte[256]; while ((SerPort.WriteBufferSize - 256) < SerPort.BytesToWrite) { ; } if (page_num < 6) { for (int i = addr * 256, j = 0; j < 256; i++, j++) { b[0] = (byte)flash_data[i]; checksum += flash_data[i]; SerPort.Write(b, 0, 1); ProgressSet(Convert.ToInt32(Math.Min(i, LoadProgress.Maximum))); } } else { for (int i = addr * 256, j = 0; j < 256; i++, j++) { b[j] = (byte)flash_data[i]; checksum += flash_data[i]; } SerPort.Write(b, 0, 256); ProgressSet(Convert.ToInt32(Math.Min((addr * 256) + 256, LoadProgress.Maximum))); } b[0] = (byte)(checksum % 256); b[1] = (byte)(checksum >> 8); SerPort.Write(b, 0, 2); } else { Log("Fatal Error, Received: 0x" + Convert.ToString(addr, 16).ToUpper() + ", Checksum was: 0x" + Convert.ToString(checksum, 16).ToUpper() + ", Retried: " + Convert.ToString(retry_cnt)); bootload_enabled = false; ButtonsSet(true, true, false); } } } }
private void PortProcessor_Tick(object sender, EventArgs e) { if (SerPort.IsOpen) { int c; while (SerPort.BytesToRead != 0) { c = SerPort.ReadByte(); if (c >= 0x80) { addr = (c & 0x7F) >> 1; data = 0; cnt = 0; sign_flag = c % 2; } else { data += (c << (7 * cnt)); cnt++; if (cnt == 5) { double data_; if ((c & 0x40) == 0x40) { data -= (c << (7 * (cnt - 1))); if ((c & 0x20) == 0x20) { data_ = (double)data * Math.Pow(10, -1 * (double)(c & 0x1F)); } else { data_ = (double)data * Math.Pow(10, (double)(c & 0x1F)); } } else { data_ = (double)data; } if (sign_flag != 0) { data_ *= -1; } try { DataList.Items[addr].SubItems[1].Text = Convert.ToString(data_); } catch { } } } } } else { SerPort.BaudRate = 19200; try { SerPort.PortName = "COM6"; SerPort.Open(); } catch { try { SerPort.PortName = "COM3"; SerPort.Open(); } catch { } } } }
void BotCtrl() { if (bot_status == 0) { } else if (bot_status == 1) { bot_busy = false; bot_do_not_disturb = false; SongStatusLabel.Text = "Robot Wants File Name"; if (FileListBox.Items[FileListBox.SelectedIndex].Length != 0) { string str = ""; int i; for (i = 0; i < 8 && i < FileListBox.Items[FileListBox.SelectedIndex].Length; i++) { str += FileListBox.Items[FileListBox.SelectedIndex].SubString(i, 1); } for (int j = i; j < 8; j++) { str += "_"; } SerPort.Write(str); bot_status = 0; SongStatusLabel.Text = "Robot is Waiting for Command"; } } else if (bot_status == 2) { bot_do_not_disturb = true; bot_busy = true; byte[] b = new byte[2]; b[0] = event_length % 256; b[1] = Convert.ToByte((event_length - (int)b[0]) / 256); SerPort.Write(b, 0, 2); f_ptr = 0; bot_status = 0; } else if (bot_status == 3) { SongStatusLabel.Text = "Robot is Loading New Chart"; bot_busy = true; bot_do_not_disturb = true; if (SerPort.BytesToWrite < 8) { byte[] b = new byte[5]; b[0] = 0; b[1] = (byte)dev_event[f_ptr].sp_code; int delay = dev_event[f_ptr].delay; delay += dev_event[f_ptr].delay_manualadj; delay = Convert.ToInt32(Math.Round((double)delay * percent_adj)); b[3] = Convert.ToByte(delay % 256); b[2] = Convert.ToByte((delay - Convert.ToInt32(b[3])) / 256); b[4] = (byte)dev_event[f_ptr].bitmask; SerPort.Write(b, 0, 5); f_ptr++; ProgBar.Value = f_ptr; bot_status = 0; } } else if (bot_status == 4) { bot_busy = false; bot_do_not_disturb = false; SongStatusLabel.Text = "Robot is Finished Loading"; bot_status = 0; } else if (bot_status == 5) { bot_busy = true; SongStatusLabel.Text = "Robot is Playing"; bot_status = 0; } else if (bot_status == 6) { bot_busy = true; SongStatusLabel.Text = "Robot is Finished Playing"; bot_status = 0; } else if (bot_status == 7) { stop_watch.Reset(); stop_watch.Start(); bot_status = 0; } else if (bot_status == 8) { stop_watch.Stop(); time_taken = (double)stop_watch.ElapsedMilliseconds / (double)1000; TimeTakenLabel.Text = Convert.ToString(time_taken); bot_status = 0; } else if (bot_status == 128) { SongStatusLabel.Text = "File Missing on Disk"; bot_status = 0; } else if (bot_status == 255) { SongStatusLabel.Text = "Disk Error on Robot"; } if (bot_busy) { PlayButton.Enabled = false; LoadFileButton.Enabled = false; FileListBox.Enabled = false; } else { PlayButton.Enabled = true; LoadFileButton.Enabled = true; FileListBox.Enabled = true; } if (bot_do_not_disturb) { StopPlayButton.Enabled = false; } else { StopPlayButton.Enabled = true; } }