Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, 11 April 2011

WP7 Tutorial - part 5: Orientation and Acceleration - X,Y,Z

Detecting gestures, orientations, and movement can be realized with the accelerometer. The accelerometer is a sensor that measures the acceleration in 3 dimensions (X, Y, and Z). If the device is not moved the accelerations measure are the gravity forces in each direction. If the device is accelerated the measured results are a combination of the acceleration and the gravity.

The accelerometer data can be accessed via the AccelerometerReadingEventArgs class. The class has values for the X, Y, and Z axis. The values are of type double and between -2 and 2 which related to acceleration "for each axis in gravitational units" - 1 is the gravitational force of the earth. See: http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx and http://msdn.microsoft.com/en-us/library/ff431810(v=vs.92).aspx or on page 80ff, C. Petzold, Programming Windows Phone 7.

A typical exercise for understanding the accelerometer is to create a bubble level (a tool to measure if something is horizontal or vertical - e.g. for hanging pictures on the wall). You probably want to freshen up on arctan2 - at least I needed ;-)

See below the c# example reading out the accelerometer on a windows phone 7. You can also download the accelerometer project directory in a single ZIP-file.

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;

// A simple example to read the accelerometer and display the values
// In order to make it work you have to add the refercerence to 
// Microsoft.Devices.Sensors to your project. To do this right-click
// in the Solution Explorer on References and than choose add Reference
// in the dialog then select Microsoft.Devices.Sensors
// Albrecht Schmidt, University of Stuttgart

// for a more comprehensive example see: 
// http://msdn.microsoft.com/en-us/library/ff431810(v=vs.92).aspx
// http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx
// and page 80ff, C. Petzold, Programming Windows Phone 7

namespace Accl_X_Y_Z
{
    public partial class MainPage : PhoneApplicationPage
    {
        Accelerometer accelerometer;
    
        public MainPage()
        {
            InitializeComponent();
            // create a new instance
            accelerometer = new Accelerometer();
            // register a callback function for when values change
            accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged);
            // start the accelerometer
            accelerometer.Start();
        }

        void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            // required as from here the textBlocks cannot be accessed
            Deployment.Current.Dispatcher.BeginInvoke(() => ChangeUI(e));
        }
        
        void ChangeUI(AccelerometerReadingEventArgs e)
        {
            // show the values on the screen
            textBlock1.Text = "X: " + e.X.ToString("0.000");
            textBlock2.Text = "Y: " + e.Y.ToString("0.000");
            textBlock3.Text = "Z: " + e.Z.ToString("0.000");
        }
    }
}

WP7 Tutorial - part 4: Storing Data on the Phone

If you want to save high scores or preferences you need persistent memory on the phone. On a traditional computer you would create a file and store your information in the file; another option on a Windows PC would be store such information in the registry. For security reasons there is no API to access the file system and there is no global persistent memory across applications on a WP7.

In general there are two ways to store data: (1) in a application specific storage (isolated storage) on the phone or (2) remotely on the internet (or to use another buzzword "in the cloud").
In this example the use of the phone isolated storage API is demonstrated. It is shown how to store and retrieve name-value pairs on the phone (to people who programmed Java ME this is conceptually similar to the record store).

For more details see: http://msdn.microsoft.com/en-us/library/cc221360(v=VS.95).aspx and page 126ff, C. Petzold, Programming Windows Phone 7. It is also possible to created an isolated storage for files see: http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(v=VS.95).aspx

See below the c# example using the local storage on a windows phone 7. You can also download the IsolatedStorageSettings project directory in a single ZIP-file.

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

// example of how to save to and load from the isolated application storage 
// this helps to create persistence storage within a single application
// in this example it is shown how to do it for a string 
// Albrecht Schmidt, University of Stuttgart  


// For more details see:
// http://msdn.microsoft.com/en-us/library/cc221360(v=VS.95).aspx
// page 126ff, C. Petzold, Programming Windows Phone 7

// storing files/directory structures see:
// http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(v=VS.95).aspx

