<commit_before>/*
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.things.contrib.driver.apa102;

import android.graphics.Color;
import android.support.annotation.VisibleForTesting;

import com.google.android.things.pio.PeripheralManagerService;
import com.google.android.things.pio.SpiDevice;

import java.io.IOException;
import java.util.Arrays;

/**
 * Device driver for APA102 / Dotstar RGB LEDs using 2-wire SPI.
 *
 * For more information on SPI, see:
 *   https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
 * For information on the APA102 protocol, see:
 *   https://cpldcpu.wordpress.com/2014/11/30/understanding-the-apa102-superled
 */

@SuppressWarnings({"unused", "WeakerAccess"})
public class Apa102 implements AutoCloseable {
    private static final String TAG = "Apa102";

    /**
     * Color ordering for the RGB LED messages; the most common modes are BGR and RGB.
     */
    public enum Mode {
        RGB,
        RBG,
        GRB,
        GBR,
        BRG,
        BGR
    }

    /**
     * The direction to apply colors when writing LED data
     */
    public enum Direction {
        NORMAL,
        REVERSED,
    }

    /**
     * The maximum brightness level
     */
    public static final int MAX_BRIGHTNESS = 31;

    // RGB LED strip configuration that must be provided by the caller.
    private Mode mLedMode;

    // RGB LED strip settings that have sensible defaults.
    private int mLedBrightness = MAX_BRIGHTNESS >> 1; // default to half

    // Direction of the led strip;
    private Direction mDirection;

    // Device SPI Configuration constants
    private static final int SPI_BPW = 8; // Bits per word
    private static final int SPI_FREQUENCY = 1000000;
    private static final int SPI_MODE = 2;

    // Protocol constants for APA102c
    private static final int APA_PACKET_LENGTH = 4;
    private static final byte APA_START_DATA_BYTE = (byte) 0x00;
    private static final byte APA_END_DATA_BYTE = (byte) 0xFF;

    // For peripherals access
    private SpiDevice mDevice = null;

    // For composing data to send to the peripheral
    private byte[] mLedData;

    /**
     * Create a new Apa102 driver.
     *
     * @param spiBusPort Name of the SPI bus<commit_msg>use the constant from the SDK<commit_after>/*
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.things.contrib.driver.apa102;

import android.graphics.Color;
import android.support.annotation.VisibleForTesting;

import com.google.android.things.pio.PeripheralManagerService;
import com.google.android.things.pio.SpiDevice;

import java.io.IOException;
import java.util.Arrays;

/**
 * Device driver for APA102 / Dotstar RGB LEDs using 2-wire SPI.
 *
 * For more information on SPI, see:
 *   https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
 * For information on the APA102 protocol, see:
 *   https://cpldcpu.wordpress.com/2014/11/30/understanding-the-apa102-superled
 */

@SuppressWarnings({"unused", "WeakerAccess"})
public class Apa102 implements AutoCloseable {
    private static final String TAG = "Apa102";

    /**
     * Color ordering for the RGB LED messages; the most common modes are BGR and RGB.
     */
    public enum Mode {
        RGB,
        RBG,
        GRB,
        GBR,
        BRG,
        BGR
    }

    /**
     * The direction to apply colors when writing LED data
     */
    public enum Direction {
        NORMAL,
        REVERSED,
    }

    /**
     * The maximum brightness level
     */
    public static final int MAX_BRIGHTNESS = 31;

    // RGB LED strip configuration that must be provided by the caller.
    private Mode mLedMode;

    // RGB LED strip settings that have sensible defaults.
    private int mLedBrightness = MAX_BRIGHTNESS >> 1; // default to half

    // Direction of the led strip;
    private Direction mDirection;

    // Device SPI Configuration constants
    private static final int SPI_BPW = 8; // Bits per word
    private static final int SPI_FREQUENCY = 1000000;
    private static final int SPI_MODE = SpiDevice.MODE2;

    // Protocol constants for APA102c
    private static final int APA_PACKET_LENGTH = 4;
    private static final byte APA_START_DATA_BYTE = (byte) 0x00;
    private static final byte APA_END_DATA_BYTE = (byte) 0xFF;

    // For peripherals access
    private SpiDevice mDevice = null;

    // For composing data to send to the peripheral
    private byte[] mLedData;

    /**
     * Create a new Apa102 driver.
     *
     * @param spiBusPort Name of the SPI bus
{'fraction_non_alphanumeric': 0.07745480326125487, 'fraction_numerical': 0.02056008507621411, 'mean_word_length': 2.9051903114186852, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2564866', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1554, 'n_words': 604}