Porting NETMF port operations to TinyCLR

Hello, I am taking a program I built for FEZ Panda 3 using NETMF and moving it to one of the new FEZ boards running TinyCLR.

On NETMF, there was an easy way to toggle an output, using the OutputPort class.

static OutputPort HEARTBEAT_IND = new OutputPort(FEZPandaIII.Gpio.Led4, false);

then, when I toggle it,

 while (true)
            {
                HEARTBEAT_IND.Write(!HEARTBEAT_IND.Read());
               Thread.Sleep(400);
            }

/* I am moving it over from NETMF to TinyCLR because the available classes of FEZ boards in the definition of an OutputPort do not include the FEZ. Error (Type or Namespace ‘FEZ’ does not exist in the namespace GHI.Pins) */

When I write for TinyCLR , I need to use the GpioPin type

static GpioPin HEARTBEAT_IND = GpioController.GetDefault().OpenPin(GHIElectronics.TinyCLR.Pins.FEZ.GpioPin.Led2);

If I attempt to toggle the same way , it flags an error that the operator ! cannot be applied to operand of type GpioPin.
This:

HEARTBEAT_IND.Write(!HEARTBEAT_IND.Read());

I tried casting the return from HEARTBEAT_IND.Read(); without success

HEARTBEAT_IND.Write((bool)!HEARTBEAT_IND.Read());

Error ----> Cannot convert type GpioPinValue to bool

Instead, I have to run through an if else statement to make the same function happen.

if(HEARTBEAT_IND.Read() == GpioPinValue.Low)
                    {
                    HEARTBEAT_IND.Write(GpioPinValue.High);
                }
                else
                {
                    HEARTBEAT_IND.Write(GpioPinValue.Low);
                }

I cannot imagine a simple function like this is lost in TinyCLR, so it must be something I am missing.
Any help is appreciated.
Thanks

Find out what the return type from HEARTBEAT_IND.Read() is and change your logic to match.

Reading the following might help…
https://docs.ghielectronics.com/software/tinyclr/tutorials/gpio.html

If you want to be able to pass a bool into pin.Write, add the following extension class:

static class Extensions
{
    public static void Write(this GpioPin pin, bool value)
    {
        pin.Write(value == true ? GpioPinValue.High : GpioPinValue.Low);
    }
}
2 Likes

exact for this way

i searched earlier to make (Legacy mode/code for) TinyCLR to be able to use either new way either old .NET Microframework style of programming …

I think your best bet is to start with the TinyCLR OS tutorials.

1 Like

with TinyCLR OS now i can do it in dual way programming of hardware :slight_smile:

  • TinyCLR OS WAY
  • .NET Microframework WAY

code of extension

using System;
using GHIElectronics.TinyCLR.Devices.Gpio;

namespace GHIElectronics.TinyCLR.Devices.Gpio
{
    static class Extensions
    {

        public static void Write(this GpioPin pin, bool value)
        {
            pin.Write(value == true ? GpioPinValue.High : GpioPinValue.Low);
        }

        public static bool Reads(this GpioPin pin)
        {
            bool val = false;
            if (pin.Read() == GpioPinValue.High) val = true;

            return val;
        }

        public static GpioPin OutputPort(this GpioPin pin, bool value)
        {
            pin.SetDriveMode(GpioPinDriveMode.Output);
            pin.Write(value);

            return pin;
        }

        public static GpioPin InputPort(this GpioPin pin, bool value, GpioPinDriveMode gpioPinDriveMode)
        {
            pin.SetDriveMode(gpioPinDriveMode);
            pin.Write(value);

            return pin;
        }
    }
}
2 Likes