namespace PhoneStorage
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        #region Save and Load Parameters from the Application Storage
        void saveToAppStorage(String ParameterName, String ParameterValue)
        {
            // use mySettings to access the Apps Storage
            IsolatedStorageSettings mySettings = IsolatedStorageSettings.ApplicationSettings;

            // check if the paramter is already stored
            if (mySettings.Contains(ParameterName))
            {
                // if parameter exists write the new value
                mySettings[ParameterName] = ParameterValue;
            }
            else
            {
                // if parameter does not exist create it
                mySettings.Add(ParameterName, ParameterValue);
            }
        }

        String loadFromAppStorage(String ParameterName)
        {
            String returnValue = "_notSet_";
            // use mySettings to access the Apps Storage
            IsolatedStorageSettings mySettings = IsolatedStorageSettings.ApplicationSettings;

            // check if the paramter exists
            if (mySettings.Contains(ParameterName))
            {
                // if parameter exists write the new value
                mySettings.TryGetValue<String>(ParameterName, out returnValue);
                // alternatively the following statement can be used:
                // returnValue = (String)mySettings[ParameterName];
            }

            return returnValue;
        }
        #endregion

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            saveToAppStorage("myIdentifer1", "Last used @ " + System.DateTime.Now.ToString("HH:mm:ss"));
            textBox1.Text = "saved...";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = loadFromAppStorage("myIdentifer1");
        }

    }
}

Friday, 25 March 2011

WP7 Tutorial - part 3: Using Location

In this example the use of the location API is demonstrated. The API is a high level interface to geo location. How the location is determined (e.g. GPS, GSM cell information) is of no concern to the developer.

The basic approach is to create an instance of GeoCoordinateWatcher and register two callback functions: one for when the status changes and one for when the location changes. The program demonstrates how these call backs are set up and how from within those function the user interface is updated with the received information. If the status is changes, the program checks what the current status is, and shows this in the status line (textBlock8.Text). If the position is changed then the new position information (Position.Location.Longitude, Position.Location.Latitude) - and additional information such as Speed, Altitude, Course, Accuracy are shown.

As an exercise you can build an application that shows you how close you are to a given target. In two input fields you enter the longitude and latitude of the destination (e.g. a geo cache location). And then you can calculate the difference from the current position to the target location and visualize or sonify the distance.

There is another example (Geo coordinate watcher) how to use this API on the Microsoft msdn website. In C. Petzold's book there is also a good example, see page 91ff.

See below the c# example using geo location on a windows phone 7. You can also download the geolocation project directory in a single ZIP-file.

using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Device;
using System.Device.Location;

// the example shows the basic functionality of the location device
// you need to add in the solution explorer a reference to System.Device
// right click on References in the solution explorer, click Add Reference, and then
// System.Device
// Albrecht Schmidt, University of Stuttgart

// for a more comprehensive example see:
// http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinatewatcher.aspx
// http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx
// and page 91ff, C. Petzold, Programming Windows Phone 7

namespace Geo_Location
{
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher;

// Constructor
public MainPage()
{
InitializeComponent();
}

// the initialize and start button is pressed
private void button1_Click(object sender, RoutedEventArgs e)
{
// initialize the geo watcher with defaul accuracy (battery saving)
// user GeoPositionAccuracy.High for higher accuracy
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
// set movement threhold - as distance in meters - default is 0
watcher.MovementThreshold = 10;

// add a handler that is called when position is changed more than MovementThreshold
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
// a handler for status change
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);

// Start reading location data
watcher.Start();
}

void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
// you cannot change the UI in this function -> you have to call the UI Thread
Deployment.Current.Dispatcher.BeginInvoke(() => ChangeStatusUI(e));
}

void ChangeStatusUI(GeoPositionStatusChangedEventArgs e)
{
String statusType="";
if ((e.Status) == GeoPositionStatus.Disabled)
{
statusType = "GeoPositionStatus.Disabled";
}
if ((e.Status) == GeoPositionStatus.Initializing)
{
statusType = "GeoPositionStatus.Initializing";
}
if ((e.Status) == GeoPositionStatus.NoData)
{
statusType = "GeoPositionStatus.NoData";
}
if ((e.Status) == GeoPositionStatus.Ready)
{
statusType = "GeoPositionStatus.Ready";
}
textBlock8.Text = statusType;
}

void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// you cannot change the UI in this function -> you have to call the UI Thread
Deployment.Current.Dispatcher.BeginInvoke(() => ChangeUI(e));
}

