Nisi’s work blog

Nisi’s work blog – programing tips

Touch Commander 2.0

Touch Commander 2.0

Pentru cunoscatorii produselor HTC nu mai este o noutate ingenioasa aplicatie Touch Cube, aplicatie care da posibilitatea sa iti folosesti touch screenul in adevaratul sens adica sa iti controlezi smartphonul direct de pe ecran fara stilus-uri si alte butuoane ajutatoare.

OK dar ceilalti posesori de smartphonuri, PDA-uri care nu au fost atat de inspirati sa is cumpere un HTC ce facem cu ei? Pai … de la ideea asta au plecat cei de la http://www.touch-commander.com care au realizat un program asemanator dand posibilitatea ca oricine are un PDA/Smartphone cu Windows Mobile 5, 6 sa se bucure de device-ul cumparat inca odata asa cum s-au bucurat in prima zi cand l-au achizitionat.

Touch Commander 2.0 - clona a softului Touch Cube de pe smartphonurile HTC Touch

Cerinte sistem:

  1. Windows Mobile 5, 6 Based Device
  2. NET Compact Framework 2.0

    (for Windows Mobile 5 only)

Download Touch Commander 2.0

C for Atmega (GCC) Data Types

Tipuri de date recunoscute de compilatorul GCC pentru microcontrolele de la Atmel

Toate Exemplele sunt testate in Avr-Studio & GCC plugin

Tipuri Intregi de date

8 bit types

  1. typedef signed char        int8_t
  2. typedef unsigned char    uint8_t

16 bit types

  1. typedef int                       int16_t
  2. typedef unsigned            int uint

32 bit types

  1. typedef long                    int32_t
  2. typedef unsigned long    uint32_t

64 bit types

  1. typedef long long                    int64_t
  2. typedef unsigned long long    uint64_t

Pointer Types

  • typedef int16_t       intptr_t
  • typedef uint16_t     uintptr_t



Atmega 128 – UART Comunication

Exemplul este valabil pentru Atmega128 + Quartz 8MHz

Atmega 128 – UART Comunication  

  1. UART0
  • Initializare

               BaudRate 4800 se seteaza registrul UBRR1H = 0 si UBRR1L = 103

               BaudRate 9600 se seteaza registrul UBRR1H = 0; UBRR1L = 51;

               BaudRate 115200  – UBRR1H = 0; UBRR1L = 3;

  • Activare Linie RX, TX si intreruperea aferenta lui RX

              - pentru a se putea citi date este necesar sa se activeze linia RX

              – de asemenea pantru a se trimite date trebuie activata linia TX

                         UCSR1B = (1<<RXEN1)|(1<<TXEN1)

             – Pentru a citi date de pe linia RX exista 2 posibilitati: sa se astepte in continuu pana la RX vine “ceva” in cazul asta nu se mai poate face nimic altceva pana nu se citeste de la UART sau sa se semnaleze cu ajutorul unei intreruperi momentul exact cand vine o data pe Rx si doar atunci sa se faca citirea.

            – Pentru a “spune” microcontrolerulul ca intreruperea pentru Rx este activata se foloseste urmatoarea linie de cod:

                        UCSR1B = (1<<RXEN1)|(1<<TXEN1) | (1 << RXCIE1)

  •  Restul de setari: (no parity, 8data, 1stop)

                        UCSR1C = (3<<UCSZ10);

  •  Citire byte din UART1

                      while ((UCSR1A & (1<<RXC1))==0);   //  astept pana apare ceva la portul serial

                      return UDR1;   // dupa ce iese din while -ul de mai sus in UDR1 este disponibil un Byte

