# -*- coding: utf-8 -*-

########################################
# HXbot utility module                 #
# EoF 2016 EoF@itphx.ru                #
########################################

# I2C bus
I2C_BUS_NUMBER = 1
EXT_COM = 0xAA
XOR_SEQ = 0xFF

# VAR
REFLEX_ADDRESS = 0x04
REFLEX_RESPONSE_DELAY = 0.1  # Delay to get response after send command, sec
REFLEX_ERROR_LIMIT = 5
REFLEX_MOVE_TIMEOUT = 60

# COMMANDS
# Util
COM_PING = 0x01
COM_GET_TEMP = 0x02
COM_GET_DISTANCE = 0x03

# Stop
STOP = 0x0F
# Move
MOVE_FLAG = 0x04
MOVE_FLOAT = 0x10
MOVE_FWD1 = 0x11
MOVE_FWD2 = 0x12
MOVE_FWD3 = 0x13
MOVE_FWD4 = 0x14
MOVE_FWD5 = 0x15
MOVE_FWD6 = 0x16
MOVE_FWD7 = 0x17
MOVE_BREAK = 0x18
MOVE_REV7 = 0x19
MOVE_REV6 = 0x1A
MOVE_REV5 = 0x1B
MOVE_REV4 = 0x1C
MOVE_REV3 = 0x1D
MOVE_REV2 = 0x1E
MOVE_REV1 = 0x1F

# Steering
STEER_FLAG = 0x05
STEER_CENTER = 0x20
STEER_LEFT1 = 0x21
STEER_LEFT2 = 0x22
STEER_LEFT3 = 0x23
STEER_LEFT4 = 0x24
STEER_LEFT5 = 0x25
STEER_LEFT6 = 0x26
STEER_LEFT7 = 0x27
STEER_FIX_CENTER = 0x28
STEER_RIGHT7 = 0x29
STEER_RIGHT6 = 0x2A
STEER_RIGHT5 = 0x2B
STEER_RIGHT4 = 0x2C
STEER_RIGHT3 = 0x2D
STEER_RIGHT2 = 0x2E
STEER_RIGHT1 = 0x2F

# Head
HEAD_H_FLAG = 0x30
HEAD_V_FLAG = 0x31
HEAD_H_R_RANGE = 83
HEAD_H_L_RANGE = 94
HEAD_V_UP_RANGE = 90
HEAD_V_DOWN_RANGE = 25
HEAD_H_CENTER = 83
HEAD_V_CENTER = 150

# CONTROL SOURCE
CONTROL_SRC_NONE = 0x00
CONTROL_SRC_JOYSTICK = 0x01
CONTROL_SRC_IRREMOTE = 0x02
CONTROL_SRC_SERVER = 0x03
CONTROL_SRC_DB = 0x04
CONTROL_SRC_SUB1 = 0x11
CONTROL_SRC_SUB2 = 0x12
CONTROL_SRC_SUB3 = 0x13
CONTROL_SRC_SUB4 = 0x14
CONTROL_SRC_SUB5 = 0x15

CONTROL_SRC_DEFAULT = CONTROL_SRC_JOYSTICK

# MOVE STATES
MOVE_STATE_STOP = 0x00
MOVE_STATE_MOVE = 0x01

# STATUS
STATUS_INIT = 0x00
STATUS_READY = 0x01
STATUS_RUNNING = 0x02
STATUS_STOP = 0x03
STATUS_EXIT = 0x04

# STATUS STATES
#STATUS_STATE_OK = 0x00
#STATUS_STATE_WARNING = 0x01
#STATUS_STATE_ERROR = 0x02
#STATUS_STATE_LOWBAT1 = 0x03
#STATUS_STATE_LOWBAT2 = 0x04

# JOYSTICK
JOYSTICK_PATH = '/dev/input/js0'
JOYSTICK_READY_TIMEOUT = 60

