Saturday, December 11, 2010

atmega*8 Hardware Sleep-Wake Toggle

While working on some holiday projects, I needed on off switches for some JeeNode based widgets. It took me a while to get it working well, and I learned some stuff while doing it. Here is the software and a explanation of the hardware setup. Enjoy.


/*
SleepWakeToggle

SUMMARY
Toggles an atmega*8 state from running to lowest power sleep
mode using a hardware interrupt, e.g. a switch. Suitable for
Arduino, JeeNode, etc.

WHY
Battery powered devices last double-plus-much-longer if you power
the chip down when not in use.

HOW IT WORKS
Interrupts are a way of having your program flow altered by simple
real world events (e.g. passage of time or activation of switches).

This program demonstrates using a single switch to accomplish two
things: putting the chip in sleep mode and then waking it up. When
a switch is activated, an interrupt is triggered and program
control is shifted to a function you specify (a callback). Two
hardware interrupts are available INT0 and INT1.

Boldly rebinding INT0/INT1 callbacks to the wake and sleep functions
in the callbacks themselves is bad magic, and doesn't seem to work
:-). So the sleep function is called from loop() after a callback
sets a flag. This way the callback which triggers sleep (here:
'toggle') is done before 'void sleep()' actually runs. This is good
because, the interrupt has to be rebound to 'wake' just before going
in to sleep_mode.

HARDWARE SETUP
Lowest power sleep mode, SLEEP_MODE_PWR_DOWN, can only respond to a
pin going from high to low. Hence, tie desired pin (2 or 3) to +
with a resistor (i.e. a "pull-up" resistor), and connect a momentary
switch to ground and the pin.

SOFTWARE SETUP
You will want to provide functions that do something interesting and
that cause the system to be in your desired state before actually
powering down. These are: "void importantThing()" and "void
prepareToSleep()" below. The state that is achieved after power up
or reset is selected by setting the global flag buttonPushed in the
setup function.

Hereby placed in the public domain, December 2010
by Kael Fischer <kael.fischer@gmail.com>

*/

// hardware interrupt stuff
#include <avr/sleep.h>
#define INT0 0
#define INT0_PIN 2
#define INT1 1
#define INT1_PIN 3

// application specific stuff
#define BLINK_PIN 13

// global flags
int buttonPushed;

void importantThing(){
// the 'work' the program does
digitalWrite(BLINK_PIN, HIGH);
delay(1000);
digitalWrite(BLINK_PIN, LOW);
delay(1000);
}

void prepareToSleep(){
// When a button is pushed
// do some prep before sleeping
// to get in the desired state
digitalWrite(BLINK_PIN, LOW);
}

void wake(){

// This is the callback that runs when the
// chip is waking up.
buttonPushed=0;

// Can set variables and check stuff. Cannot do fancy stuff.
// Program flow returns to line after 'sleep_mode()' in sleep.

// No code is required here.
}

void toggle(){
// set a flag telling system to sleep on next loop iteration.
// this is the normal running state callback triggered by the
// switch.
detachInterrupt(1); // this seems to be allowed in callback
if (buttonPushed ==0){ // just in case, ignore switch if already
buttonPushed=1; // doing it
}
}

void sleep(){
prepareToSleep(); //application specific

// YOU ARE GETTING SLEEPY.....
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
attachInterrupt(INT1,wake,LOW); // rebind interrupt
sleep_mode(); // sleeping

// program returns here, after 'wake' callback

// now back to loop
}

void setup() {
// On Off Toggle setup
pinMode(INT1_PIN, INPUT); // set switch pin to input
// to limit current
attachInterrupt(INT1,toggle,LOW);
buttonPushed=1; // set to 1 = sleep after reset
// 0 = run loop after reset

// app specific setup here
pinMode(BLINK_PIN, OUTPUT);
}

void loop() {
if (buttonPushed == 1) {
sleep();
} else {
// reset button events to sleep
attachInterrupt(INT1,toggle,LOW);
}
// do important things
importantThing();
}

Sunday, December 5, 2010

Recent Icicles




The Dork Haus had some great icicles over Thanksgiving and a few days after. It was cold. Click through and check it out at higher resolution: Gallery link.

Thursday, September 30, 2010

Swingin' at Whole Foods?



Whole Foods (inc.) in California always seemed so uptight.

Monday, September 20, 2010

The Meat Eaters - NYTimes.com


Jeff McMahan says: : it would be instrumentally good if predatory animal species were to become extinct and be replaced by new herbivorous species. A very modest proposal, of course.