Formatting C# strings

  1.  
  2. string s = String.Format("{{ hello to all }}");
  3. Console.WriteLine(s);    //prints ‘{ hello to all }’
  4.  
  5. int i = 42;
  6. string s = String.Format("{0}", i);   //prints ‘42′
  7.  
  8. int i = 42;
  9. string s = String.Format("{{{0}}}", i);   //prints ‘{42}’
  10.  
  11.  

  1.  
  2. int i = 42;
  3. string s = String.Format("{0:N}", i);   //prints ‘42.00′
  4.  
  5. int i = 42;
  6. string s = String.Format("{{{0:N}}}", i);   //prints ‘{N}’
  7.  
  8. int i = 42;
  9. string s = String.Format("{0:N!}", i);   //prints ‘N!’
  10.  
  11. int i = 42;
  12. string s = String.Format("{{{0:N}}}", i);   //prints ‘{N}’
  13.  
  14. string s =
  15. String.Format("{0}{1}{2}", "{", i, "}");   //prints ‘{42.00}’
  16.  
  17. int i = 42;
  18. string s = String.Format("{{{0}}}", i.ToString("N")) ;   //prints ‘{42.00}’
  19.  
  20. int i = 42;
  21. string s = String.Format("{0:{{0.00}}}", i);   //prints ‘{42.00}’
  22.  
  23. int i = 42;
  24. string s = String.Format("{0,-7:N}", i);   //prints ‘42.00  ’, ",-7" left-justifies the string
  25.  
  26. int i = 42;
  27. string s1 = String.Format("{{{0,-7:N}}}", i);   //prints ‘{42.00  }’
  28. string s2 = String.Format("{{{0,-7}}}", i.ToString("N")) ;   //prints ‘{42.00  }’
  29. string s3 = String.Format("{0,-9:{{0.00}}}", i);   //prints ‘{42.00}  ’
  30.  
  31. int i = 42000;
  32. string s = String.Format("{0,-15:{{#,##0.00}}}", i);   //prints ‘{42,000.00}     ‘

LINQ to DataSet (C#)

LINQ to DataSet

  • Perform set operations on sequences of DataRow objects.
  • Retrieve and set DataColumn values
  • Obtain a LINQ standard IEnumerable sequence from a DataTable so Standard Query Operatorsmay be called.


To use LINQ to Dataset include next namespaces

  1. using System.Data; using System.Linq;

Step by Step Exemple:

Starting with a simple class:

  1. class Student {
  2.       public int Id;
  3.       public string Name;
  4.  }

this class is like a table where we have an row Id (public int Id) and a value (here is Name public string Name)

Next convert a list of Student Class in a Data Table

  1.     // create DataTable
  2.       DataTable table = new DataTable();
  3.       table.Columns.Add("Id", typeof(Int32));
  4.       table.Columns.Add("Name", typeof(string));
  5.          // fill with info
  6.      foreach (Student student in students){
  7.           table.Rows.Add(student.Id, student.Name);
  8.      }

Other Query examples:

There are to way of write a LINQ Query
Query expression
and
Method Query

  1. // Query expression example
  2. var query = from r in customerDataTable.AsEnumerable()
  3.      where r.Field("LastName") == "Smith"
  4.      select r.Field(“FirstName”);
  1. // same example in Method query
  2. var query = customerDataTable.AsEnumerable()
  3.             .Where(dr => dr.Field("LastName") == "Smith")
  4.             .Select(dr => dr.Field("FirstName"));

Buy Visual studio:

.NET – LINQ in C#

LINQ (Language integrated Query)

  1. string[] numbers = { "0042", "010", "9", "27" };
  2. int[] nums = numbers.Select(s => Int32.Parse(s)).ToArray();
  3. foreach (var num in nums)
  4. Console.WriteLine(num);

What where LINQ is useful?

LINQ to Objects -

LINQ to XML -

LINQ to SQL -

LINQ to DataSet

  • Perform set operations on sequences of DataRow objects.
  • Retrieve and set DataColumn values
  • Obtain a LINQ standard IEnumerable sequence from a DataTable so Standard Query Operatorsmay be called.

LINQ to Entities -

101 LinQ Samples

What You can do with LINQ

  1. int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  2. // select var numsPlusOne =
  3. from n in numbers
  4. select n + 1;
  5. // where var lowNums =
  6.  
  7. from n in numbers
  8.        where n < 5
  9.        select n;
  10.  
  11. // groupby var numberGroups =
  12.        from n in numbers
  13.        group n by n % 5 into g
  14.        select new { Remainder = g.Key, Numbers = g };
  15.  
  16. // many select
  17.      int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
  18.      int[] numbersB = { 1, 3, 5, 7, 8 };
  19.      var pairs =
  20.          from a in numbersA
  21.                from b in numbersB
  22.                where a < b
  23.                select new {a, b};
  24.  
  25. // Take – first n elements.
  26.      int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  27.      var first3Numbers = numbers.Take(3);
  28.      Console.WriteLine("First 3 numbers:");
  29.      foreach (var n in first3Numbers) {
  30.          Console.WriteLine(n);
  31.      }        
  32.  
  33. // Skip – This sample uses Skip to get all but the first 4 elements of the array.
  34.      int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  35.      var allButFirst4Numbers = numbers.Skip(4);
  36.      var firstNumbersLessThan6 = numbers.TakeWhile(n => n > 6);        
  37.  
  38. // TakeWhile  (take 5, 4, 1 3)
  39.      int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  40.      var firstNumbersLessThan6 = numbers.TakeWhile(n => n > 6);      
  41.  
  42. // SkipWhile (allButFirst3Numbers  contain 3, 9, 8, 6, 7, 8, 0)
  43.      int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  44.      var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0);      
  45.  
  46. // OrderBy
  47.      string[] words = { "cherry", "apple", "blueberry" };
  48.      var sortedWords =
  49.           from w in words
  50.           orderby w   /* [ascending][descending] */
  51.           select w;  
  52.  
  53. /* ascending or descending are optional, if order direction is missing
  54. (ascending/descending) by default is sort ascending*/      
  55.  
  56. // Reverse
  57.      string[] digits = { "zero", "one", "two", "three", "four",
  58.                                "five", "six", "seven", "eight", "nine" };
  59.      var reversedIDigits = (
  60.      from d in digits select d)
  61.            .Reverse();      
  62.  
  63. /* exemple reverse digits list*/      
  64.  
  65. // DISTINCT
  66.     int[] factorsOf300 = { 2, 2, 3, 5, 5 };
  67.     var uniqueFactors = factorsOf300.Distinct();
  68. /* uniqueFactors become something like 2, 3, 5*/      
  69.  
  70. // UNION
  71.     int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
  72.     int[] numbersB = { 1, 3, 5, 7, 8 };
  73.     var uniqueNumbers = numbersA.Union(numbersB);
  74. /* append numbersB to numbersA and store it in uniqueNumbers  */      
  75.  
  76. // INTERSECT
  77.     int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
  78.     int[] numbersB = { 1, 3, 5, 7, 8 };
  79.     var commonNumbers = numbersA.Intersect(numbersB);      
  80.  
  81. // EXCEPT
  82.     int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
  83.     int[] numbersB = { 1, 3, 5, 7, 8 };
  84.     IEnumerable aOnlyNumbers = numbersA.Except(numbersB);    
  85.  
  86.  /* aOnlyNumbers contain just numbers who are in one list and not in the other */
  87.  

