Detecting The State Of A USB Port

Status
Not open for further replies.

thagh05t

Active member
Hello all, I am wondering if there is a way to detect the state of a USB port on a Mac and possibly listen for state changes.

I have explored options with the serial libraries without avail. So far I have found nothing that will detect an actual state or state change of a USB port. Case in point, if a USB device is plugged into a PC/Mac and you go to boot options it should check all USB ports for an external device for boot. This may be done pretty early in the boot process, but still I would love to be able to figure this out.
 
You could try adding code inside Teensy's USB interrupt.

That code is different on 8 vs 32 bit Teensy. Since you didn't even say which board you're using, nor what you're trying to detect, I really can't go into much detail.
 
I apologize for that, I am using a teensy 3.1 although I think this project will be migrating later to the LC because the cost is 50% less. To tell you the truth this is only my second attempt at any C style code so be gentle with me when it comes to specific things. I do know other languages so it wont take me long to pick this up, please bear with me.

As far as the I/O goes.... I have no clue what I am looking for..... I am looking for anything and everything to be detected as changes happen I would like Teensy to possible record it to SD, blink binary, idc really.

As soon as I plug the teensy into the Mac I would like the teensy to listen for events the Mac may be busy doing. My thoughts are that maybe the Mac will say "Hello is anybody there on USB port xxx" and I will be able to catch that event and do something with it.
 
i am also trying to read the interrupt page to see if I can grasp part of the concept before your next reply.
 
Can I get a bit of info on how interrupts work exactly? I am trying to sift the internet for some material, but it's pretty insinuated rather than documented. The code I have so far is throwing a construct error.

Code:
#include <avr/io.h>
#include <avr/interrupt.h>

void setup(){
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  volatile int a = 0;
  delay(3000);
}

void loop(){
  volatile int a = 1;
  while( (a = 1) ){
    digitalWrite(13, LOW);
  }
}

ISR(USB_GEN_vect){
  volatile int a = 1;
}
 
For example of what I am wanting. If I plug in a USB flash drive to a Mac it will flash an LED and then go dormant. If you mount the USB or access any files it will begin to flash again. I want to be able to set up an interrupt on whatever makes it flash. Any advice??
 
Status
Not open for further replies.
Back
Top