Sunday, July 11, 2010

black raspberry bounty

year 2 for the berries, and they are rocking. this am we got 37 more for a total of 98 so far.

Published with Blogger-droid v1.4.5

Friday, April 9, 2010

Atheist Barbie's going to do science! (stand back)


Atheist Barbie

BONUS Document Friday: Alexander Haig and the Wiretapping of American Citizens « UNREDACTED

Haig totally should have done prison time - for so many things.


Former Secretary of State Alexander M. Haig,who died last week at the age of 85, played a variety of roles in the military and the government, including combat duty in Korea and Vietnam, Army Chief of Staff, White House Chief of Staff, and Supreme Allied Commander Europe. It is worth remembering, however, that when he was military assistant to national security adviser Henry Kissinger and later his deputy, Haig played a central role in a wiretap scandal which was one of the abuses of power that brought down the presidency of Richard M. Nixon. Indeed, the wiretaps were an element of Article 2 of the articles of impeachmentadopted by the House Judiciary Committee on July 27, 1974.
...
When Haig made the formal request for the wiretaps, he told FBI assistant director William Sullivan that the request was “made on the highest authority” and that the matter was “so sensitive it demands handling on a need-to-know basis, with no record maintained.” That was not the way the FBI did business, especially when White House officials were issuing unprecedented requests to wiretap White House aides...


BONUS Document Friday: Alexander Haig and the Wiretapping of American Citizens « UNREDACTED

Sunday, April 4, 2010

How-To: Make Peeps sushi?

The latest from the the good people at Make:


How-To: Make Peeps sushi?:
peepshi-1.jpg


peepshi-2.jpg


Some Peeps, some Rice Krispies treats, and some fruit roll-ups, and you've got delicious 'Peepshi.' (Okay, I think I'll be sick now...)



How to Make Peepshi = Peeps Sushi



More:

Annual Parade o' Peeps



Read more | Permalink | Comments |



Read more articles in Holiday projects |


Digg this!"

Tuesday, March 9, 2010

Friday, March 5, 2010

Friday, February 26, 2010

PEP 278 -- Universal Newline Support

PEP 278 -- Universal Newline Support

Gadzooks! - python file.readline() is broken on Mac newlines - even when running on Macs!

Who knew! Use file(path,'rU') to open your file with "Universal Newlines"

Wednesday, February 24, 2010

Wednesday, February 17, 2010

Tuesday, February 16, 2010

Women's 10k pursiut

Neuner was my hero as soon as I saw her in the sprint. You won't see it on NBC but you can watch the whole race. It's a good one.

Women's 10k, you know you want to.

Saturday, February 13, 2010

Clean Install Windows 7 With Upgrade Media

My XP installation got hosed at home. It would not boot and it was neither the MBR nor the partition's boot loader, so I was going to have to repair all the drivers. I took the opportunity to install Windows7 on a new RAID1 volume on 2 new 1TB Barracudas. I got the upgrade retail box which says "All editions of Windows XP and Vista qualify you to upgrade." It seemed safest to unplug the old drive during the install. But this let me unable to activate Windows7 even after I plugged in the old drive (to copy over the old files). It seems that Windows7 wants to see the old installation (not just the old install media, as in the past) while it is installing if you want to use an upgrade product key.

Here is the solution I used; very easy:

Clean Install Windows 7 With Upgrade Media and Product Key on Formatted or Empty Blank Hard Drive » My Digital Life

Workaround 3: Clean Install and Activate Windows 7 with MediaBootInstall Registry Hack

  1. After booting up, ensure that no Windows Updates pending task which requires a system reboot to finish installing.

    Tip: Normally, an orange-colored shield icon will be displayed next to Shutdown button in the Start Menu Power Button or at notification tray area if a restart is required.

    If reboot is required, restart the computer to allow any pending updates is installed.

  2. Run Registry Editor (RegEdit).
  3. Navigate to the following registry key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\OOBE

  4. In the right pane, change the value data for MediaBootInstall from 1 to0.

    MediaBootInstall Registry

    Alternatively, just download and execute MediaBootInstall.reg to merge the value into registry.

  5. Open an elevated command prompt as administrator.
  6. Run the command below to reset Windows 7 activation status:

    slmgr -rearm

    Rearm Windows

  7. Reboot the computer.
  8. Run the Activate Windows utility (accessible from bottom section ofControl Panel -> System and Security -> System, or type Activate Windows into Start Search), type in the upgrade product key and activate Windows.