# These constants were borrowed from linux/input.h
JOYSTICK_AXIS_NAMES = {
    0x00: 'x',
    0x01: 'y',
    0x02: 'z',
    0x03: 'rx',
    0x04: 'ry',
    0x05: 'rz',
    0x06: 'trottle',
    0x07: 'rudder',
    0x08: 'wheel',
    0x09: 'gas',
    0x0a: 'brake',
    0x10: 'hat0x',
    0x11: 'hat0y',
    0x12: 'hat1x',
    0x13: 'hat1y',
    0x14: 'hat2x',
    0x15: 'hat2y',
    0x16: 'hat3x',
    0x17: 'hat3y',
    0x18: 'pressure',
    0x19: 'distance',
    0x1a: 'tilt_x',
    0x1b: 'tilt_y',
    0x1c: 'tool_width',
    0x20: 'volume',
    0x28: 'misc',
}

JOYSTICK_BUTTON_NAMES = {
    0x120: 'trigger',
    0x121: 'thumb',
    0x122: 'thumb2',
    0x123: 'top',
    0x124: 'top2',
    0x125: 'pinkie',
    0x126: 'base',
    0x127: 'base2',
    0x128: 'base3',
    0x129: 'base4',
    0x12a: 'base5',
    0x12b: 'base6',
    0x12f: 'dead',
    0x130: 'a',
    0x131: 'b',
    0x132: 'c',
    0x133: 'x',
    0x134: 'y',
    0x135: 'z',
    0x136: 'tl',
    0x137: 'tr',
    0x138: 'tl2',
    0x139: 'tr2',
    0x13a: 'select',
    0x13b: 'start',
    0x13c: 'mode',
    0x13d: 'thumbl',
    0x13e: 'thumbr',

    0x220: 'dpad_up',
    0x221: 'dpad_down',
    0x222: 'dpad_left',
    0x223: 'dpad_right',

    # XBox 360 controller uses these codes.
    0x2c0: 'dpad_left',
    0x2c1: 'dpad_right',
    0x2c2: 'dpad_up',
    0x2c3: 'dpad_down',
}

# JOYSTICK BINDS
JOYSTICK_MOVE_AXIS = 'y'
JOYSTICK_STEER_AXIS = 'x'
JOYSTICK_HEAD_V_AXIS = 'rz'
JOYSTICK_HEAD_H_AXIS = 'z'
JOYSTICK_START_BTN_1 = 'tl2'
JOYSTICK_START_BTN_2 = 'tr2'

# HXpower
POWER_ADDRESS = 0x05
POWER_RESPONSE_DELAY = 0.05  # Delay to get response after send command, sec
POWER_DELAY = 0.3
POWER_ERROR_LIMIT = 3
POWER_PF_TIMEOUT = 60

# Commands
# VOLTMETERS
COM_GET_VIN = 0x01
COM_GET_VDC = 0x02
COM_GET_VBT = 0x03
COM_GET_VBA = 0x04
COM_GET_VIS = 0x05
COM_GET_VZU = 0x06
COM_GET_VCC = 0x07

# AMPERMETERS
COM_GET_AHX = 0x21
COM_GET_ABA = 0x22
COM_GET_AIN = 0x23
COM_GET_ADC = 0x24

# STATUS
COM_GET_STAT1 = 0x08
# 0x01 - vcc_ok
# 0x02 - vin_ok
# 0x04 - vdc_ok
# 0x08 - vbt_ok
# 0x10 - vba_ok
# 0x20 - lr_enabled
# 0x40 - ba_enabled
# 0x80 - ba_blocked

COM_GET_STAT2 = 0x09
# 0x01 - bl_powered
# 0x02 - bl_error
# 0x04 - pf_complete
# 0x08 - pf_charge
# 0x10 - vis_ok
# 0x20 - vzu_ok
# 0x40 - pf_enabled
# 0x80 - bl_enabled

COM_GET_STAT3 = 0x20
# 0x01 - ahx_ok
# 0x02 - aba_ok
# 0x04 - ain_ok
# 0x08 - adc_ok

