[270] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using Bridge.Html5;
|
---|
| 6 | using Bridge.jQuery2;
|
---|
| 7 |
|
---|
| 8 | namespace WebMrbc
|
---|
| 9 | {
|
---|
| 10 | internal class MainLoopWorkspace : IClassWorkspace
|
---|
| 11 | {
|
---|
| 12 | internal Workspace workspace;
|
---|
| 13 | BlocklyView view;
|
---|
| 14 | protected Ruby _RubyCode;
|
---|
| 15 |
|
---|
| 16 | public string Identifier { get { return view.Identifier; } }
|
---|
| 17 | public Workspace Workspace { get { return workspace; } }
|
---|
| 18 | public BlocklyView View { get { return view; } }
|
---|
| 19 | public Ruby RubyCode { get { return _RubyCode; } }
|
---|
| 20 |
|
---|
| 21 | public MainLoopWorkspace(BlocklyView view)
|
---|
| 22 | {
|
---|
| 23 | this.view = view;
|
---|
| 24 | workspace = view.Init();
|
---|
| 25 |
|
---|
| 26 | UpdateInitialBlock();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | private void UpdateInitialBlock()
|
---|
| 30 | {
|
---|
| 31 | var xml = Document.CreateElement("xml");
|
---|
| 32 | var vset = Document.CreateElement("block");
|
---|
| 33 | vset.SetAttribute("type", "variables_set");
|
---|
| 34 | xml.AppendChild(vset);
|
---|
| 35 |
|
---|
| 36 | var field = Document.CreateElement("field");
|
---|
| 37 | field.SetAttribute("name", "VAR");
|
---|
| 38 | field.AppendChild(Document.CreateTextNode("MAKER_CODE"));
|
---|
| 39 | vset.AppendChild(field);
|
---|
| 40 |
|
---|
| 41 | var value = Document.CreateElement("value");
|
---|
| 42 | value.SetAttribute("name", "VALUE");
|
---|
| 43 | vset.AppendChild(value);
|
---|
| 44 |
|
---|
| 45 | var text = Document.CreateElement("shadow");
|
---|
| 46 | text.SetAttribute("type", "text");
|
---|
| 47 | value.AppendChild(text);
|
---|
| 48 |
|
---|
| 49 | field = Document.CreateElement("field");
|
---|
| 50 | field.SetAttribute("name", "TEXT");
|
---|
| 51 | field.AppendChild(Document.CreateTextNode("\\x00\\x00\\xB3"));/* TOPPERSプロジェクト */
|
---|
| 52 | text.AppendChild(field);
|
---|
| 53 |
|
---|
| 54 | Blockly.Xml.domToWorkspace(xml, Workspace);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public string GetImageUrl()
|
---|
| 58 | {
|
---|
| 59 | return "img/no_image.png";
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public bool IsPreset()
|
---|
| 63 | {
|
---|
| 64 | return true;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public string ToCode(string filename)
|
---|
| 68 | {
|
---|
| 69 | if (Collections.EcnlTaskWorkspace == null)
|
---|
| 70 | return "";
|
---|
| 71 |
|
---|
| 72 | _RubyCode = new Ruby(filename);
|
---|
| 73 | var result = _RubyCode.defineMainLoop(this, Collections.EcnlTaskWorkspace.Identifier);
|
---|
| 74 | view.Changed = false;
|
---|
| 75 | return result;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | //Element ele;
|
---|
| 79 |
|
---|
| 80 | public void Activate()
|
---|
| 81 | {
|
---|
| 82 | //ele = Blockly.Xml.workspaceToDom(workspace);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public void Inactivate()
|
---|
| 86 | {
|
---|
| 87 | //workspace.clear();
|
---|
| 88 | //Blockly.Xml.domToWorkspace(ele, workspace);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public void ReloadToolbox(HTMLElement toolbox)
|
---|
| 92 | {
|
---|
| 93 | toolbox.AppendChild(Document.CreateElement("sep"));
|
---|
| 94 | var xml = jQuery.ParseXML(App.ArduinoToolbox);
|
---|
| 95 | var categories = xml.ChildNodes[0];
|
---|
| 96 | foreach (var item in categories.ChildNodes) {
|
---|
| 97 | if (item.NodeName != "category")
|
---|
| 98 | continue;
|
---|
| 99 | toolbox.AppendChild(item);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public void OpenModifyView(Action<bool> callback)
|
---|
| 104 | {
|
---|
| 105 | view.ReloadToolbox(this);
|
---|
| 106 | callback(true);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public string Template(string template)
|
---|
| 110 | {
|
---|
| 111 | template = template.Replace("%identifier%", Identifier);
|
---|
| 112 | template = template.Replace("%attribute%", "MainLoop");
|
---|
| 113 | template = template.Replace("%img%", GetImageUrl());
|
---|
| 114 | return template;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | partial class Ruby
|
---|
| 119 | {
|
---|
| 120 | internal string defineMainLoop(MainLoopWorkspace workspace, string identifier)
|
---|
| 121 | {
|
---|
| 122 | string code = workspaceToCode(workspace.Workspace);
|
---|
| 123 | var sb = new StringBuilder();
|
---|
| 124 |
|
---|
| 125 | sb.Append(code);
|
---|
| 126 | sb.AppendLine();
|
---|
| 127 |
|
---|
| 128 | sb.AppendLine($"ctrl = {identifier}.new()");
|
---|
| 129 | sb.AppendLine();
|
---|
| 130 | sb.AppendLine("# メインループ");
|
---|
| 131 | sb.AppendLine("while (true) do");
|
---|
| 132 | sb.AppendLine(" # メッセージ待ち");
|
---|
| 133 | sb.AppendLine(" ret = TargetBoard::wait_msg(ctrl.timer)");
|
---|
| 134 | sb.AppendLine(" if !ret then");
|
---|
| 135 | sb.AppendLine(" break;");
|
---|
| 136 | sb.AppendLine(" end");
|
---|
| 137 | sb.AppendLine();
|
---|
| 138 | sb.AppendLine(" # 経過時間の計算");
|
---|
| 139 | sb.AppendLine(" ctrl.progress ret[0]");
|
---|
| 140 | sb.AppendLine();
|
---|
| 141 | sb.AppendLine(" # 戻り値が2つなら");
|
---|
| 142 | sb.AppendLine(" if ret.length == 2 then");
|
---|
| 143 | sb.AppendLine(" # 内部イベント");
|
---|
| 144 | sb.AppendLine(" case (ret[1])");
|
---|
| 145 | sb.AppendLine(" when 1 then");
|
---|
| 146 | sb.AppendLine(" TargetBoard::restart");
|
---|
| 147 | sb.AppendLine(" when 2 then");
|
---|
| 148 | sb.AppendLine(" ctrl.ntf_inl");
|
---|
| 149 | sb.AppendLine(" end");
|
---|
| 150 | sb.AppendLine(" # 戻り値が3つなら");
|
---|
| 151 | sb.AppendLine(" elsif ret.length == 3 then");
|
---|
| 152 | sb.AppendLine(" # 通信レイヤーからのメッセージ(通信端点と電文)");
|
---|
| 153 | sb.AppendLine(" ctrl.recv_msg(ret[1], ret[2])");
|
---|
| 154 | sb.AppendLine(" end");
|
---|
| 155 | sb.AppendLine();
|
---|
| 156 | sb.AppendLine(" # タイムアウトの処理があれば行う");
|
---|
| 157 | sb.AppendLine(" ctrl.call_timeout");
|
---|
| 158 | sb.AppendLine("end");
|
---|
| 159 |
|
---|
| 160 | return sb.ToString();
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|