Back to project page konashi-android-sdk.
The source code is released under:
Apache License
If you think the Android project konashi-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.uxxu.konashi.lib; /*from w w w . j av a 2 s. c om*/ import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; /** * konashi??????????????? * * @author monakaz, YUKAI Engineering * http://konashi.ux-xu.com * ======================================================================== * Copyright 2014 Yukai Engineering 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. * */ public class KonashiManager extends KonashiBaseManager implements KonashiApiInterface { private static final int PIO_LENGTH = 8; private static final int PWM_LENGTH = 8; private static final int AIO_LENGTH = 3; // konashi members // PIO private byte mPioModeSetting = 0; private byte mPioPullup = 0; private byte mPioInput = 0; private byte mPioOutput = 0; // PWM private byte mPwmSetting = 0; private int[] mPwmDuty; private int[] mPwmPeriod; // AIO private int[] mAioValue; // I2C private byte mI2cSetting; private byte[] mI2cReadData; private int mI2cReadDataLength; private byte mI2cReadAddress; // UART private byte mUartSetting; private byte mUartBaudrate; private byte mUartRxData; // Hardware private int mBatteryLevel; private int mRssi; /////////////////////////// // Initialization /////////////////////////// private void initializeMembers(){ int i; // PIO mPioModeSetting = 0; mPioPullup = 0; mPioInput = 0; mPioOutput = 0; // PWM mPwmSetting = 0; mPwmDuty = new int[PWM_LENGTH]; for(i=0; i<PWM_LENGTH; i++) mPwmDuty[i] = 0; mPwmPeriod = new int[PWM_LENGTH]; for(i=0; i<PWM_LENGTH; i++) mPwmPeriod[i] = 0; // AIO mAioValue = new int[AIO_LENGTH]; for(i=0; i<AIO_LENGTH; i++) mAioValue[i] = 0; // I2C mI2cSetting = 0; mI2cReadData = new byte[Konashi.I2C_DATA_MAX_LENGTH]; for(i=0; i<Konashi.I2C_DATA_MAX_LENGTH; i++) mI2cReadData[i] = 0; mI2cReadDataLength = 0; mI2cReadAddress = 0; // UART mUartSetting = 0; mUartBaudrate = 0; mUartRxData = 0; // Hardware mBatteryLevel = 0; mRssi = 0; } @Override public void initialize(Context context) { super.initialize(context); initializeMembers(); } /////////////////////////// // Observer /////////////////////////// /** * konashi??????????????????????? * @param observer ???????????? */ public void addObserver(KonashiObserver observer){ mNotifier.addObserver(observer); } /** * ????????????????????? * @param observer ???????????? */ public void removeObserver(KonashiObserver observer){ mNotifier.removeObserver(observer); } /** * ????????????????????????? */ public void removeAllObservers(){ mNotifier.removeAllObservers(); } /////////////////////////// // PIO /////////////////////////// /** * PIO?????????????????????????????????????????????????????? * @param pin ??????PIO????????? * @param mode ???????????????INPUT ??? OUTPUT ?????????????????? */ @Override public void pinMode(int pin, int mode){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pin >= Konashi.PIO0 && pin <= Konashi.PIO7 && (mode == Konashi.OUTPUT || mode == Konashi.INPUT)){ if(mode == Konashi.OUTPUT){ mPioModeSetting |= (byte)(0x01 << pin); }else{ mPioModeSetting &= (byte)(~(0x01 << pin) & 0xFF); } byte[] val = new byte[1]; val[0] = mPioModeSetting; addWriteMessage(KonashiUUID.PIO_SETTING_UUID, val); } } /** * PIO?????????????????????????????????????????????????????? * @param modes PIO0 ? PIO7 ????8??????? */ @Override public void pinModeAll(int modes){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(modes >= 0x00 && modes <= 0xFF){ mPioModeSetting = (byte)modes; byte[] val = new byte[1]; val[0] = mPioModeSetting; addWriteMessage(KonashiUUID.PIO_SETTING_UUID, val); } } /** * PIO???????????????????????????? * @param pin ??????PIO???????? * @param pullup ?????????????????????PULLUP ??? NO_PULLS ?????????????????? */ @Override public void pinPullup(int pin, int pullup){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pin >= Konashi.PIO0 && pin <= Konashi.PIO7 && (pullup == Konashi.PULLUP || pullup == Konashi.NO_PULLS)){ if(pullup == Konashi.PULLUP){ mPioPullup |= (byte)(0x01 << pin); }else{ mPioPullup &= (byte)(~(0x01 << pin) & 0xFF); } byte[] val = new byte[1]; val[0] = mPioPullup; addWriteMessage(KonashiUUID.PIO_PULLUP_UUID, val); } } /** * PIO???????????????????????????? * @param pullups PIO0 ? PIO7 ????8??????????????? */ @Override public void pinPullupAll(int pullups){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pullups >= 0x00 && pullups <= 0xFF){ mPioPullup = (byte)pullups; byte[] val = new byte[1]; val[0] = mPioPullup; addWriteMessage(KonashiUUID.PIO_PULLUP_UUID, val); } } /** * PIO?????????????????????????? * @param pin PIO???????? * @return HIGH(1) ?????????? LOW(0) */ @Override public int digitalRead(int pin){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return -1; } return (mPioInput >> pin) & 0x01; } /** * PIO??????????????????????????????? * @return PIO?????(PIO0?PIO7??????????8bit(1byte)???????) */ @Override public int digitalReadAll(){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return -1; } return mPioInput; } /** * PIO???????????????????????? * @param pin ??????PIO???????? * @param value ??????PIO????????HIGH ?????????? LOW ????????? */ @Override public void digitalWrite(int pin, int value){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pin >= Konashi.PIO0 && pin <= Konashi.PIO7 && (value == Konashi.HIGH || value == Konashi.LOW)){ KonashiUtils.log("digitalWrite pin: " + pin + ", value: " + value); if(value == Konashi.HIGH){ mPioOutput |= 0x01 << pin; } else { mPioOutput &= ~(0x01 << pin) & 0xFF; } byte[] val = new byte[1]; val[0] = mPioOutput; addWriteMessage(KonashiUUID.PIO_OUTPUT_UUID, val); } } /** * PIO???????????????????????? * @param value PIO????????PIO0?PIO7??????????8bit(1byte)??????? */ @Override public void digitalWriteAll(int value){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(value >= 0x00 && value <= 0xFF){ mPioOutput = (byte)value; byte[] val = new byte[1]; val[0] = mPioOutput; addWriteMessage(KonashiUUID.PIO_OUTPUT_UUID, val); } } /////////////////////////// // PWM /////////////////////////// /** * PIO ??????????? PWM ???????????????/??????????????????? * @param pin PWM?????????????PIO??????????Konashi.PIO0 ? Konashi.PIO7? * @param mode ??????PWM???????Konashi.PWM_DISABLE, Konashi.PWM_ENABLE, Konashi.PWM_ENABLE_LED_MODE ?????????????????????? */ @Override public void pwmMode(int pin, int mode){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pin >= Konashi.PIO0 && pin <= Konashi.PIO7 && (mode == Konashi.PWM_DISABLE || mode == Konashi.PWM_ENABLE || mode == Konashi.PWM_ENABLE_LED_MODE)){ if(mode == Konashi.PWM_ENABLE || mode == Konashi.PWM_ENABLE_LED_MODE){ mPwmSetting |= 0x01 << pin; } else { mPwmSetting &= ~(0x01 << pin) & 0xFF; } if (mode == Konashi.PWM_ENABLE_LED_MODE){ pwmPeriod(pin, Konashi.PWM_LED_PERIOD); pwmLedDrive(pin, 0.0F); } byte[] val = new byte[1]; val[0] = mPwmSetting; addWriteMessage(KonashiUUID.PWM_CONFIG_UUID, val); } } /** * ??????????PWM????????? * @param pin PWM?????????????PIO??????????Konashi.PIO0 ? Konashi.PIO7? * @param period ????????????????(us)???32bit??????????????????????????2^(32)us = 71.5?? */ @Override public void pwmPeriod(int pin, int period){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pin >= Konashi.PIO0 && pin <= Konashi.PIO7 && mPwmDuty[pin] <= period){ mPwmPeriod[pin] = period; byte[] val = new byte[5]; val[0] = (byte)pin; val[1] = (byte)((mPwmPeriod[pin] >> 24) & 0xFF); val[2] = (byte)((mPwmPeriod[pin] >> 16) & 0xFF); val[3] = (byte)((mPwmPeriod[pin] >> 8) & 0xFF); val[4] = (byte)((mPwmPeriod[pin] >> 0) & 0xFF); addWriteMessage(KonashiUUID.PWM_PARAM_UUID, val); } } /** * ??????????PWM????????(ON??????????????????)???????? * @param pin PWM?????????????PIO??????????Konashi.PIO0 ? Konashi.PIO7? * @param duty ???????????????????(us)???32bit??????????????????????????2^(32)us = 71.5?? */ @Override public void pwmDuty(int pin, int duty){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pin >= Konashi.PIO0 && pin <= Konashi.PIO7 && duty <= mPwmPeriod[pin]){ mPwmDuty[pin] = duty; byte[] val = new byte[5]; val[0] = (byte)pin; val[1] = (byte)((mPwmDuty[pin] >> 24) & 0xFF); val[2] = (byte)((mPwmDuty[pin] >> 16) & 0xFF); val[3] = (byte)((mPwmDuty[pin] >> 8) & 0xFF); val[4] = (byte)((mPwmDuty[pin] >> 0) & 0xFF); addWriteMessage(KonashiUUID.PWM_DUTY_UUID, val); } } /** * ??????????LED?????????0%?100%????????? * @param pin PWM?????????????PIO??????????Konashi.PIO0 ? Konashi.PIO7? * @param dutyRatio LED?????????0.0F?100.0F ?????????????????????????? */ @Override public void pwmLedDrive(int pin, float dutyRatio){ int duty; if(dutyRatio >= 0.0 && dutyRatio <= 100.0){ duty = (int)(Konashi.PWM_LED_PERIOD * dutyRatio / 100); pwmDuty(pin, duty); } } /** * pwmLedDrive(int pin, float dutyRatio) ??? double???dutyRatio????????? * @param pin PWM?????????????PIO??????????Konashi.PIO0 ? Konashi.PIO7? * @param dutyRatio LED?????????0.0?100.0 ?????????????????????????? */ @Override public void pwmLedDrive(int pin, double dutyRatio){ pwmLedDrive(pin, (float)dutyRatio); } /////////////////////////// // AIO /////////////////////////// /** * AIO ???????????????????????????????? konashi ?????? * @param pin AIO?????????????????????????? AIO0, AIO1, AIO2 */ @Override public void analogReadRequest(int pin) { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pin==Konashi.AIO0){ addReadMessage(KonashiUUID.ANALOG_READ0_UUID); } else if(pin==Konashi.AIO1){ addReadMessage(KonashiUUID.ANALOG_READ1_UUID); } else if(pin==Konashi.AIO2) { addReadMessage(KonashiUUID.ANALOG_READ2_UUID); } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); } } /** * AIO ?????????????????????????? * @param pin AIO?????????????????????????? AIO0, AIO1, AIO2 */ @Override public int analogRead(int pin) { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return -1; } if(pin >= Konashi.AIO0 && pin <= Konashi.AIO2){ return mAioValue[pin]; } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); return -1; } } /** * AIO ???????????????????????????? * @param pin AIO?????????????????????????? AIO0, AIO1, AIO2 * @param milliVolt ?????????mV??????0?1300??????? */ @Override public void analogWrite(int pin, int milliVolt){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(pin >= Konashi.AIO0 && pin <= Konashi.AIO2 && milliVolt >= 0 && milliVolt <= Konashi.ANALOG_REFERENCE){ byte[] val = new byte[3]; val[0] = (byte)pin; val[1] = (byte)((milliVolt >> 8) & 0xFF); val[2] = (byte)((milliVolt >> 0) & 0xFF); addWriteMessage(KonashiUUID.ANALOG_DRIVE_UUID, val); } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); } } /////////////////////////// // UART /////////////////////////// /** * UART ?????/????????? * @param mode ??????UART???????Konashi.UART_DISABLE, Konashi.UART_ENABLE ??? */ public void uartMode(int mode){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(mode==Konashi.UART_DISABLE || mode==Konashi.UART_ENABLE){ mUartSetting = (byte)mode; byte[] val = new byte[1]; val[0] = (byte)mode; addWriteMessage(KonashiUUID.UART_CONFIG_UUID, val); } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); } } /** * UART ?????????????? * @param baudrate UART????????Konashi.UART_RATE_2K4 ??? Konashi.UART_RATE_9K6 ??? */ public void uartBaudrate(int baudrate){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(baudrate==Konashi.UART_RATE_2K4 || baudrate==Konashi.UART_RATE_9K6){ mUartBaudrate = (byte)baudrate; byte[] val = new byte[2]; val[0] = (byte)((baudrate >> 8) & 0xFF); val[1] = (byte)((baudrate >> 0) & 0xFF); addWriteMessage(KonashiUUID.UART_BAUDRATE_UUID, val); } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); } } /** * UART ???????1??????????? * @param data ?????????? */ public void uartWrite(byte data){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(mUartSetting==Konashi.UART_ENABLE){ byte[] val = new byte[1]; val[0] = data; addWriteMessage(KonashiUUID.UART_TX_UUID, val); } else { notifyKonashiError(KonashiErrorReason.NOT_ENABLED_UART); } } /////////////////////////// // I2C /////////////////////////// /** * I2C????????????????? * @param condition ????????Konashi.I2C_START_CONDITION, Konashi.I2C_RESTART_CONDITION, Konashi.I2C_STOP_CONDITION ??????????? */ private void i2cSendCondition(int condition) { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(!isEnableI2c()){ notifyKonashiError(KonashiErrorReason.NOT_ENABLED_I2C); return; } if(condition==Konashi.I2C_START_CONDITION || condition==Konashi.I2C_RESTART_CONDITION || condition==Konashi.I2C_STOP_CONDITION){ byte[] val = new byte[1]; val[0] = (byte)condition; addWriteMessage(KonashiUUID.I2C_START_STOP_UUID, val); } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); } } /** * I2C????????????????????????????? * @return ????????????????????????????true */ private boolean isEnableI2c(){ return (mI2cSetting==Konashi.I2C_ENABLE || mI2cSetting==Konashi.I2C_ENABLE_100K || mI2cSetting==Konashi.I2C_ENABLE_400K); } /** * I2C???/????????? * @param mode ??????I2C???????Konashi.I2C_DISABLE , Konashi.I2C_ENABLE, Konashi.I2C_ENABLE_100K, Konashi.I2C_ENABLE_400K???? */ @Override public void i2cMode(int mode) { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(mode==Konashi.I2C_DISABLE || mode==Konashi.I2C_ENABLE || mode==Konashi.I2C_ENABLE_100K || mode==Konashi.I2C_ENABLE_400K){ mI2cSetting = (byte)mode; byte[] val = new byte[1]; val[0] = (byte)mode; addWriteMessage(KonashiUUID.I2C_CONFIG_UUID, val); } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); } } /** * I2C????????????????????? */ @Override public void i2cStartCondition() { i2cSendCondition(Konashi.I2C_START_CONDITION); } /** * I2C?????????????????????? */ @Override public void i2cRestartCondition() { i2cSendCondition(Konashi.I2C_RESTART_CONDITION); } /** * I2C????????????????????? */ @Override public void i2cStopCondition() { i2cSendCondition(Konashi.I2C_STOP_CONDITION); } /** * I2C???????????????????????????? * @param length ?????????(byte)?????????? Konashi.I2C_DATA_MAX_LENGTH (19)byte?????? * @param data ??????????????? * @param address ????????????? */ @Override public void i2cWrite(int length, byte[] data, byte address) { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(!isEnableI2c()){ notifyKonashiError(KonashiErrorReason.NOT_ENABLED_I2C); return; } if(length>0 && length<=Konashi.I2C_DATA_MAX_LENGTH){ byte[] val = new byte[20]; val[0] = (byte)(length + 1); val[1] = (byte)((address << 1) & 0xFE); for(int i=0; i<length; i++){ val[i+2] = data[i]; } addWriteMessage(KonashiUUID.I2C_WRITE_UUID, val); } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); } } /** * I2C??????????????????????????????????????? * @param length ??????????????????? Konashi.I2C_DATA_MAX_LENGTHs (19) * @param address ???????????????? */ @Override public void i2cReadRequest(int length, byte address) { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } if(!isEnableI2c()){ notifyKonashiError(KonashiErrorReason.NOT_ENABLED_I2C); return; } if(length>0 && length<=Konashi.I2C_DATA_MAX_LENGTH){ mI2cReadAddress = (byte)((address<<1)|0x1); mI2cReadDataLength = length; byte[] val = {(byte)length, mI2cReadAddress}; addWriteMessage(KonashiUUID.I2C_READ_PARAM_UUID, val); } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); } } /** * I2C????????????????????????? * @param length ??????????????????? Konashi.I2C_DATA_MAX_LENGTHs (19) */ @Override public byte[] i2cRead(int length) { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return null; } if(!isEnableI2c()){ notifyKonashiError(KonashiErrorReason.NOT_ENABLED_I2C); return null; } if(length>0 && length<=Konashi.I2C_DATA_MAX_LENGTH && length==mI2cReadDataLength){ return mI2cReadData; } else { notifyKonashiError(KonashiErrorReason.INVALID_PARAMETER); return null; } } /////////////////////////// // Hardware /////////////////////////// /** * konashi????????? */ @Override public void reset(){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } byte[] val = new byte[1]; val[0] = 1; addWriteMessage(KonashiUUID.HARDWARE_RESET_UUID, val); } /** * konashi ?????????????????????????? konashi ?????? */ @Override public void batteryLevelReadRequest(){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } addReadMessage(KonashiUUID.BATTERY_SERVICE_UUID, KonashiUUID.BATTERY_LEVEL_UUID); } /** * konashi ???????????????? * @return 0 ? 100 ????????????????????????????? */ @Override public int getBatteryLevel(){ if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return -1; } return mBatteryLevel; } /** * konashi ?????????????????????????? */ @Override public void signalStrengthReadRequest() { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return; } readRemoteRssi(); } /** * konashi ???????????? * @return ????(????????db) */ @Override public int getSignalStrength() { if(!isEnableAccessKonashi()){ notifyKonashiError(KonashiErrorReason.NOT_READY); return -1; } return mRssi; } //////////////////////////////// // Notification event handler //////////////////////////////// // TODO: need refactor @Override protected void onUpdatePioInput(byte value) { // PIO input notification mPioInput = value; super.onUpdatePioInput(value); } @Override protected void onUpdateAnalogValue(int pin, int value) { mAioValue[pin] = value; super.onUpdateAnalogValue(pin, value); } @Override protected void onRecieveUart(byte data) { mUartRxData = data; super.onRecieveUart(data); } @Override protected void onUpdateBatteryLevel(int level) { mBatteryLevel = level; super.onUpdateBatteryLevel(level); } @Override protected void onUpdateSignalSrength(int rssi) { mRssi = rssi; super.onUpdateSignalSrength(rssi); } }