diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 2b568a965..fd108b568 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -121,7 +121,18 @@ int http_fn_index(http_request_t *request) { } } for(i = 0; i < CHANNEL_MAX; i++) { - if(BIT_CHECK(relayFlags,i)) { + int channelType; + + channelType = CHANNEL_GetType(i); + + if(channelType == ChType_Temperature) { + int iValue; + + iValue = CHANNEL_Get(i); + + hprintf128(request,"Temperature Channel %i value %i C
",i, iValue); + + } else if(BIT_CHECK(relayFlags,i)) { const char *c; if(CHANNEL_Check(i)) { c = "r"; @@ -132,7 +143,7 @@ int http_fn_index(http_request_t *request) { hprintf128(request,"",i); hprintf128(request,"",c,i); } - if(BIT_CHECK(pwmFlags,i)) { + else if(BIT_CHECK(pwmFlags,i)) { int pwmValue; pwmValue = CHANNEL_Get(i); diff --git a/src/i2c/drv_i2c_local.h b/src/i2c/drv_i2c_local.h new file mode 100644 index 000000000..32e380217 --- /dev/null +++ b/src/i2c/drv_i2c_local.h @@ -0,0 +1,33 @@ + +enum i2cDeviceType_e { + I2CDEV_UNKNOWN, + I2CDEV_TC74, +}; + +typedef enum i2cBusType_e { + I2C_BUS_ERROR, + I2C_BUS_I2C1, + I2C_BUS_I2C2, + +} i2cBusType_t; + +typedef struct i2cDevice_s { + int busType; + int addr; + int type; + struct i2cDevice_s *next; +} i2cDevice_t; + +typedef struct i2cDevice_TC74_s { + i2cDevice_t base; + // private TC74 variables + // Our channel index to save the result temp + int targetChannel; +} i2cDevice_TC74_t; + + +void DRV_I2C_Write(UINT8 addr, UINT8 data); +void DRV_I2C_Read(UINT8 addr, UINT8 *data); +int DRV_I2C_Begin(int dev_adr); +void DRV_I2C_Close(); + diff --git a/src/i2c/drv_i2c_main.c b/src/i2c/drv_i2c_main.c new file mode 100644 index 000000000..94eba8afd --- /dev/null +++ b/src/i2c/drv_i2c_main.c @@ -0,0 +1,148 @@ +#include "../new_common.h" +#include "../new_pins.h" +#include "../new_cfg.h" +#include "../new_cmd.h" +#include "../logging/logging.h" +#include "drv_i2c_local.h" + + +#if PLATFORM_BK7231T + +#include "i2c_pub.h" +I2C_OP_ST i2c_operater; +DD_HANDLE i2c_hdl; + +void DRV_I2C_Write(UINT8 addr, UINT8 data) +{ + char buff = (char)data; + + i2c_operater.op_addr = addr; + ddev_write(i2c_hdl, &buff, 1, (UINT32)&i2c_operater); +} + +void DRV_I2C_Read(UINT8 addr, UINT8 *data) +{ + i2c_operater.op_addr = addr; + ddev_read(i2c_hdl, (char*)data, 1, (UINT32)&i2c_operater); +} +int DRV_I2C_Begin(int dev_adr) { + + uint32_t status; + uint32_t oflag; + oflag = I2C_DEF_DIV; + + i2c_hdl = ddev_open("i2c1", &status, oflag); + if(DD_HANDLE_UNVALID == i2c_hdl){ + addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"DRV_I2C_Begin ddev_open failed, status %i!\n",status); + return 1; + } + addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"DRV_I2C_Begin ddev_open OK, adr %i!\n",dev_adr); + + i2c_operater.salve_id = dev_adr; + + return 0; +} +void DRV_I2C_Close() { + + ddev_close(i2c_hdl); +} +#else + +void DRV_I2C_Write(UINT8 addr, UINT8 data) +{ +} + +void DRV_I2C_Read(UINT8 addr, UINT8 *data) +{ +} +int DRV_I2C_Begin(int dev_adr) { + + return 1; // error +} +void DRV_I2C_Close() { + +} +#endif + + +i2cDevice_t *g_i2c_devices = 0; + +i2cBusType_t DRV_I2C_ParseBusType(const char *s) { + if(!stricmp(s,"I2C1")) + return I2C_BUS_I2C1; + if(!stricmp(s,"I2C2")) + return I2C_BUS_I2C2; + return I2C_BUS_ERROR; +} +void DRV_I2C_AddNextDevice(i2cDevice_t *t) { + t->next = g_i2c_devices; + g_i2c_devices = t; +} +void DRV_I2C_AddDevice_TC74_Internal(int busType,int address, int targetChannel) { + i2cDevice_TC74_t *dev; + + dev = malloc(sizeof(i2cDevice_TC74_t)); + + dev->base.addr = address; + dev->base.busType = busType; + dev->base.type = I2CDEV_TC74; + dev->base.next = 0; + dev->targetChannel = targetChannel; + + DRV_I2C_AddNextDevice(dev); +} +int DRV_I2C_AddDevice_TC74(const void *context, const char *cmd, const char *args) { + const char *i2cModuleStr; + int address; + int targetChannel; + i2cBusType_t busType; + + Tokenizer_TokenizeString(args); + i2cModuleStr = Tokenizer_GetArg(0); + address = Tokenizer_GetArgInteger(1); + targetChannel = Tokenizer_GetArgInteger(2); + + //addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"DRV_I2C_AddDevice_TC74: module %s, address %i, target %i\n", i2cModuleStr, address, targetChannel); + + busType = DRV_I2C_ParseBusType(i2cModuleStr); + + DRV_I2C_AddDevice_TC74_Internal(busType,address,targetChannel); + + return 1; +} +// TC74 A0 (address type 0) +// setChannelType 5 temperature +// addI2CDevice_TC74 I2C1 0x48 5 +// TC74 A2 (address type 2) +// setChannelType 6 temperature +// addI2CDevice_TC74 I2C1 0x4A 6 +void DRV_I2C_Init() +{ + CMD_RegisterCommand("addI2CDevice_TC74","",DRV_I2C_AddDevice_TC74, "Adds a new I2C device", NULL); + +} +void DRC_I2C_RunDevice(i2cDevice_t *dev) +{ + switch(dev->type) + { + case I2CDEV_TC74: + DRV_I2C_TC74_RunDevice(dev); + break; + default: + addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"DRC_I2C_RunDevice: error, device of type %i at adr %i not handled\n", dev->type, dev->addr); + break; + + + } +} +void DRV_I2C_EverySecond() +{ + i2cDevice_t *cur; + + cur = g_i2c_devices; + while(cur) { + //addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"DRV_I2C_EverySecond: going to run device of type %i with addr %i\n", cur->type, cur->addr); + DRC_I2C_RunDevice(cur); + cur = cur->next; + } +} diff --git a/src/i2c/drv_i2c_tc74.c b/src/i2c/drv_i2c_tc74.c new file mode 100644 index 000000000..67918fb9b --- /dev/null +++ b/src/i2c/drv_i2c_tc74.c @@ -0,0 +1,34 @@ +#include "../new_common.h" +#include "../new_pins.h" +#include "../new_cfg.h" +#include "../new_cmd.h" +#include "../logging/logging.h" +#include "drv_i2c_local.h" + +int DRV_I2C_TC74_readTemperature(int dev_adr) +{ + byte temp; + + addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"DRV_I2C_TC74_readTemperature: called for addr %i\n", dev_adr); + + DRV_I2C_Begin(dev_adr); + DRV_I2C_Write(0,0x00); + DRV_I2C_Read(0x00,&temp); + DRV_I2C_Close(); + + addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"DRV_I2C_TC74_readTemperature: result is %i\n", temp); + + return temp; + +} +void DRV_I2C_TC74_RunDevice(i2cDevice_t *dev) +{ + i2cDevice_TC74_t *tc74; + byte temp; + + tc74 = (i2cDevice_TC74_t*)dev; + + temp = DRV_I2C_TC74_readTemperature(tc74->base.addr); + + CHANNEL_Set(tc74->targetChannel, temp, 0); +} \ No newline at end of file diff --git a/src/new_cmd.c b/src/new_cmd.c index 35eea2ee8..46bd3d2b1 100644 --- a/src/new_cmd.c +++ b/src/new_cmd.c @@ -89,9 +89,9 @@ command_t *CMD_Find(const char *name) { #define MAX_CMD_LEN 512 #define MAX_ARGS 32 -//static char g_buffer[MAX_CMD_LEN]; -//static char *g_args[MAX_ARGS]; -//static int g_numArgs = 0; +static char g_buffer[MAX_CMD_LEN]; +static char *g_args[MAX_ARGS]; +static int g_numArgs = 0; bool isWhiteSpace(char ch) { if(ch == ' ') @@ -104,15 +104,54 @@ bool isWhiteSpace(char ch) { return true; return false; } -// NOTE: arg0 is command name -//int CMD_GetArgsCount() { -// return g_numArgs; -//} -// NOTE: arg0 is command name -//const char *CMD_GetArg(int i) { -// return g_args[i]; -//} +int Tokenizer_GetArgsCount() { + return g_numArgs; +} +const char *Tokenizer_GetArg(int i) { + return g_args[i]; +} +int Tokenizer_GetArgInteger(int i) { + const char *s; + s = g_args[i]; + if(s[0] == '0' && s[1] == 'x') { + int ret; + sscanf(s, "%x", &ret); + return ret; + } + return atoi(s); +} +void Tokenizer_TokenizeString(const char *s) { + int r = 0; + char *p; + int i; + while(isWhiteSpace(*s)) { + s++; + } + + strcpy(g_buffer,s); + p = g_buffer; + g_numArgs = 0; + g_args[g_numArgs] = p; + g_numArgs++; + while(*p != 0) { + if(isWhiteSpace(*p)) { + *p = 0; + if((p[1] != 0)) { + g_args[g_numArgs] = p+1; + g_numArgs++; + } + } + if(*p == ',') { + *p = 0; + g_args[g_numArgs] = p+1; + g_numArgs++; + } + p++; + } + + +} // get a string up to whitespace. // if stripnum is set, stop at numbers. int get_cmd(const char *s, char *dest, int maxlen, int stripnum){ diff --git a/src/new_pins.c b/src/new_pins.c index a0bb68f4c..3a65b72bb 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -109,52 +109,7 @@ int TC74_readTemp_method1(int adr) return t; } -#include "i2c_pub.h" -I2C_OP_ST i2c_operater; -DD_HANDLE i2c_hdl; -static void camera_intf_sccb_write(UINT8 addr, UINT8 data) -{ - char buff = (char)data; - - i2c_operater.op_addr = addr; - ddev_write(i2c_hdl, &buff, 1, (UINT32)&i2c_operater); -} -void camera_intf_sccb_read(UINT8 addr, UINT8 *data) -{ - i2c_operater.op_addr = addr; - ddev_read(i2c_hdl, (char*)data, 1, (UINT32)&i2c_operater); -} -int TC74_readTemp_method2(int dev_adr) -{ - byte buffer[9]; - int t; - uint32_t status; - uint32_t oflag; - oflag = I2C_DEF_DIV; - - //i2c1_init(); - i2c_hdl = ddev_open("i2c1", &status, oflag); - if(DD_HANDLE_UNVALID == i2c_hdl){ - addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"TC74_readTemp_method2 ddev_open failed, status %i!\n",status); - return -1; - } - - i2c_operater.salve_id = dev_adr; - - addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"TC74_readTemp_method2 ddev_open OK!\n"); - - - camera_intf_sccb_write(0,0x00); - - camera_intf_sccb_read(0x00,&buffer[0]); - t = buffer[0]; - - - ddev_close(i2c_hdl); - return t; - -} ///*/*/*int TC74_readTemp_method2(int adr) //{ @@ -382,47 +337,6 @@ static void I2CWRNBYTE_CODEC(unsigned char reg, unsigned char val) } -void run_i2c_test() -{ - int res; - addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"Will do I2C attemt %i!\n",attempt); - attempt++; - // TC74A2 - res = TC74_readTemp_method2(0x4A); - addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"Temp result is %i!\n",res); - //res = TC74_readTemp_method3(0x4A); - //addLogAdv(LOG_INFO, LOG_FEATURE_I2C,"Temp result is %i!\n",res); -} -void my_i2c_test(void *p) -{ - - attempt = 0; -/// delay_ms(500); -/// while(1) - ///{ - //// delay_ms(500); - run_i2c_test(); - - ////} -} -xTaskHandle g_i2c_test = NULL; -void start_i2c_test() -{ - OSStatus err = kNoErr; - - - err = rtos_init_timer(&g_i2c_test, - 1 * 1000, - my_i2c_test, - (void *)0); - - - if(err != kNoErr) - { - ADDLOG_ERROR(LOG_FEATURE_I2C, "create \"I2C\" thread failed!\r\n"); - } - rtos_start_timer(&g_i2c_test); -} void testI2C_method1() { char buffer[8]; @@ -515,7 +429,10 @@ void PIN_XR809_GetPortPinForIndex(int index, int *xr_port, int *xr_pin) { #endif // it was nice to have it as bits but now that we support PWM... //int g_channelStates; -unsigned char g_channelValues[GPIO_MAX] = { 0 }; +int g_channelValues[GPIO_MAX] = { 0 }; +// channel content types, mostly not required to set +int g_channelTypes[GPIO_MAX] = { 0 }; + pinButton_s g_buttons[GPIO_MAX]; @@ -721,6 +638,12 @@ void NEW_button_init(pinButton_s* handle, uint8_t(*pin_level)(void *self), uint8 handle->button_level = handle->hal_button_Level(handle); handle->active_level = active_level; } +void CHANNEL_SetType(int ch, int type) { + g_channelTypes[ch] = type; +} +int CHANNEL_GetType(int ch) { + return g_channelTypes[ch]; +} void CHANNEL_SetAll(int iVal) { int i; @@ -1233,6 +1156,39 @@ void PIN_ticks(void *param) } } } +int CHANNEL_ParseChannelType(const char *s) { + if(!stricmp(s,"temperature") || !stricmp(s,"temp")) + return ChType_Temperature; + if(!stricmp(s,"default") ) + return ChType_Default; + return ChType_Error; +} +static int CMD_SetChannelType(const void *context, const char *cmd, const char *args){ + int channel; + const char *type; + int typeCode; + + Tokenizer_TokenizeString(args); + + if(Tokenizer_GetArgsCount() < 2) { + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL,"This command requires 2 arguments"); + return 1; + } + channel = Tokenizer_GetArgInteger(0); + type = Tokenizer_GetArg(1); + + typeCode = CHANNEL_ParseChannelType(type); + if(typeCode == ChType_Error) { + + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL,"Channel %i type not set because %s is not a known type", channel,type); + return 1; + } + + CHANNEL_SetType(channel,typeCode); + + addLogAdv(LOG_INFO, LOG_FEATURE_GENERAL,"Channel %i type changed to %s", channel,type); + return 0; +} static int showgpi(const void *context, const char *cmd, const char *args){ int i; @@ -1296,8 +1252,8 @@ void PIN_Init(void) CMD_RegisterCommand("showgpi", NULL, showgpi, "log stat of all GPIs", NULL); + CMD_RegisterCommand("setChannelType", NULL, CMD_SetChannelType, "qqqqqqqq", NULL); } - void PIN_set_wifi_led(int value){ int res = -1; int i; diff --git a/src/new_pins.h b/src/new_pins.h index 850373ee8..42fdf4d6e 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -23,6 +23,12 @@ enum IORole { IOR_Total_Options, }; +enum ChannelType { + ChType_Default, + ChType_Error, + ChType_Temperature, + +}; // NOTE: you must keep size of this structure in sync with // structures in flash config code for both BK7231 and XR809 typedef struct pinsState_s { @@ -58,6 +64,9 @@ void PIN_SetGenericDoubleClickCallback(void (*cb)(int pinIndex)); void CHANNEL_Set(int ch, int iVal, int bForce); int CHANNEL_Get(int ch); int CHANNEL_GetRoleForOutputChannel(int ch); +// See: enum ChannelType +void CHANNEL_SetType(int ch, int type); +int CHANNEL_GetType(int ch); void PIN_SaveToFlash(); void PIN_LoadFromFlash(); diff --git a/src/user_main.c b/src/user_main.c index 75ffe6be2..69a4ca0ee 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -223,6 +223,7 @@ static void app_led_timer_handler(void *data) MQTT_RunEverySecondUpdate(); TuyaMCU_RunFrame(); NTP_OnEverySecond(); + DRV_I2C_EverySecond(); g_secondsElapsed ++; ADDLOGF_INFO("Timer is %i free mem %d\n", g_secondsElapsed, xPortGetFreeHeapSize()); @@ -369,6 +370,7 @@ void user_main(void) CFG_InitAndLoad(); TuyaMCU_Init(); + DRV_I2C_Init(); wifi_ssid = CFG_GetWiFiSSID(); wifi_pass = CFG_GetWiFiPass(); @@ -442,7 +444,6 @@ void user_main(void) CMD_ExecuteCommand("exec autoexec.bat"); } - // start_i2c_test(); err = rtos_init_timer(&led_timer, diff --git a/windowsTest_msvc2008.vcproj b/windowsTest_msvc2008.vcproj index ae72488c3..5ff3e5436 100644 --- a/windowsTest_msvc2008.vcproj +++ b/windowsTest_msvc2008.vcproj @@ -163,6 +163,18 @@ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx" UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" > + + + + + + @@ -307,18 +319,6 @@ /> - - - - -