Copied base code from Adafruit.