I'm not sure about a serial USB event, but presumably you'd be powering your Teensy from something other than the USB so it could run while the computer is asleep. If so, why not just sense voltage on the USB VCC pin?
The Teensy is powered by the computer and the Teensy still runs when the computer is asleep.
The Teensy is powered by the computer and the Teensy still runs when the computer is asleep.
extern "C" { extern volatile uint8_t usb_configuration; }
extern "C" { extern volatile uint8_t usb_configuration; }
void setup(void) {
pinMode(13, OUTPUT);
}
void loop(void) {
if (usb_configuration) {
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
delay(100);
}
#define kIOMessageServiceIsTerminated iokit_common_msg(0x010)
#define kIOMessageServiceIsSuspended iokit_common_msg(0x020)
#define kIOMessageServiceIsResumed iokit_common_msg(0x030)
#define kIOMessageServiceIsRequestingClose iokit_common_msg(0x100)
#define kIOMessageServiceIsAttemptingOpen iokit_common_msg(0x101)
#define kIOMessageServiceWasClosed iokit_common_msg(0x110)
#define kIOMessageServiceBusyStateChange iokit_common_msg(0x120)
#define kIOMessageServicePropertyChange iokit_common_msg(0x130)
#define kIOMessageCanDevicePowerOff iokit_common_msg(0x200)
#define kIOMessageDeviceWillPowerOff iokit_common_msg(0x210)
#define kIOMessageDeviceWillNotPowerOff iokit_common_msg(0x220)
#define kIOMessageDeviceHasPoweredOn iokit_common_msg(0x230)
#define kIOMessageCanSystemPowerOff iokit_common_msg(0x240)
#define kIOMessageSystemWillPowerOff iokit_common_msg(0x250)
#define kIOMessageSystemWillNotPowerOff iokit_common_msg(0x260)
#define kIOMessageCanSystemSleep iokit_common_msg(0x270)
#define kIOMessageSystemWillSleep iokit_common_msg(0x280)
#define kIOMessageSystemWillNotSleep iokit_common_msg(0x290)
#define kIOMessageSystemHasPoweredOn iokit_common_msg(0x300)
#define kIOMessageSystemWillRestart iokit_common_msg(0x310)
#define kIOMessageSystemWillPowerOn iokit_common_msg(0x320)
// compile with gcc -Wall -O2 -framework CoreFoundation -framework IOKit -o power power.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <termios.h>
#include <sys/select.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOMessage.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#define PORTTYPE int
#define BAUD B115200
PORTTYPE port = -1;
void
die(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
exit(1);
}
void
MySleepCallBack (void * refCon, io_service_t service, natural_t messageType,
void * messageArgument)
{
struct timeval now;
gettimeofday(&now, NULL);
#if DEBUG
fprintf(stderr, "messageType %08lx, arg %08lx %ld\n",
(long unsigned int)messageType,
(long unsigned int)messageArgument, now.tv_sec);
#endif
switch (messageType) {
case kIOMessageSystemWillSleep:
if (port >= 0) write(port, "s", 1);
fprintf(stderr, "sleep @ %ld\n", now.tv_sec);
break;
case kIOMessageSystemWillPowerOn:
if (port >= 0) write(port, "a", 1);
fprintf(stderr, "awake @ %ld\n", now.tv_sec);
break;
default:
break;
}
}
PORTTYPE
open_port_and_set_baud_or_die(const char *name, long baud)
{
PORTTYPE fd;
struct termios tinfo;
fd = open(name, O_RDWR | O_NONBLOCK);
if (fd < 0) die("unable to open port %s\n", name);
if (tcgetattr(fd, &tinfo) < 0) die("unable to get serial parms\n");
cfmakeraw(&tinfo);
if (cfsetspeed(&tinfo, baud) < 0) die("error in cfsetspeed\n");
tinfo.c_cflag |= CLOCAL;
if (tcsetattr(fd, TCSANOW, &tinfo) < 0) die("unable to set baud rate\n");
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
return fd;
}
int
main (int argc, const char *argv[])
{
io_connect_t root_port;
IONotificationPortRef notifyPortRef;
io_object_t notifierObject;
void *refCon=NULL;
if (argc >= 2) {
port = open_port_and_set_baud_or_die(argv[1], BAUD);
}
root_port = IORegisterForSystemPower(refCon, ¬ifyPortRef,
MySleepCallBack, ¬ifierObject);
if (root_port == 0)
{
printf("IORegisterForSystemPower failed\n");
return 1;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notifyPortRef),
kCFRunLoopCommonModes);
CFRunLoopRun();
}
int led = 13;
void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
}
void loop() {
if (Serial.available()) {
char inChar = (char) Serial.read();
switch (inChar) {
case 's':
digitalWrite(led, LOW);
break;
case 'a':
digitalWrite(led, HIGH);
break;
default:
break;
}
}
}