Atmel Studio: Clearing debugWIRE

debugWIRE is a fantastic bit of technology in many recent AVR chips, allowing you to set breakpoints, step through your code, and a bunch of other useful features. All you need is a programmer that supports it – the AVR Dragon for instance.

In the Atmel Studio project, simply set the Debugger/Programmer Interface to debugWIRE.

The only fly in the ointment is that for extremely low-power applications, having the DWEN fuse and thus debugWIRE enabled will prevent the chip from fully sleeping, meaning your device is much more thirsty than expected.

So you set the Debugger/Programmer Interface back to ISP, and try to program your device again; Failure! In fact; CRYPTIC ERROR MESSAGE!

TO CLEAR debugWIRE FROM A CHIP WITHIN Atmel Studio 6:
Start a debug session on the chip, then
Go to the “Debug” menu and select “Disable debugWIRE and close”.

Now you can access the chip using ordinary ISP, and low-power devices will be more frugal.

This may seem an obvious thing, but there are a number of “solutions” floating around, including people advocating hooking HVPP programmers into devices simply to clear DWEN!

More Javascript Templates Using ‘src’

There was some concern about my previous solution, that it loads templates synchronously and serially. An application should have templates inlined before it goes to production anyway.

Fair point though, there is a better way.

Better Definition

Firstly; Marionette.js can transparently lazy-load and cache DOM-stored templates;

//What I had been doing
var MyView1 = Marionette.ItemView.extend({
	template: _.template( $("#template_myview1").html() )
});

//What I should have been doing
var MyView2 = Marionette.ItemView.extend({
	template: "#template_myview2"
});

The difference between the two is significant.

  • MyView1 will access the DOM as soon as the class is defined, and build the template immediately.
  • MyView2 will access the DOM only when a MyView2 instance is constructed, but only builds the template once.

This means the templates are only built at the time they are first used. The template blocks don’t need to be accessible at the time the classes are defined. The app only needs to build the template functions the first time they are used.

Using the MyView2 style will give much faster start-up times for larger applications.

(Marionette.TemplateCache can be used stand-alone, or you can fairly easily create your own caching DOM template loader)

Better Loading

Now, the template blocks won’t be referenced until they are used. We can load them asynchronously now, but we still don’t want to start the app until the templates have been fetched. $.ready(…) won’t wait for the templates, but there’s a fairly easy workaround;

//A list of promises that need to resolve before starting
var loaded = [];

//Load all templates
var $templates = $('script[type="text/template"]');
$templates.each(function() {
	var src = $(this).attr("src");
	if (src) {
		loaded.push( //Wait for the template to load
			$.ajax(src, {context:this}).
			done(function(data) {
				$(this).html(data);
			})
		);
	}
});

//Wait for the DOM to be ready
loaded.push($.ready);

//Initialise after everything is loaded
$.when.apply($,loaded).done(function() {
	//Start application
});

There! With that in place;

  • Templates are loaded into script blocks as you’d expect, asynchronously and in parallel.
  • The application starts after the DOM and the templates are loaded.
  • For production, the templates are inlined. Without any code changes, the application will transparently start as soon as $.ready is completed.

Javascript Templates Using ‘src’

Background

John Resig was the first to document a technique for storing templates in <script> tags;

<script type="text/html" id="item_template">
   <li><b>{{ name }}</b> - {{ description }}</li>
</script>

This works well for several reasons;

  1. Using the unknown type “text/html” means that the browser ignores it.
  2. Giving the tag an id allows us to easily grab its contents;
//Build a new item, and add it to the list
//Using JQuery, Underscore.js
var contents = _.template(
  $("#item_template").html(),
  {name: "Foo", description:"Sir Foo the Second"}
);
$list.append( $(contents) );

Today there are many libraries available for retrieving and building templates in this way, such as Mustache.js and ICanHaz.js, and more that integrate support.

Problem

One of the only problems with this technique, is that the scripts must be declared inline.

<!-- Works! -->
<script type="text/html" id="item_template">
   <li><b>{{ name }}</b> - {{ description }}</li>
</script>

<!-- Doesn't work. -->
<script type="text/html" id="item_template2" 
                  src="templates/item2.html"></script>

If the browser doesn’t understand the type, it won’t load the source. Whatever the reason, the template text is never added to the DOM.

Inlining template contents is detrimental to readability, maintainability, and traceability.