void ChangeUI(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
textBlock1.Text = "Longitute: " + e.Position.Location.Longitude;
textBlock2.Text = "Latitute: " + e.Position.Location.Latitude;
textBlock3.Text = "Speed: " + e.Position.Location.Speed;
textBlock4.Text = "Altitude: " + e.Position.Location.Altitude;
textBlock5.Text = "Course: " + e.Position.Location.Course;
textBlock6.Text = "Vertical Accuracy: " + e.Position.Location.VerticalAccuracy;
textBlock7.Text = "Horizontal Accuracy: " + e.Position.Location.HorizontalAccuracy;
textBlock8.Text = "location updated at " + System.DateTime.Now.ToString("HH:mm:ss");
}

// the stop button clicked ... stop the watcher
private void button2_Click(object sender, RoutedEventArgs e)
{
if (watcher != null) { watcher.Stop(); }
textBlock8.Text = "location reading stopped";
}
}
}

Saturday, 19 March 2011

WP7 Tutorial - part 2: Vibration

This examples shows how to activate the vibration motor / vibration actuator in the phone. The calls Microsoft.Devices.VibrateController.Default.Start and Microsoft.Devices.VibrateController.Default.Stop are used to switch the actuator on and off.
When switching the vibration on the parameter sets the duration for which it should be on. The duration is between 0 and 5 seconds. With the function TimeSpan.FromMilliseconds(duration), where duration is a number, the parameter can be set easily.

The standard API only supports to switch on and off the vibration. We experimenting with the code you can explore how to have vibrations of different intensity. To do this you have to switch on and off the vibration (e.g. 100 ms on then 50 ms off) - basically doing pulse-width modulation.

There is more information on the vibration controller on the Microsoft site.

See below the c# example for controling the vibration a windows phone 7.
You can also download the vibration project directory in a single ZIP-file.

using System;
using System.Windows;
using Microsoft.Phone.Controls;

// example of how switch on the vibration motor for a given time
// another call to switch it off
// Albrecht Schmidt, University of Stuttgart

// see:
// http://msdn.microsoft.com/en-us/library/microsoft.devices.vibratecontroller.default(v=VS.92).aspx


namespace Vibration
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
double duration;
duration = Convert.ToDouble(textBox1.Text);
if (duration > 5000)
{
duration = 5000;
}
// starts the vibrations (valid duration are between 0 and 5 seconds)
Microsoft.Devices.VibrateController.Default.Start(TimeSpan.FromMilliseconds(duration));
}

private void button2_Click(object sender, RoutedEventArgs e)
{
Microsoft.Devices.VibrateController.Default.Stop();
}
}
}

Friday, 18 March 2011

WP7 Tutorial - part 1: Phone calls and SMS

Over the next weeks I like to share some examples I have created while learning to program the Windows Phone 7 platform. The tutorial is mainly explaining some of the APIs and components I found interesting (in particular related to context-awareness and human computer interaction).

If you are really new to programming on this platform the App-Hub is probably a good place to start. There are also plenty of helpful examples on the Microsoft web page. As prerequisite for this tutorial I assume that you have successfully installed Visual Studio (Express), the windows phone development tools, and that you have managed to get your first "Hello World" written, compiled, and deployed. There is also plenty of material on youtube that helps to get started. The Programming Windows Phone 7 book by Charles Petzold (free available as PDF) is also a good starting point.

Several examples that follow in the tutorial (e.g. vibration, accelerometer) having a real phone to test the programs is highly recommended.

The Windows Phone 7 is a phone and hence I start with a program that makes use of the basic phone functionality: making a phone call and sending an SMS.

This example also highlights the approach taken in several of the APIs. Your program (a third party application) is restricted to transfers control to the phones basic functions when making a call or sending an SMS. Basically the API call opens the phone/SMS applications with the parameters you hand over. From a security point of view this is nice as an application can not send SMS or do phone calls without the user recognizing (and agreeing to) it. From a programming perspective this has disadvantages as automation of functionality (e.g. always sent an SMS when I am coming closer than 10 km to home) is not possible.

The example demonstrates how to access interactively the phone book (using the PhoneNumberChooserTask), how to initiate a phone call task (using Microsoft.Phone.Tasks.PhoneCallTask), and how to initiate a SMS task (using Microsoft.Phone.Tasks.SmsComposeTask).
The PhoneNumberChooserTask is an example of a callback - used a lot when programming for WP7. The basic concept is to register a function that is called when an event happens. In this case the event is that the number is chosen and then the function myPhoneNumberChooser_Completed is called. The nice thing with Visual Studio is that you do not have to type this (or remember the syntax) - just use the TAB-key after you typed +=

