Linux USB: turning the power on and off?

后端 未结 13 1333
遥遥无期
遥遥无期 2020-12-07 10:41

How can I programmatically enable and disable the power to a particular USB port on Linux? Is such a thing even possible? Mac answers appreciated as well!

I was tryi

13条回答
  •  粉色の甜心
    2020-12-07 10:51

    Don't buy an expensive smart hub just for turning USB gadgets on and off.
    All you need is a microcontroller.

    Arduino Nano™ ATmega328 * 1

    The Nano is a 16MHz 8-bit computer with 2K RAM and 32K of flash storage.
    It has 22 programmable pins (8 analog and 14 digital).
    It can read/write USB, and is powered by its 5.0V microUSB port (up to 12.0V external).

    // USB Blinker
    // Blink LED while receiving USB stream
    //
    // For Arduino Nano™
    
    int LED = 13;
    
    // setup() is run once at powerup or when reset button is pressed
    //
    void setup() {
            pinMode(LED, OUTPUT);   // Configure pin D13 as output 
            Serial.begin(9600);     // Open 9600bps USB stream
    }
    
    // loop() runs forever at 16Mhz, blinking the LED at 1Hz when receiving USB data.
    // 
    void loop() { 
            if (Serial.available() > 0) {           // When stream is buffering
                    digitalWrite(LED, HIGH);        //   turn on LED 
                    delay(500);                     //   wait half second
                    digitalWrite(LED, LOW);         //   turn off LED
                    delay(500);                     //   wait half second
                    while (Serial.available() > 0)  //   drain the buffer
                            Serial.read();         
             }
    }
    


    Exquisite tiny cases are available (also free 3d printables).

    C4Labs Zebra Black Ice Case
    6

    * Use Genuine Arduino Nano™ only. Beware of counterfeits.

    UPDATE: Miniaturised ATtiny and wireless microcontollers are also available.

提交回复
热议问题