1 (edited by Shasta McNasty 2010-01-26 16:00:20)

Topic: Read bend sensor in widget (Chumby One)

I'm trying to read the bend sensor (top button) on my Chumby One, in order to get an easy-to-use snooze button in Duane's custom alarm ring screen from this thread:  http://forum.chumby.com/viewtopic.php?id=4570

Here's the ActionScript that I'm using (the this.x.onRelease bits are Duane's code, the bend sensor-related stuff was shamelessly stolen from this thread: http://forum.chumby.com/viewtopic.php?id=4128)

_bent = _global.ASnative(5,25);
var getbend:Number;
var lastbend:Number;
var changestate:Boolean = true;

this.snoozeButton.onRelease = function() {
    this._parent._chumby_alarm.snoozeAlarm();
}

this.offButton.onRelease = function() {
    this._parent._chumby_alarm.stopAlarm();
}

this.offWithMusicButton.onRelease = function() {
    this._parent._chumby_alarm.stopAlarmWithMusic();
}

this.clearBackupButton.onRelease = function() {
    this._parent._chumby_alarm.resetBackupAlarm();
}

this.onEnterFrame = function() {

    getbend = _bent();
    
    if (getbend != lastbend) {
        changestate = true;
    }
        
    if ((getbend == 0) && changestate) {
        changestate = false;
    }

    if ((getbend == 1) && changestate) {
        this._parent._chumby_alarm.snoozeAlarm();        
        changestate = false;
    }
    
    lastbend = _bent();
}

All of Duane's on-screen buttons work right (of course!) but the top button doesn't do anything on either press or release.

I'm targeting FlashLite 3/ActionScript 2 in Adobe Flash CS4 (v10).  Any hints on what I'm doing wrong?  I don't know a ton of Flash (as you might guess from my cargo-cult programming technique!), but I do write field-programmable gate array firmware code in VHDL for a living, and this looks like it should do what I want (i.e. track changes in the state of the bend sensor, and when it is depressed, snooze the alarm).  Do I need to include a library or something?  Or is there just a bug in my code that I don't see?  I have the frame rate set to 12 fps, btw. 

Thanks for any help anybody can give me.

Re: Read bend sensor in widget (Chumby One)

On the chumby one, the top button also generates a key event - try adding an object as a listener to the Key class, and make onKeyUp and onKeyDown handlers and see what you get.

Re: Read bend sensor in widget (Chumby One)

OK, I just tried this which is a zillion times cleaner-looking: 

myListener = new Object();

Key.addListener(myListener);

myListener.onKeyDown = function () {
    this._parent._chumby_alarm.snoozeAlarm();
}

this.snoozeButton.onRelease = function() {
    this._parent._chumby_alarm.snoozeAlarm();
}

this.offButton.onRelease = function() {
    this._parent._chumby_alarm.stopAlarm();
}

this.offWithMusicButton.onRelease = function() {
    this._parent._chumby_alarm.stopAlarmWithMusic();
}

this.clearBackupButton.onRelease = function() {
    this._parent._chumby_alarm.resetBackupAlarm();
}

No joy. 

When I tried in in Device Central (replacing the onKeyDown action with a trace statement) I saw the trace show up in the Device Output window (but that was by hitting a key on the keyboard...is there a Chumby profile for Device Central?) so the code is basically OK. 

Still wondering if I'm missing some library include in my ActionScript...the above code section is the entirety of the ActionScript in my .fla file.  Or did I set my compiler options wrong...I'm targeting FlashLite 3 and ActionScript 2.  Like I said, I'm a Flash n00b and I could be doing something really dumb...

I can post the whole .fla file if you want, but it's just your .fla file with my updated ActionScript. 

Thanks for all your help so far, and especially for the initial .fla file in response to my request for this functionality in the other thread.  It's great to have the music keep going!  Hitting the on-screen snooze is no problem, I'm mostly trying to get the top button to work as a learning exercise.

Re: Read bend sensor in widget (Chumby One)

The problem is that "myListener" is an Object, not a MovieClip, therefore it doesn't have a "_parent", unless you create the link yourself.

Try adding "myListener._parent = this;" just after you create it.

Re: Read bend sensor in widget (Chumby One)

Yup, that gets me most of the way there!  Like I said, I'm a Flash n00b.  The kind of programming I do normally is at a much lower (non-object) level!

Still looks like I have an issue with maybe overloading the button though.  Hitting the top bar snoozes the alarm but has other side effects.  If my starting condition is with the Chumby in Night Mode, hitting the on-screen snooze snoozes the alarm and returns the Chumby to Night Mode (which is good).  But hitting the top button snoozes the alarm and switches to the Control Panel instead of returning to Night Mode.

I don't have any handler for onKeyUp, I'll try adding that back in to see if it changes anything. 

Thanks again for the help!

Re: Read bend sensor in widget (Chumby One)

I switched the snooze to the onKeyUp handler, that works!  Hooray, I actually wrote (*) a Chumby widget.

(*) by "wrote" I mean "stole all the code from this forum and Duane told how to patch it together"...thanks again Duane!

Re: Read bend sensor in widget (Chumby One)

OK, just one more thing - each time this screen is launched, it will create a "myListener" Object and attach it to the Key Object.  This leaks memory, since the Object can't be garbage-collected as long as it remains in the listener queue.

To address this, what you should probably do is something like:

myListener.onKeyDown = function () {
    Key.removeListener(this._parent.myListener); // add this line in each of the handlers
    this._parent._chumby_alarm.snoozeAlarm();
}

You might also be able to do something using the onUnload() handler, but it can sometimes be unpredictable.

Re: Read bend sensor in widget (Chumby One)

Thanks again, will do.  This object stuff makes my head all explode-y, but I get what you are saying about the memory leak.  Fortunately right now I'm only leaking memory once (or twice if I really can't get out of bed!) a day...

Re: Read bend sensor in widget (Chumby One)

Oh cool, this thread was exactly what I needed to find.

This probably substantially duplicates what Shasta was working on, but since s/he didn't post them:

Improved Alarm Screen: FLA SWF

Notable things that I found to be helpful:

- nice big "snooze" target for those of us who don't sleep with our glasses on smile
- hitting snooze automatically resets the backup alarm
- hitting the bend switch will snooze and also reset the backup alarm
- slightly nicer colors smile

The only problem right now is that even using the KeyUp handler to snooze, I'm still seeing the problem that hitting the bend button both snoozes and brings up the control panel, rather than simply returning to the last-playing widget.  Suggestions?