Buy Visual studio:

LINQ to SQL (C#)

Startup for LINQ to SQL with C#

This walkthrough requires the following:

This walkthrough uses a dedicated folder (“c:\linqtest6″) to hold files. Create this folder before you begin the walkthrough.

The Northwind sample database.

If you do not have this database on your development computer, you can download it from the Microsoft download site. For instructions, see Downloading Sample Databases (LINQ to SQL). After you have downloaded the database, copy the northwnd.mdf file to the c:\linqtest6 folder.

A C# code file generated from the Northwind database.

You can generate this file by using either the Object Relational Designer or the SQLMetal tool. This walkthrough was written by using the SQLMetal tool with the following command line:

sqlmetal /code:”c:\linqtest6\northwind.cs” /language:csharp “C:\linqtest6\northwnd.mdf” /pluralize

For more information, see Code Generation Tool (SqlMetal.exe).
Overview

This walkthrough consists of six main tasks:

Creating the LINQ to SQL solution in Visual Studio.

Adding the database code file to the project.

Creating a new customer object.

Modifying the contact name of a customer.

Deleting an order.

Submitting these changes to the Northwind database.
Creating a LINQ to SQL Solution

In this first task, you create a Visual Studio solution that contains the necessary references to build and run a LINQ to SQL project.
To create a LINQ to SQL solution

On the Visual Studio File menu, point to New, and then click Project.

In the Project types pane in the New Project dialog box, click Visual C#.

In the Templates pane, click Console Application.

In the Name box, type LinqDataManipulationApp.

In the Location box, verify where you want to store your project files.

Click OK.
Adding LINQ References and Directives

This walkthrough uses assemblies that might not be installed by default in your project. If System.Data.Linq is not listed as a reference in your project, add it, as explained in the following steps:
To add System.Data.Linq

In Solution Explorer, right-click References, and then click Add Reference.

In the Add Reference dialog box, click .NET, click the System.Data.Linq assembly, and then click OK.

The assembly is added to the project.

Add the following directives at the top of Program.cs:
C#

  1.  
  2. using System.Data.Linq;
  3. using System.Data.Linq.Mapping;

Adding the Northwind Code File to the Project

These steps assume that you have used the SQLMetal tool to generate a code file from the Northwind sample database. For more information, see the Prerequisites section earlier in this walkthrough.
To add the northwind code file to the project

On the Project menu, click Add Existing Item.

In the Add Existing Item dialog box, navigate to c:\linqtest6\northwind.cs, and then click Add.

The northwind.cs file is added to the project.
Setting Up the Database Connection

First, test your connection to the database. Note especially that the database, Northwnd, has no i character. If you generate errors in the next steps, review the northwind.cs file to determine how the Northwind partial class is spelled.
To set up and test the database connection

Type or paste the following code into the Main method in the Program class:
C#

  1.  
  2. // Use the following connection string.
  3. Northwnd db = new Northwnd(@”c:\linqtest6\northwnd.mdf");    
  4.  
  5. // Keep the console window open after activity stops.
  6. Console.ReadLine();

Press F5 to test the application at this point.

A Console window opens.

You can close the application by pressing Enter in the Console window, or by clicking Stop Debugging on the Visual Studio Debug menu.
Creating a New Entity

Creating a new entity is straightforward. You can create objects (such as Customer) by using the new keyword.

In this and the following sections, you are making changes only to the local cache. No changes are sent to the database until you call SubmitChanges toward the end of this walkthrough.
To add a new Customer entity object

Create a new Customer by adding the following code before Console.ReadLine(); in the Main method:
C#

  1.  
  2. // Create the new Customer object.
  3. Customer newCust = new Customer();
  4. newCust.CompanyName = "AdventureWorks Cafe";
  5. newCust.CustomerID = "ADVCA";    
  6.  
  7. // Add the customer to the Customers table.
  8. db.Customers.InsertOnSubmit(newCust);    
  9.  
  10. Console.WriteLine("\nCustomers matching CA before insert");    
  11.  
  12. foreach (var c in db.Customers.Where(cust => cust.CustomerID.Contains("CA")))
  13. {
  14. Console.WriteLine("{0}, {1}, {2}",
  15. c.CustomerID, c.CompanyName, c.Orders.Count);
  16. }

Press F5 to debug the solution.

Press Enter in the Console window to stop debugging and continue the walkthrough.
Updating an Entity

In the following steps, you will retrieve a Customer object and modify one of its properties.
To change the name of a Customer

Add the following code above Console.ReadLine();:
C#

  1.  
  2. // Query for specific customer.
  3. // First() returns one object rather than a collection.
  4. var existingCust =
  5. (from c in db.Customers
  6. where c.CustomerID == "ALFKI"
  7. select c)
  8. .First();    
  9.  
  10. // Change the contact name of the customer.
  11. existingCust.ContactName = "New Contact";

Deleting an Entity

Using the same customer object, you can delete the first order.

The following code demonstrates how to sever relationships between rows, and how to delete a row from the database. Add the following code before Console.ReadLine to see how objects can be deleted:
To delete a row

Add the following code just above Console.ReadLine();:
C#

  1.  
  2. // Access the first element in the Orders collection.
  3. Order ord0 = existingCust.Orders[0];    
  4.  
  5. // Access the first element in the OrderDetails collection.
  6. OrderDetail detail0 = ord0.OrderDetails[0];    
  7.  
  8. // Display the order to be deleted.
  9. Console.WriteLine
  10. ("The Order Detail to be deleted is: OrderID = {0}, ProductID = {1}",
  11. detail0.OrderID, detail0.ProductID);    
  12.  
  13. // Mark the Order Detail row for deletion from the database.

Submitting Changes to the Database

The final step required for creating, updating, and deleting objects, is to actually submit the changes to the database. Without this step, your changes are only local and will not appear in query results.
To submit changes to the database

Insert the following code just above Console.ReadLine:
C#

  1.  
  2. db.SubmitChanges();

Insert the following code (after SubmitChanges) to show the before and after effects of submitting the changes:
C#

  1.  
  2. Console.WriteLine("\nCustomers matching CA after update");
  3. foreach (var c in db.Customers.Where(cust =>
  4. cust.CustomerID.Contains("CA")))
  5. {
  6. Console.WriteLine("{0}, {1}, {2}",
  7. c.CustomerID, c.CompanyName, c.Orders.Count);
  8. }

Press F5 to debug the solution.

Press Enter in the Console window to close the application.