Gadgeteer Touch piano

I had some extra time this week so I thought I do something a bit musical today.  I created a simple touch piano with my gadgeteer kit.  I use a simple piezo speaker attached to an extender module.  The display module shows a series of labeled keys.  Each key is wired to the TouchDown and TouchUp events.  Pressing the key sets the PWM for the appropriate tone.

The code for this project can be found in the SimpleBuzzer project in my GitHub repository.

Here’s the keyboard in action:

The UI layout is pretty easy. I first build a panel with the Border objects representing keys.

        private Panel GetPanel()
        {
            Panel mainPanel = new Panel();
            StackPanel sp = new StackPanel(Orientation.Horizontal);
            mainPanel.Children.Add(sp);

            Border c = GetBorder("C");
            Border d = GetBorder("D");
            Border e = GetBorder("E");
            Border f = GetBorder("F");
            Border g = GetBorder("G");
            Border a = GetBorder("A");
            Border b = GetBorder("B");
            sp.Children.Add(c);
            sp.Children.Add(d);
            sp.Children.Add(e);
            sp.Children.Add(f);
            sp.Children.Add(g);
            sp.Children.Add(a);
            sp.Children.Add(b);
            return mainPanel;
        }

The GetBorder method returns an appropriately sized Border with a label wired to the touch events

        public Border GetBorder(String text)
        {
            Font f = Resources.GetFont(Resources.FontResources.small);
            Border retval = new Border();
            retval.Background = new SolidColorBrush(GT.Color.White);
            retval.BorderBrush = new SolidColorBrush(GT.Color.Black);
            retval.Foreground = new SolidColorBrush(GT.Color.Black);
            retval.Height = 35;
            retval.Width = 35;
            retval.SetMargin(4,4,4,4);
            Text t = new Text(f,text);
            t.ForeColor = GT.Color.Black;
            retval.Child = t;
            retval.TouchDown += TouchDown;
            retval.TouchUp += TouchUp;
            return retval;
        }

The TouchDown event plays the tone selected. The TouchUp event stops playing the tone.

        void TouchDown(object sender, TouchEventArgs e)
        {
            Border b = (Border) sender;
            Text t = (Text) b.Child;
            uint tone = 2272; // A
            if (t.TextContent == "C") tone = 3830;
            if (t.TextContent == "D") tone = 3400;
            if (t.TextContent == "E") tone = 3038;
            if (t.TextContent == "F") tone = 2864;
            if (t.TextContent == "G") tone = 2550;
            if (t.TextContent == "A") tone = 2272;
            if (t.TextContent == "B") tone = 2028;
            
            pwm.Active = true;
            pwm.SetPulse(tone * 1000, (tone / 2) * 1000);
        }

        private void TouchUp(object sender, TouchEventArgs e)
        {
            pwm.Active = false;
        }