See below the c# example for sending SMS and making a phone call. You can also download the Phone/SMS project directory in a single ZIP-file.

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

// example how to invoke a phone call and an SMS from a program
// the basic approach is that you can set the phone number (and
// for SMS the message body) and then you call the phone/sms
// application
// Albrecht Schmidt, University of Stuttgart
// more on how to invoke phone tasks (photo, email, ...)
// http://msdn.microsoft.com/en-us/library/ff769543(v=vs.92).aspx

namespace Phone_Call_and_SMS
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

// choose a phone number
private void button1_Click(object sender, RoutedEventArgs e)
{
PhoneNumberChooserTask myPhoneNumberChooser;
myPhoneNumberChooser = new PhoneNumberChooserTask();
myPhoneNumberChooser.Completed += new EventHandler&lt;PhoneNumberResult&gt;(myPhoneNumberChooser_Completed);
myPhoneNumberChooser.Show();
}

// call back after the phone number is choosen
void myPhoneNumberChooser_Completed(object sender, PhoneNumberResult e)
{
if (e.TaskResult == TaskResult.OK)
{
textBox1.Text = e.PhoneNumber.ToString();
}
}

// making a phone call (opening the sms application with presets)
private void button2_Click(object sender, RoutedEventArgs e)
{
Microsoft.Phone.Tasks.PhoneCallTask phonecall = new Microsoft.Phone.Tasks.PhoneCallTask();
phonecall.PhoneNumber = textBox1.Text; // set phone number
phonecall.Show();
}

// sending an sms (opening the sms application with presets)
private void button3_Click(object sender, RoutedEventArgs e)
{
Microsoft.Phone.Tasks.SmsComposeTask sms = new Microsoft.Phone.Tasks.SmsComposeTask();
sms.To = textBox1.Text; // set phone number
sms.Body = textBox2.Text; // set body
sms.Show();
}
}
}

Tuesday, 23 February 2010

More on Embedded Development Tools, Watch to program

In my digital systems design class we have used the eZ430-F2013 development kits - they are cheap (about 20 €) and most students get it working :-)

I have seen TI has a new development kit which is really interesting: "eZ430-Chronos Wireless Watch Development Tool" - Could be an useful basis for wearable computing projects - perhaps we should get them for the class on pervasive computing next term?

The TI commercial is on youtube:
http://www.youtube.com/watch?v=LDJIBydJvoM

Some years back IBM had a Linux watch as research platform. Gábor Blaskó published some concepts and protoytpes on how to interact with a watch computer, e.g. with strokes [1] or with a string [2].

[1] Gábor Blaskó and Steven Feiner. An Interaction System for Watch Computers Using Tactile Guidance and Bidirectional Segmented Strokes. Proc. 8th IEEE International Symposium on Wearable Computers (ISWC 2004), Arlington, VA, USA, 31 October - 3 November, 2004. pp.120-123.

[2] Gábor Blaskó, Chandra Narayanaswami, Steven Feiner. Prototyping Retractable String-Based Interaction Techniques for Dual-Display Mobile Devices. Proc. ACM Conference on Human Factors in Computing Systems (CHI 2006), Montreal, Québec, Canada, 22 - 27 April, 2006. pp.369-372.

Wednesday, 1 July 2009

DIY Segway - to motivate embedded programming?

Elektor magazine features in the current issue a DIY Segway called Elektor Wheelie. The system is build around a ATMEGA32 and has obviously a lot of mechanics. For sensing an ADXL320 (acceleration sensor) and an IDG-300 (gyro) are used. Looks like a fun project - and you have full access to the software (not sure what you really would program differently, perhaps one can tune it to get faster ;-)

Perhaps it could be a platform to motive embedded programming - with clear real-time constraints, as it hurts if you fall off… Next term we are teaching digital system design and programming of microcontroller systems - should we get one for the lab? Someone willing to built it?

Thursday, 8 May 2008

A service for true random numbers

