Main Site Documentation

Common helpers / extensions library?


#1

Hi All,

Does anyone know if there is already a common extensions library for net mf? I have found myself creating equivillents to string.IsNullOrEmpty, and string.Replace and wrapper for converting byte[] to a string etc. I’m sure alot of others are doing the same thing!

If there isn’t it would me nice to make one (im happy to start it).

Cheers,
Ross


#2

I don’t think there is anything like this right now, so you might be a pioneer in starting it :slight_smile:


#3

This is a good idea. We are looking to make this on fezzer.com


#4

Cool. Well i’ll take a look at Fezzer once its up from maintenance and create a project.

Hopefully it supports contributions / teams for projects like codeplex…?

EDIT: Looks like it doesn’t. I’ll start one at codeplex as the project so it can be open source and contributed to, and upload release files / zip to Fezzer.


#5

Fezzer is still under beta, so starting it on codeplex is probably a good idea.


#6

I’d also suggest that it (Fezzer) was not meant to replace something like codeplex. The focus might be wider than this now, but it started as a way to share good code snippets and the like, to help not reinvent the wheel, and to not lose the details that get talked about in threads here. That means it’s more sharing than collaboration - but since it’s still beta (and the guys at GHI are so dynamic) you’ll probably see that it blends those lines as it evolves.


#7

excellent idea, heres something I use all the time


namespace System
{
    // SPOT doesnt have this class so well make one 
    // maybe later it will be included
    class BitConverter
    {
        public static string ToString( byte[] data, int offset, int count )
        {
            const string hex = "0123456789ABCDEF";

            // convert bytes to a hex string???
            char[] chars = new char[count * 3 - 1];
            int index = 0;

            //foreach (byte b in data)
            for ( int n = 0; n < count; n++ )
            {
                byte b = data[offset + n];
                chars[index++] = hex[b / 16];
                chars[index++] = hex[b % 16];
                if ( index < chars.Length )
                    chars[index++] = '-';
            }

            return new string( chars );
        }

        public static string ToString( byte[] data )
        {
            return ToString( data, 0, data.Length );
        }

        public static UInt16 ToUInt16( byte[] data, int offset )
        {
            return (UInt16)Microsoft.SPOT.Hardware.Utility.ExtractValueFromArray( data, offset, 2 );
        }

        public static UInt32 ToUInt32( byte[] data, int offset )
        {
            return (UInt32)Microsoft.SPOT.Hardware.Utility.ExtractValueFromArray( data, offset, 4 );
        }
    }
}


#8

Thanks. I’ve setup a code project site. Just getting the details and some initial information / code up there and i’ll put it up here so people can add suggested extensions. I’ll post back with a link in a couple of days.


#9

I rather like this function from Jens Kuhner’s book

public static string ByteToHex(byte b)
        {
            const string hex = "0123456789ABCDEF";
            int lowNibble = b & 0x0F;
            int highNibble = (b & 0xF0) >> 4;
            string s = new string(new char[] { hex[highNibble], hex[lowNibble] });
            return s;
        }

And this map function taken from Arduino

private uint map(int x, int in_min, int in_max, int out_min, int out_max)
{
   int Puls = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
   return (uint)Puls;
}

And it is used like this;


if (_newPos != _currentPos)
{
   _currentPos = _newPos;
   uint Puls = map(_currentPos, -40, 40, 800, 2200);
   Rudder.SetPulse(20 * 1000 * 1000, Puls * 1000);
}

#10

Look at all this useful code…where is the code share website? :frowning: Josh? hurry hurry!


#11

Just to keep it all together.

Sync time with NTP server: http://www.tinyclr.com/forum/10/1294/


        /// <summary>
        /// Get DateTime from NTP Server 
        /// Based on:
        /// http://weblogs.asp.net/mschwarz/archive/2008/03/09/wrong-datetime-on-net-micro-framework-devices.aspx
        /// </summary>
        /// <param name="TimeServer">Timeserver</param>
        /// <returns>Local NTP Time</returns>
        public static DateTime NTPTime(String TimeServer)
        {
            // Find endpoint for timeserver
            IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);
 
            // Connect to timeserver
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            s.Connect(ep);
 
            // Make send/receive buffer
            byte[] ntpData = new byte[48];
            Array.Clear(ntpData, 0, 48);
 
            // Set protocol version
            ntpData[0] = 0x1B;
 
            // Send Request
            s.Send(ntpData);
 
            // Receive Time
            s.Receive(ntpData);
 
            byte offsetTransmitTime = 40;
 
            ulong intpart = 0;
            ulong fractpart = 0;
 
            for (int i = 0; i <= 3; i++)
                intpart = (intpart << 8) | ntpData[offsetTransmitTime + i];
 
            for (int i = 4; i <= 7; i++)
                fractpart = (fractpart << 8) | ntpData[offsetTransmitTime + i];
 
            ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
 
            s.Close();
 
            TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
            DateTime dateTime = new DateTime(1900, 1, 1);
            dateTime += timeSpan;
 
            TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            DateTime networkDateTime = (dateTime + offsetAmount);
 
            return networkDateTime;
        }


#12

Note: above NTP code only works on Cobra, not on W5100-enabled ethernet boards.

[quote]The method Connect() Receive() Send() work for TCP and UDP in FEZ Cobra ( Microsoft’s sockets). They work only with TCP sockets now with Wiznet sockets. Use ReceiveFrom() and SendTo() for UDP.
[/quote]


#13

Hi All,

Here is a sneak peak of the functionality that will be available as of go-live in a couple of days. Coded most of it already, just fine tuning and completing unit tests of them.

  • StringBuilder
  • string.Replace
  • StringUtility.Format (e.g. StringUtility.Format("{0:F}",7) will return “7.00”)
  • StringUtility.IsNullOrEmpty
  • Parse.TryParseInt (Long/Short etc)

Will be released under general MIT license. Will look at adding additional functionality as suggested here, just need to make sure there are no license issues etc with using it.


#14

Initial Release is up and ready to go!!

“.NET Micro Framework - Common Extensions” is designed to provide additional functionality to the core .NET Micro framework to fill common usage gaps between NetMf and it’s bigger .NET framework cousins.

The functionality provided within this project includes:
StringBuilder, String.Replace, StringUtility.Format, StringUtility.IsNullOrEmpty and more!

http://netmfcommonext.codeplex.com/


#15

I missed having Replace and Format, so those will definitely get heavy use.
Thank you!