Netduino Plus 1 and Pins class - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Netduino Plus 1 and Pins class


  • Please log in to reply
14 replies to this topic

#1 BlueSky

BlueSky

    Member

  • Members
  • PipPip
  • 13 posts

Posted 14 August 2014 - 06:26 AM

Hi, I have a question! In Pins class some variables of Cpu.Pin are created and integer values have been assigned to them. In the usual, it is not possible because Cpu.Pin is an enum and we can’t assign new integer values to them. How is this possible?

For example:

public const Cpu.Pin ONBOARD_LED = 55;

 

 

I’m using the Netduino Plus 1 and Firmware v4.1.



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 14 August 2014 - 02:56 PM

Hi BlueSky,

Try type-casting the pin #...

Cpu.Pin ONBOARD_LED = (Cpu.Pin)55;
Does that get you going?

Chris

#3 BlueSky

BlueSky

    Member

  • Members
  • PipPip
  • 13 posts

Posted 14 August 2014 - 07:38 PM

Hi Chris,

 

I know for this casting, I can use these ways:

1)

public const Cpu.Pin ONBOARD_LED = (Cpu.Pin) 55;

 

2)

public const Cpu.Pin GPIO_PIN_D2 = (Cpu.Pin) 0;

public const Cpu.Pin ONBOARD_LED = GPIO_PIN_D2 + 55;

 

3)

implicit operator

 

But this casting in the Pins class was interesting for me and I like to understand how it is possible because there is not any casting in this code!

public const Cpu.Pin ONBOARD_LED = 55;

 

Thanks for your help.

I would like to know about programming the Pins class in SecretLabs.NETMF.Hardware.NetduinoPlus namespace!



#4 beastyboy

beastyboy

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationNetherlands

Posted 15 August 2014 - 04:18 PM

Hi,

 

default type for enum is int:

 

check this:

 

http://msdn.microsof...y/sbbt4032.aspx

 

But changing them is considered bad practice:

 

http://msdn.microsof...y/cc138362.aspx

 

Cheers Olaf


Van SchOten Ict Diensten en Services
http://www.voids.nl


#5 BlueSky

BlueSky

    Member

  • Members
  • PipPip
  • 13 posts

Posted 15 August 2014 - 08:00 PM

Hi Cheers,

 

Thanks for your help! These links in accordance with my statement. You also agree with me, right?

 

 

I know default type for enum is int.

But we can assign integer values to each enumerator when we define an enum.

When we define a variable of enum, we are not allow to assign new integer value to it, because it is a compiler error!

 

In this sample after defining an enum (Cpu.Pin) ,an integer value have been assigned to a variable of Cpu.Pin!

 

public const Cpu.Pin ONBOARD_LED = 55;

 

I’ve tested this code and it is a compiler error!

 

and in this link:

http://msdn.microsof...y/cc138362.aspx

it is said:

To assign an arbitrary value to a variable of an enumeration type is to introduce a high risk for errors.



#6 beastyboy

beastyboy

    Advanced Member

  • Members
  • PipPipPip
  • 194 posts
  • LocationNetherlands

Posted 16 August 2014 - 01:06 AM

The danger is that you should not change enum values and just set them once.

 

For example the cpu.pin ONBOARD_LED could have a different value on the Netduino 1 and the Netduino Plus.

 

So only use the names and don't change the values.

 

They are to be set by the actual person that knows how the pins relate.

 

So if they are set to 55 then that is the correct value for your hardware.

 

They should be set only one time and not to be changed during execution.

 

So the variable is attached to the ONBOARD_LED not to the cpu.pin that is just the type.


Van SchOten Ict Diensten en Services
http://www.voids.nl


#7 BlueSky

BlueSky

    Member

  • Members
  • PipPip
  • 13 posts

Posted 16 August 2014 - 06:16 PM

Hi,

 

Thanks for your help, Cheers! But I’m confused!

 

I know about setting of pins and their values, but my question about programming these and their assignments!

 

For the first time, When Pins class is written and is run successfully (without error)?

 

You mean that the setting is done at the time of porting board? This mean that there is an error but this program is running correctly in the first (at the time of porting)? If I want to write a Pins class with correct values, I don’t have any problems and it is running successfully? So how do you solve compiler error?

 

Because I really have an error for this line! :( 

 

public const Cpu.Pin ONBOARD_LED = 55;

 

Error :

Cannot implicitly convert type 'int' to 'Microsoft.SPOT.Hardware.Cpu.Pin'. An explicit conversion exists (are you missing a cast?)

 

afsane



#8 BlueSky

BlueSky

    Member

  • Members
  • PipPip
  • 13 posts

Posted 18 August 2014 - 05:22 PM

Hi everyone,
 
There is no one to help me?! :huh: 


#9 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 18 August 2014 - 06:16 PM

Hi BlueSky,

If you use the explicit type conversion [i.e. (Cpu.Pin)55] then it should work for you. Due to the way that Cpu.Pin is defined and used, we recommend using explicit conversions if you need to specify a physical MCU pin #.

Chris

#10 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 18 August 2014 - 07:01 PM

public const Cpu.Pin ONBOARD_LED = 55;

 

Where exactly do you see this code? I cannot find it anywhere in the source. Are you inspecting the assembly (.dll) with some tool like .NET Reflector?



#11 BlueSky

BlueSky

    Member

  • Members
  • PipPip
  • 13 posts

Posted 18 August 2014 - 08:07 PM

Hi chris,

 

Thanks alot for your help! :) 



#12 BlueSky

BlueSky

    Member

  • Members
  • PipPip
  • 13 posts

Posted 18 August 2014 - 08:13 PM

Hi CW2,

 

No, I don’t use any software.

 

In the visual studio, you should write Pins and then right click on Pins and after that, click on Go To Difinition!

So you can see Pins class in a new tab, and you can see this code!

 

I attach the picture of it for you!

 

 

using System;

using SecretLabs.NETMF.Hardware.NetduinoPlus;
 
namespace test1
{
    public class Program
    {
        public static void Main()
        {
            Pins
        }
    }
}

 

Attached Files



#13 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 18 August 2014 - 08:38 PM

You have to watch how names of pins get passed around.

 

I define a variable like such

 

RegulatedMotor M0 = new RegulatedMotor( PWMChannels.PWM_PIN_D5, Pins.GPIO_PIN_D9, Pins.GPIO_PIN_D10 );

 

but the function is defined

 

public RegulatedMotor(Cpu.PWMChannel pwmPin, Cpu.Pin encA, Cpu.Pin encB) { ... }



#14 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 18 August 2014 - 08:44 PM

So you can see Pins class in a new tab, and you can see this code!

 

Ok, thanks for the information. Well, the explanation is the code you see is not the actual source, but generated by Visual Studio via reflection - and obviously it is not quite correct, as the [mandatory] cast is missing. It could even be a 'feature', to make the definition easier to read by removing the obvious cast.



#15 BlueSky

BlueSky

    Member

  • Members
  • PipPip
  • 13 posts

Posted 19 August 2014 - 04:17 AM

Hi,

 

Oooo…! :o 

 

I didn’t know this! Excuse me for my mistake!

 

CW2, you’ve really helped me! Thanks for your help.

 

And everyone, Thanks a lot for your help,too! :) 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.