CSplusのPython Consoleを使ってRenesas RX Simulator用のテキストベースのInteractive Input/Output Formを作ってみた

こんにちは。NoMaYです。

別スレッド『e2 studio v7.5.0のFreeRTOS ProjectでVisual Expression+Renesas RX Simulator/TB-RX65Nで試せるSample Programを作ってみた』で、e2 studioのVisual Expressionを使ってRenesas RX Simulator用の入出力画面(GUIベース:LEDが2個+ボタンが1個)を作ってみたのですが、今度は、同様なもの(テキストベースですが)をCS+のPythonコンソールで.NET FrameworkのFormを使って作ってみました。以下の画面コピーの通り見栄えは決して良くないですが、必要充分には機能するものを作ることが出来ました。

CS+のPythonコンソールで.NET FrameworkのFormを使って作って入出力画面(テキストベース)


(参) e2 studioのVisual Expressionを使って作って入出力画面(GUIベース)


今回のCS+のPythonコンソールのスクリプトは以下の通りです。赤文字箇所でCS+のデバッガ機能を呼び出しています。([追記] フォームを開いたままプロジェクトを閉じるとエラーが発生することに気付いたので橙文字箇所を追加しました。)

sim_rx65n_LED0_LED1_SW1_form.py

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form, FormBorderStyle, Label, Button, Timer
from System.Drawing import Size, Color, Font, FontStyle, ContentAlignment

class IForm(Form):
    def __init__(self):
        self.Text = 'LED0, LED1, SW1 Interactive Input/Output Form'
        self.ClientSize = Size(440, 100)
        self.FormBorderStyle = FormBorderStyle.FixedSingle
        self.MaximizeBox = False
        font = Font('MS UI Gothic', 32, FontStyle.Bold)
        ta = ContentAlignment.MiddleCenter
        self.LED0 = Label(Top=20, Left=20, Height=60, Width=120, Font=font, TextAlign=ta)
        self.LED0.Text = 'LED0'
        self.LED1 = Label(Top=20, Left=160, Height=60, Width=120, Font = font, TextAlign=ta)
        self.LED1.Text = 'LED1'
        self.SW1 = Button(Top=20, Left=300, Height=60, Width=120, Font = font, TextAlign=ta)
        self.SW1.Text = 'SW1'
        self.SW1.Click += self.OnClickSW1
        self.Controls.Add(self.LED0)
        self.Controls.Add(self.LED1)
        self.Controls.Add(self.SW1)
        self.timer = Timer()
        self.timer.Interval = 200
        self.timer.Tick += self.OnTickTimer
    def OnLoad(self, event):
        print 'Now Running...'
        self.ThrowExceptSave = common.ThrowExcept
        self.ViewOutputSave  = common.ViewOutput
        common.ThrowExcept = False
        common.ViewOutput  = False
        self.timer.Start()
        self.BringToFront()
    def OnClosed(self, event):
        self.timer.Stop()
        common.ThrowExcept = self.ThrowExceptSave
        common.ViewOutput  = self.ViewOutputSave
        print 'Quit'
    def OnTickTimer(self, sender, event):
        # The debugger (or build) object does not exist if no project is opened.
        # Or the debugger (or build) object will disappear after project is closed.
        try:
            if 0 == debugger.Watch.GetValue("PORTD.PODR.B6"):
                self.LED0.BackColor = Color.Lime
            else:
                self.LED0.BackColor = Color.DarkGray
            if 0 == debugger.Watch.GetValue("PORTD.PODR.B7"):
                self.LED1.BackColor = Color.Lime
            else:
                self.LED1.BackColor = Color.DarkGray
        except:
            pass
    def OnClickSW1(self, sender, event):
        # The debugger (or build) object does not exist if no project is opened.
        # Or the debugger (or build) object will disappear after project is closed.
        try:
            debugger.Watch.SetValue("PORTB.PIDR.B1", 0)
        except:
            pass

Application.Run(IForm())

なお、このスクリプトは、以下の画面コピーのように、スクリプトファイルをCS+のプロジェクトツリーに登録するようにして、ファイルの右ボタンメニューの[Pythonコンソールで実行する]で実行させるのが、一番楽ではないかと思います。



[追記]

フォームを開いたままプロジェクトを閉じると以下の画面コピーのエラーが発生することに気付いたので修正しました。