Main Site Documentation

PWM spaghetti code



im trying to port my code from NET MF to TinyCLR and using my PWM outputs is a mess.

this is my old code:

static PWM channel1 = new PWM(GHI.Pins.G30.PwmOutput.PC8, hertz, light, false);
static PWM channel2 = new PWM(GHI.Pins.G30.PwmOutput.PC9, hertz, light, false);
static PWM channel3 = new PWM(GHI.Pins.G30.PwmOutput.PA0, hertz, light, false);
static PWM channel4 = new PWM(GHI.Pins.G30.PwmOutput.PA1, hertz, light, false);
static PWM channel5 = new PWM(GHI.Pins.G30.PwmOutput.PA2, hertz, light, false);
static PWM channel6 = new PWM(GHI.Pins.G30.PwmOutput.PA3, hertz, light, false);
static PWM channel7 = new PWM(GHI.Pins.G30.PwmOutput.PB9, hertz, light, false);
static PWM channel8 = new PWM(GHI.Pins.G30.PwmOutput.PB8, hertz, light, false);
static PWM channel9 = new PWM(GHI.Pins.G30.PwmOutput.PA8, hertz, light, false);

and this is the new one:

PwmController pwm1 = PwmController.FromName(G30.PwmChannel.Controller1.Id);
PwmController pwm2 = PwmController.FromName(G30.PwmChannel.Controller2.Id);
PwmController pwm3 = PwmController.FromName(G30.PwmChannel.Controller3.Id);
PwmController pwm4 = PwmController.FromName(G30.PwmChannel.Controller4.Id);

        PwmChannel channel1 = pwm3.OpenChannel(G30.PwmChannel.Controller3.PC8);
        PwmChannel channel2 = pwm3.OpenChannel(G30.PwmChannel.Controller3.PC9);
        PwmChannel channel3 = pwm2.OpenChannel(G30.PwmChannel.Controller2.PA0);
        PwmChannel channel4 = pwm2.OpenChannel(G30.PwmChannel.Controller2.PA1);
        PwmChannel channel5 = pwm2.OpenChannel(G30.PwmChannel.Controller2.PA2);
        PwmChannel channel6 = pwm2.OpenChannel(G30.PwmChannel.Controller2.PA3);
        PwmChannel channel7 = pwm4.OpenChannel(G30.PwmChannel.Controller4.PB9);
        PwmChannel channel8 = pwm4.OpenChannel(G30.PwmChannel.Controller4.PB8);
        PwmChannel channel9 = pwm1.OpenChannel(G30.PwmChannel.Controller1.PA8);

this has a high risk of messing up controllers and channels and it completely passes, no error checking

can this be written differently??


NETMF did this completely wrong. Actually in NETMF there was no checking. If you change the frequency on a channel, it may or may not effect the other channels. It was horrible!

In TinyCLR, controllers are separate and so you are sure that changing the frequency on a specific channel would not effect the other, as long as they are on different controllers.

I would change your code to specify the controller in the name. For example channel1C4.