Power off an USB device in software on Windows

Some USB hubs have the ability to turn power off to its downstream devices. “Is it possible to power up ports on a USB hub from Ubuntu?” https://askubuntu.com/questions/149242/is-it-possible-to-power-up-ports-on-a-usb-hub-from-ubuntu Which points to some c source for hub-ctrl.c See: http://www.gniibe.org/development/ac-power-control-by-USB-hub/index I tested this on Ubuntu with a Dream-Cheeky USB LED unit, and it did seem to turn … Read more

Set ALSA master volume from C code

The following works for me. The parameter volume is to be given in the range [0, 100]. Beware, there is no error handling! void SetAlsaMasterVolume(long volume) { long min, max; snd_mixer_t *handle; snd_mixer_selem_id_t *sid; const char *card = “default”; const char *selem_name = “Master”; snd_mixer_open(&handle, 0); snd_mixer_attach(handle, card); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); snd_mixer_selem_id_alloca(&sid); snd_mixer_selem_id_set_index(sid, 0); … Read more

Static allocation of opaque data types

You can use the _alloca function. I believe that it’s not exactly Standard, but as far as I know, nearly all common compilers implement it. When you use it as a default argument, it allocates off the caller’s stack. // Header typedef struct {} something; int get_size(); something* create_something(void* mem); // Usage handle* ptr = … Read more

Looking for an efficient integer square root algorithm for ARM Thumb2

Integer Square Roots by Jack W. Crenshaw could be useful as another reference. The C Snippets Archive also has an integer square root implementation. This one goes beyond just the integer result, and calculates extra fractional (fixed-point) bits of the answer. (Update: unfortunately, the C snippets archive is now defunct. The link points to the … Read more

Difference between const & const volatile

An object marked as const volatile will not be permitted to be changed by the code (an error will be raised due to the const qualifier) – at least through that particular name/pointer. The volatile part of the qualifier means that the compiler cannot optimize or reorder access to the object. In an embedded system, … Read more