After the exam board meeting at Trinity College in Dublin (I am external examiner for the Ubicomp program) I went back with Mads Haahr (the course director) to his office. Besides the screen on which he works he has one extra where constantly the log entries of his web server is displayed. It is an interesting awareness devices ;-) some years ago we did a project where we used the IP-address of incoming HTTP-requests to guess who the visitors are and to show their web pages on an awareness display [1], [2]. Looking back at web visitors works very well in an academic context and with request out of larger companies where one can expect that information is available on the web. Perhaps we should revisit the work and look how we can push this further given the new possibilities in the web.

The web-server Mads has in his office is pretty cool – it provides true random numbers – based on atmospheric noise picked up with 3 real radios (I saw them)! Have a look at the service for yourself: www.random.org. It provides an HTTP interface to use those numbers in your own applications. I would not have though of a web service to provide random numbers – but thinking a little more it makes a lot of sense…

[1] Schmidt, A. and Gellersen, H. 2001. Visitor awareness in the web. In Proceedings of the 10th international Conference on World Wide Web (Hong Kong, Hong Kong, May 01 - 05, 2001). WWW '01. ACM, New York, NY, 745-753. DOI= http://doi.acm.org/10.1145/371920.372194

[2] Gellersen, H. and Schmidt, A. 2002. Look who's visiting: supporting visitor awareness in the web. Int. J. Hum.-Comput. Stud. 56, 1 (Jan. 2002), 25-46. DOI= http://dx.doi.org/10.1006/ijhc.2001.0514

Thursday, 24 April 2008

Teacher Training: mobile phone programming in JAVA

Yesterday evening we organized a teacher training session on JAVA programming for mobile phone. We introduced them to the basics of Java ME and gave a tutorial on using Netbeans with mobility pack for creating applications. One idea for using the phone as an application platform is to increase motivation.

In some of the discussions we came across the issue of “how to teach programming” in school. I personally thing that explaining and learning the basic concepts of statement, variable, if-then, and loop is best done with not much around… I showed some of the activities on the OLPC for programming: Pippy (http://wiki.laptop.org/go/Pippy) and Turtle Art (http://wiki.laptop.org/go/Turtle_Art) and we talked about processing (http://www.processing.org/) as a further option.

Monday, 10 December 2007

Prototypes of unconventional user interfaces

In Linz the students of the course unconventional user interfaces showed their first set of functional prototypes. The topics are related to interactive mirrors, context-aware advertisement posters, healthy rear-seat entertainment, and text input while driving. The assignment was to create a system that allows creating an authentic user experience for the concept. The technical solutions were very different and ranged from a dismantled keyboard to a system using a micro-controller, from a two-way mirror with display behind to direction detection in front of an advert. Even though the prototypes were fairly simple most of them showed impressively how much of an idea a functional prototype can transport.

Friday, 24 August 2007

Great tutorial on eXtreme Programming/Agile Methods

Today Karl-Heinz Sylla and Reinhard Budde (both senior researcher at Fraunhofer IAIS) gave for the summer research project a tutorial on agile methods for software engineering. The experience they have from large scale real world projects is impressive! We looked at different approaches to software construction and discussed the pros and cons. Short iterations, user stories, pair programming and test driven development seem to fit very well to our work approach and project goals. A good starting point for more on the topic in particular with a teaching perspective are the following 2 papers: LeJeune, N. F. 2006. Teaching software engineering practices with Extreme Programming. J. Comput. Small Coll. 21, 3 (Feb. 2006), 107-117 and Schneider, J. and Johnston, L. 2003. eXtreme Programming at universities: an educational perspective. In Proceedings of the 25th international Conference on Software Engineering (Portland, Oregon, May 03 - 10, 2003). International Conference on Software Engineering. IEEE Computer Society, Washington, DC, 594-599.

From a user interface engineering perspective is very positive that agile methods are good to integrate with user centred design – in my experience much better than traditional software construction processes. Especially the fact that XP (eXtreme Programming) is open to change in functionally throughout the process is a key.

In this summer research project one great challenge is that the students have to build up knowledge in various areas (e.g. search technologies, web technology, user interfaces) while creating high quality code. There is a very interesting paper that discusses software engineering issues in the context of web applications: Jazayeri, M. 2007. Some Trends in Web Application Development. In 2007 Future of Software Engineering (May 23 - 25, 2007). International Conference on Software Engineering. IEEE Computer Society, Washington, DC, 199-213.

Always when the discussion comes to programming languages a debate on strong typing starts. Especially in the web context this seems come up again and again…