# TEMP
COM_GET_POWER_TEMP = 0x10

VIN_MIN = 10.0
VIN_MAX = 14.0
VIN_LOW = 11.0
VIN_HIGH = 13.0

VDC_MIN = 5.0
VDC_MAX = 5.7
VDC_LOW = 5.1
VDC_HIGH = 5.5

VBT_MIN = 3.65
VBT_MAX = 4.25
VBT_LOW = 3.68
VBT_FULL = 4.1

VBA_MIN = 9.0
VBA_MAX = 12.8
VBA_LOW = 10.0
VBA_FULL = 12.4

VIS_MIN = 4.5
VIS_MAX = 5.3
VIS_LOW = 4.7
VIS_HIGH = 5.2

VZU_MIN = 5.0
VZU_MAX = 6.3
VZU_LOW = 5.2
VZU_HIGH = 6.2

VCC_MIN = 4.5
VCC_MAX = 5.3
VCC_LOW = 4.7
VCC_HIGH = 5.1

AHX_MIN = 0.3
AHX_MAX = 2.0
AHX_LOW = 0.4
AHX_HIGH = 1.2

ABA_MIN = 0.05
ABA_MAX = 2.0
ABA_LOW = 0.5
ABA_HIGH = 1.5

AIN_MIN = 0.05
AIN_MAX = 2.0
AIN_LOW = 0.1
AIN_HIGH = 1.5

ADC_MIN = 0.05
ADC_MAX = 3.0
ADC_LOW = 0.1
ADC_HIGH = 1.5

# RELAYS
COM_POWER_ON_BL = 0x11  # 17
COM_POWER_ON_PF = 0x12  # 18
COM_SWITCH_TO_IS = 0x13  # 19
COM_SWITCH_TO_IN = 0x14  # 20

COM_SHUTDOWN_BL = 0x15  # 21
COM_SHUTDOWN_PF = 0x16  # 22
COM_SWITCH_TO_LR = 0x17  # 23
COM_SWITCH_TO_BA = 0x18  # 24

COM_ENABLE_PF = 0x1B  # 27
COM_DISABLE_PF = 0x1C  # 28

COM_ENABLE_BL = 0x1D  # 29
COM_DISABLE_BL = 0x1E  # 30

# HXserver
SRV_PORT = 50306
SRV_HOST = ''
SRV_CMD_GET_ALL = 0x01
SRV_TIMEOUT = 1
SRV_MAX_ERR_COUNT = 3

# RETURN CODES
# Universal return codes
OK_RSP = 0x00
NO_RSP = 0xFF
ERR_RSP = 0x01
BLK_RSP = 0x02
CSE_RSP = 0x03
IOE_RSP = 0x04
TMO_RSP = 0x05

# HXbot modul flags
I2C_BUS_ERR = 0x0001
HXPOWER_ERR = 0x0002
REFLEX_ERR = 0x0004
SERVER_ERR = 0x0008

JOYSTICK_WRN = 0x0010
HXCAMERA_WRN = 0x0020
HXLCD_WRN = 0x0040
HXDB_WRN = 0x0080
HXLIGHT_WRN = 0x0100
HXSOUND_WRN = 0x0200
HXLOG_WRN = 0x0400

# HXlcd
LCD_ADDRESS = 0x06
LCD_ERROR_LIMIT = 5
LCD_DELAY = 1
LCD_RESPONSE_DELAY = 0.05

# HXcam
CAM_MAX_WRN_COUNT = 3
CAM_DELAY = 1.0

CAM_STATE_STP = 0
CAM_STATE_RUN = 1
CAM_STATE_WRN = 2
CAM_STATE_ERR = 3

# HXdb
DB_DELAY = 1.0

DB_HOST = 'localhost'
DB_NAME = 'hxdb'
DB_USER = 'hxdb'
DB_PASS = 'hxdbP@S$'