Some libraries offer asynchronous methods for loading templates. (require.js among them) Some have issues with complexity and performance.

It feels natural to define and compile the template functions at class definition time. Other solutions may add unnecessary layers of indirection.

Solution

I’ll preface this by saying “Okay, maybe this won’t work for you”, but so far it’s working well, transparently, and fast. And when you go to production, a decent deployment optimisation process will inline the templates anyway.

What if the src attributes were read properly?

<script type="text/html" id="template_widgets"
                    src="templates/widgets.html"></script>
<script type="text/html" id="template_widgets_item 
                    src="templates/widgets_item.html"></script>
...
...
<script type="text/javascript">
  //Synchronously load the templates
  var $templates = $('script[type="text/html"]');
  $templates.each(function() { 
    //Only get non-inline templates
    //No need to change code for production.
    if ($(this).attr("src")) { 
      $.ajax(
        $(this).attr("src"), 
        {
          async:false, 
          context:this, 
          success:function(data) { $(this).html(data); }
        }
      );
    }
  });
  //From this point onwards (in the same script, in the 
  //following scripts) all the "text/html" script tags 
  //will be loaded as if they were inline.
</script>

Just insert this code after all your template tags, and before you build your template functions, and you’ll be able to treat them as if they were inline all along.

Easy.* 🙂

*There are some downsides to loading template serially and synchronously. I’ve written a follow-up article that discusses a technically better solution.

Finished: Display Case Doors

IKEA, your ‘professional assembly service’ sucks. My grandmother’s display case doors have scraped since the day it was installed, and the repeated twisting caused the glue to fail on one of them.

  • Glued back together the broken door.
  • Planed a few millimetres off the bottom of both doors.
  • Smooth, silent doors that no longer stick.
  • Happy grandmother!

Scribing Robot Arm

Development is finally finished, and the campaign has gone live! (And I can talk about it!)

http://www.themostpowerfularm.com/

It’s for a great cause – petitioning the Australian Government to help fund research into Duchenne Muscular Dystrophy.

Robot Arm View

The arm in its natural habitat – the RoboDino workshop.

I designed and scratch-built this SCARA-type anatomical robot arm for agencies Finch and Havas, for use in their appeal campaign.

  • Stands approximately 350mm tall, with a reach of about 500mm.
  • Built from OpenBeam extrusion and laser-cut bamboo.
  • Hardware control by an Arduino running grbl with a grblShield v4, and a Micro Maestro servo controller
  • Software control by custom software to handle inverse kinematics, font rendering, sequencing
  • Queuing service to reliably sequence and write names from campaign site.

The robot arm can draw more than just text, see?

http://www.youtube.com/watch?v=B-EFDt3eXv4

Each short segment in the Hilbert Curve is approximately 1.2mm long! It could draw smaller, but the width of the ink becomes a problem!

A build log is not available yet, but there’s lots of info at the campaign link above, and lots more photos here.

Jacob Lancaster’s appeal to the Prime Minister

Jacob and the team

If you’re in Sydney, you can come and see it in person! It’s at Customs House, Circular Quay, for the next two weeks.

Finished: Bubble Wrap Dispenser

This one slipped through the cracks – a Bubble Wrap Dispenser!

IMG_20121222_170702[1]

IMG_20121222_170711[1]

Mostly pretty simple to make;

  • The box is a gift box from the $2 shop, with a slot cut in the lid and fancy tape applied.
  • The logos are some quick work in Inkscape, printed and cut from glossy paper.
  • The bubble wrap…

…was perforated on the laser cutter! I made a dashed line file, and cut the ~5m length of bubble wrap with littls perforations – just pull and tear, and you get a square of bubble wrap.
Worked great, and was really well received. 🙂

Finished: Garden Box

Finished another thing! And in miserable weather, too.
Thanks to my wife for doing all the planting, and to my mum for helping with construction and supplying much of the materials as a ‘lots of assembly required’ Christmas present.
It’s a BIG balcony-mounted garden box.

image

Herbs, lettuces, and a chilli plant. All set and ready to grow.

image

Sturdily anchored to the support posts, let’s hope it won’t fall down.

image

1.9 m wide, and built out of treated pine. Note the straps underneath to stop the planters from falling through. (Nobody wants that landing on their head) Also, the EIGHT retaining straps (3mm gal steel) anchoring it to the wall.

image

They’re enjoying the otherwise miserable weather we’re having!