Products for USB Sensing and Control
It is currently Mon May 20, 2013 12:31 am

All times are UTC - 7 hours [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Sat Mar 13, 2010 2:19 am 
Offline
Fresh meat

Joined: Sat Mar 13, 2010 1:48 am
Posts: 3
Location: Germany
Hi,

just started using Phidgets - they're great :D

Q: I'd like to communicate with the Phidgets just before the Mac goes to sleep. Unfortunately the Phidget lib/driver seems to catch the system sleep event before my Cocoa app does and kills the USB comm so I won't get through anymore :(

The log sequence is
Quote:
- INFO: Pausing USB traffic in preparation for system sleep...
- NSWorkspaceWillSleepNotification
(this is my app getting the sleep event; at this time e.g. CPhidgetTextLCD_setDisplayString returns 13 - EPHIDGET_TIMEOUT)
- INFO: USB traffic paused, now sleeping...


I even tried IOKit's kIOMessageSystemWillSleep notification, but that comes even later.

Any ideas? Is there a way to handle this?

Thanks!
Frank


Top
 Profile Send private message  
 
PostPosted: Mon Mar 15, 2010 10:06 am 
Offline
Lead Developer
User avatar

Joined: Mon Jun 20, 2005 8:46 am
Posts: 2345
Location: Canada
I react to the power events kIOMessageCanSystemSleep and kIOMessageSystemWillSleep by registering for power notifications with IORegisterForSystemPower.

I stop any pending and future USB transfers in the power event callback, so you would need to finish any communications you want to do before the callback gets called in the phidget21 library.

I can't say for sure, but I imagine that if you register for power callbacks before I do, you will get called first. You just need to set up the callback before calling open on any Phidgets. Also, you'll need to react to the kIOMessageCanSystemSleep event as it gets called before kIOMessageSystemWillSleep.

I resume trafic in the kIOMessageSystemHasPoweredOn event. If you try to send a message to Phidgets in this event, before the library has reacted, you'll probably get an error.

-Patrick


Top
 Profile Send private message  
 
PostPosted: Thu Mar 18, 2010 1:44 am 
Offline
Fresh meat

Joined: Sat Mar 13, 2010 1:48 am
Posts: 3
Location: Germany
Thanks a lot for your help! I tried all that, but without success :(

You're still to early for me :/ However, I register in awakeFromNib: and I'll see if I could do it even earlier (not sure where, main() maybe?).

Any other ideas? I guess extending your API is not an option? E.g. by providing a hook to a user function "doThisBeforeThePhidgetLibGoesToSleep()" ;-)

Power on is not a problem, but I'd like to switch off/on some hardware attached to an IF-Kit and put a message on an LCD when the user puts the mac to sleep with the Remote.

Frank


Top
 Profile Send private message  
 
PostPosted: Thu Mar 18, 2010 10:43 am 
Offline
Lead Developer
User avatar

Joined: Mon Jun 20, 2005 8:46 am
Posts: 2345
Location: Canada
Hi,

I've added CPhidget_set_OnWillSleep_Handler and CPhidget_set_OnWakeup_Handler to the Mac library. This will let you handle the power events properly - I'm not really sure why MacOS won't notify you before the Phidget library gets notified, but this is definitely something that needs to be possible.

I'm releasing the new library to the web now.

-Patrick


Top
 Profile Send private message  
 
PostPosted: Thu Mar 18, 2010 11:20 am 
Offline
Fresh meat

Joined: Sat Mar 13, 2010 1:48 am
Posts: 3
Location: Germany
My hero! :D :D :D :D :D
It works!

Thanks a lot! That's amazing support!

Frank


Top
 Profile Send private message  
 
PostPosted: Sat May 28, 2011 4:54 pm 
Offline
Phidgetly

Joined: Wed Aug 18, 2010 11:18 am
Posts: 15
Is there any documentation on these new functions ? I don't see them in the manual. I am imagining how they work. but not sure.

I have the exact problem as the other person. I get the sleep notification and I call the phidget routines and nothing happen then my program goes to sleep with the phidget relays staying on when I want them off.

Thanks,
Arthur


Top
 Profile Send private message  
 
PostPosted: Sat May 28, 2011 5:00 pm 
Offline
Phidgetly

Joined: Wed Aug 18, 2010 11:18 am
Posts: 15
I want to add in these handlers but actually I don't know how to do it in Objective-C world. How does one have a C function have access to Objective-C objects ? Also,I remember having crashes with QT and attach events calling into its event loop.

thanks,
again


Top
 Profile Send private message  
 
PostPosted: Mon May 30, 2011 10:00 am 
Offline
Lead Developer
User avatar

Joined: Mon Jun 20, 2005 8:46 am
Posts: 2345
Location: Canada
The Cocoa examples all demonstrate how to access Objective-C from the C callbacks - you basically need to pass 'self' as the usrPtr argument when you set up the callback - and then you have access to that object in the callback.

The TextLCD Cocoa example demonstrates the sleep callbacks. They are not documented.

-Patrick


Top
 Profile Send private message  
 
PostPosted: Wed Jun 01, 2011 10:57 am 
Offline
Phidgetly

Joined: Wed Aug 18, 2010 11:18 am
Posts: 15
Hi Patrick,

Okay, I looked at the example TextLCD. I have now a phidget call back into a C function. This C function then tries to call my objective C object. I am not sure the Objective C object works correctly. I saw a post on the web saying the callback does not have access to the instance variables of the objects. In my case I have 6 RelayObjects that control six relay cards and So I need the Objective-C object to turn them off on sleep. The NSLog also when executing the object from the call back spits out these messages.

*** __NSAutoreleaseNoPool(): Object 0x10055de40 of class NSCFString autoreleased with no pool in place - just leaking
NSSelectionArray autoreleased with no pool in place - just leaking
2011-05-31 23:13:06.698 DanceWild[24769:7817] *** __N


Top
 Profile Send private message  
 
PostPosted: Wed Jun 01, 2011 11:06 am 
Offline
Phidgetly

Joined: Wed Aug 18, 2010 11:18 am
Posts: 15
Patrick here is some of the code having problems. Currently it just freezes up. I think it is due to instance variables not being accessible. The Errors due to being in a different memory pool are disturbing.

Thanks for any help,
Arthur


Code:
// Some Implementation File
void * refToSelf;
int cCallback(void *userPtr)
{
   DoorManager *self = (id)userPtr;
//   objc_msgSend (self, @selector(cancelAllTasks), nil);   
    [self cancelAllTasks];
   NSLog(@"Phidget Sleep call back\n");
   return 0;
}
implementation DoorManager

#pragma mark Init / Final
-(id) init {      
   if (self=[super init]) {
// Handle sleeping      
      CPhidget_set_OnWillSleep_Handler(cCallback,self);
      refToSelf = self;
   }
   return self;
}
...
-(BOOL) cancelAllTasks {   
   [DBManager logStr:@"Close all doors called via Phidget Library"];
   return [doorOpener cancelAllTasks];
}
@end


Code:
// Next File DoorOpener.m

-(BOOL) cancelAllTasks {
   
   BOOL loopStatus=YES;
       status    =YES;
   
   [DBManager logStr:[NSMutableString stringWithFormat:@"Canceling all tasks(%d)",[fifoOfTimers count] ]];         
   NSArray *temp = [NSArray arrayWithArray: fifoOfTimers];   
   for (NSMutableDictionary  *task in temp) {
      loopStatus =  [self cancelTask:task];
      status = (loopStatus == NO) ? loopStatus : status;
      [fifoOfTimers removeObject:task];
//       [NSThread sleepForTimeInterval:3];
   }

    return status;
}

// Cancel closes the door by setting the relay off and then cancels the timer
// Returns to the caller a boolean status of YES if phidget returned EPHIDGET_OK.
-(BOOL ) cancelTask : (NSDictionary *) dict {
   int index;
   RelayCard *rc;
   NSNumber  *taskIdNSNum ;
   
   index      = [ [dict objectForKey:@"theRelayIndex" ] intValue];
   rc         =   [dict objectForKey:@"theRelayCard"  ];
   taskIdNSNum =   [dict objectForKey:@"taskId"      ];
   id < NSObject,DoorOpenerDelegate>  delegateId = [dict objectForKey:@"delegateId"   ];
      
   int resultCode = [rc setRelaysIndex:index state:NO];
   status = YES;
   if( resultCode != EPHIDGET_OK ) {
      [DBManager logStr:[NSMutableString stringWithFormat:@"Task(%@) Cancel Completion Failed with Result:%@",taskIdNSNum,[rc lastErrorMsg]]];            
      status = NO;      
   } else {
      [DBManager logStr:[NSMutableString stringWithFormat:@"Task(%@) Cancel Completion Success with Result:%@",taskIdNSNum,[rc lastErrorMsg]]];            
   }
   [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timerHasExpired:) object:dict];
   if (delegateId && [delegateId respondsToSelector:@selector(taskCompleted:taskId:)]) {
      [delegateId taskCompleted:status taskId:[taskIdNSNum intValue]];      
   } else {
      if (delegateId && [delegateId respondsToSelector:@selector(taskCompleted:)]) {
         //         [delegate taskCompleted:status];      
      }
   }   
   return status;
}

// File relayCard.m
-(int) setRelaysIndex:(int) index state:(BOOL) state {
   
   if(  hwIsSimulated )
      return EPHIDGET_OK;

   resultCode = CPhidgetInterfaceKit_setOutputState(ifkit, index , state);
   if( resultCode != EPHIDGET_OK ) {      
      CPhidget_getErrorDescription(resultCode,&lastErrorResultStr);
//      NSLog(@"Failed to set card's relay index(%d) with serial number%d, its state is unknown",index,serialNumber); // Caller should log the error
   }
   relayStates[index] = state;
   return resultCode;
}


Top
 Profile Send private message  
 
PostPosted: Wed Jun 01, 2011 3:01 pm 
Offline
Lead Developer
User avatar

Joined: Mon Jun 20, 2005 8:46 am
Posts: 2345
Location: Canada
DoorManager *self = (id)userPtr; in the callback probably isn't the best idea, as 'self' is an objective-c keyword - I'm not sure if this will give you problems or not.

The rest looks ok.

You will definitely need to create an NSAutoReleasePool in the callback because it's not on the main thread - you may also want to performSelectorOnMainThread rather then calling the function directly - you can see all of this in the interfacekit Cocoa example (among others) for the sensor change event for example.

-Patrick


Top
 Profile Send private message  
 
PostPosted: Wed Jun 08, 2011 9:14 pm 
Offline
Phidgetly

Joined: Wed Aug 18, 2010 11:18 am
Posts: 15
Thanks , all that you said has helped !! !!


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 7 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group