Unverified Commit 11592951 authored by Arnim Läuger's avatar Arnim Läuger Committed by GitHub
Browse files

Merge pull request #2582 from nodemcu/dev

Next master drop
parents 4095c408 61433c44
...@@ -44,7 +44,7 @@ INCLUDES += -I ../libc ...@@ -44,7 +44,7 @@ INCLUDES += -I ../libc
INCLUDES += -I ../coap INCLUDES += -I ../coap
INCLUDES += -I ../mqtt INCLUDES += -I ../mqtt
INCLUDES += -I ../u8g2lib/u8g2/src/clib INCLUDES += -I ../u8g2lib/u8g2/src/clib
INCLUDES += -I ../ucglib INCLUDES += -I ../ucglib/ucg/src/clib
INCLUDES += -I ../lua INCLUDES += -I ../lua
INCLUDES += -I ../pcm INCLUDES += -I ../pcm
INCLUDES += -I ../platform INCLUDES += -I ../platform
......
...@@ -32,18 +32,23 @@ static size_t cronent_count = 0; ...@@ -32,18 +32,23 @@ static size_t cronent_count = 0;
static uint64_t lcron_parsepart(lua_State *L, char *str, char **end, uint8_t min, uint8_t max) { static uint64_t lcron_parsepart(lua_State *L, char *str, char **end, uint8_t min, uint8_t max) {
uint64_t res = 0; uint64_t res = 0;
/* Gobble whitespace before potential stars; no strtol on that path */
while (*str != '\0' && (*str == ' ' || *str == '\t')) {
str++;
}
if (str[0] == '*') { if (str[0] == '*') {
uint32_t each = 1; uint32_t each = 1;
*end = str + 1; *end = str + 1;
if (str[1] == '/') { if (str[1] == '/') {
each = strtol(str + 2, end, 10); each = strtol(str + 2, end, 10);
if (end != 0)
if (each == 0 || each >= max - min) { if (each == 0 || each >= max - min) {
return luaL_error(L, "invalid spec (each %d)", each); return luaL_error(L, "invalid spec (each %d)", each);
} }
} }
for (int i = 0; i <= (max - min); i++) { for (int i = 0; i <= (max - min); i++) {
if (((min + i) % each) == 0) res |= (uint64_t)1 << i; if ((i % each) == 0) res |= (uint64_t)1 << i;
} }
} else { } else {
uint32_t val; uint32_t val;
...@@ -63,14 +68,17 @@ static uint64_t lcron_parsepart(lua_State *L, char *str, char **end, uint8_t min ...@@ -63,14 +68,17 @@ static uint64_t lcron_parsepart(lua_State *L, char *str, char **end, uint8_t min
static int lcron_parsedesc(lua_State *L, char *str, struct cronent_desc *desc) { static int lcron_parsedesc(lua_State *L, char *str, struct cronent_desc *desc) {
char *s = str; char *s = str;
desc->min = lcron_parsepart(L, s, &s, 0, 59); desc->min = lcron_parsepart(L, s, &s, 0, 59);
if (*s != ' ') return luaL_error(L, "invalid spec (separator @%d)", s - str); if (*s != ' ' && *s != '\t') return luaL_error(L, "invalid spec (separator @%d)", s - str);
desc->hour = lcron_parsepart(L, s + 1, &s, 0, 23); desc->hour = lcron_parsepart(L, s + 1, &s, 0, 23);
if (*s != ' ') return luaL_error(L, "invalid spec (separator @%d)", s - str); if (*s != ' ' && *s != '\t') return luaL_error(L, "invalid spec (separator @%d)", s - str);
desc->dom = lcron_parsepart(L, s + 1, &s, 1, 31); desc->dom = lcron_parsepart(L, s + 1, &s, 1, 31);
if (*s != ' ') return luaL_error(L, "invalid spec (separator @%d)", s - str); if (*s != ' ' && *s != '\t') return luaL_error(L, "invalid spec (separator @%d)", s - str);
desc->mon = lcron_parsepart(L, s + 1, &s, 1, 12); desc->mon = lcron_parsepart(L, s + 1, &s, 1, 12);
if (*s != ' ') return luaL_error(L, "invalid spec (separator @%d)", s - str); if (*s != ' ' && *s != '\t') return luaL_error(L, "invalid spec (separator @%d)", s - str);
desc->dow = lcron_parsepart(L, s + 1, &s, 0, 6); desc->dow = lcron_parsepart(L, s + 1, &s, 0, 6);
while (*s != '\0' && (*s == ' ' || *s == '\t')) {
s++;
}
if (*s != 0) return luaL_error(L, "invalid spec (trailing @%d)", s - str); if (*s != 0) return luaL_error(L, "invalid spec (trailing @%d)", s - str);
return 0; return 0;
} }
...@@ -78,6 +86,7 @@ static int lcron_parsedesc(lua_State *L, char *str, struct cronent_desc *desc) { ...@@ -78,6 +86,7 @@ static int lcron_parsedesc(lua_State *L, char *str, struct cronent_desc *desc) {
static int lcron_create(lua_State *L) { static int lcron_create(lua_State *L) {
// Check arguments // Check arguments
char *strdesc = (char*)luaL_checkstring(L, 1); char *strdesc = (char*)luaL_checkstring(L, 1);
void *newlist;
luaL_checkanyfunction(L, 2); luaL_checkanyfunction(L, 2);
// Parse description // Parse description
struct cronent_desc desc; struct cronent_desc desc;
...@@ -93,8 +102,12 @@ static int lcron_create(lua_State *L) { ...@@ -93,8 +102,12 @@ static int lcron_create(lua_State *L) {
// Set entry // Set entry
ud->desc = desc; ud->desc = desc;
// Store entry // Store entry
newlist = os_realloc(cronent_list, sizeof(int) * (cronent_count + 1));
if (newlist == NULL) {
return luaL_error(L, "out of memory");
}
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
cronent_list = os_realloc(cronent_list, sizeof(int) * (cronent_count + 1)); cronent_list = newlist;
cronent_list[cronent_count++] = luaL_ref(L, LUA_REGISTRYINDEX); cronent_list[cronent_count++] = luaL_ref(L, LUA_REGISTRYINDEX);
return 1; return 1;
} }
...@@ -120,8 +133,13 @@ static int lcron_schedule(lua_State *L) { ...@@ -120,8 +133,13 @@ static int lcron_schedule(lua_State *L) {
ud->desc = desc; ud->desc = desc;
size_t i = lcron_findindex(L, ud); size_t i = lcron_findindex(L, ud);
if (i == -1) { if (i == -1) {
void *newlist;
newlist = os_realloc(cronent_list, sizeof(int) * (cronent_count + 1));
if (newlist == NULL) {
return luaL_error(L, "out of memory");
}
cronent_list = newlist;
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
cronent_list = os_realloc(cronent_list, sizeof(int) * (cronent_count + 1));
cronent_list[cronent_count++] = lua_ref(L, LUA_REGISTRYINDEX); cronent_list[cronent_count++] = lua_ref(L, LUA_REGISTRYINDEX);
} }
return 0; return 0;
......
...@@ -67,6 +67,9 @@ static int ds18b20_lua_readoutdone(void); ...@@ -67,6 +67,9 @@ static int ds18b20_lua_readoutdone(void);
// Setup onewire bus for DS18B20 temperature sensors // Setup onewire bus for DS18B20 temperature sensors
// Lua: ds18b20.setup(OW_BUS_PIN) // Lua: ds18b20.setup(OW_BUS_PIN)
static int ds18b20_lua_setup(lua_State *L) { static int ds18b20_lua_setup(lua_State *L) {
platform_print_deprecation_note("ds18b20 C module superseded by Lua implementation", "soon");
// check ow bus pin value // check ow bus pin value
if (!lua_isnumber(L, 1) || lua_isnumber(L, 1) == 0) { if (!lua_isnumber(L, 1) || lua_isnumber(L, 1) == 0) {
return luaL_error(L, "wrong 1-wire pin"); return luaL_error(L, "wrong 1-wire pin");
......
...@@ -587,7 +587,7 @@ static int file_mount( lua_State *L ) ...@@ -587,7 +587,7 @@ static int file_mount( lua_State *L )
if (vol->vol = vfs_mount( ldrv, num )) { if (vol->vol = vfs_mount( ldrv, num )) {
/* set its metatable */ /* set its metatable */
luaL_getmetatable(L, "vfs.vol"); luaL_getmetatable(L, "file.vol");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
return 1; return 1;
} else { } else {
......
This diff is collapsed.
...@@ -378,22 +378,6 @@ static int tmr_create( lua_State *L ) { ...@@ -378,22 +378,6 @@ static int tmr_create( lua_State *L ) {
} }
#if defined(ENABLE_TIMER_SUSPEND) && defined(SWTMR_DEBUG)
static void tmr_printRegistry(lua_State* L){
swtmr_print_registry();
}
static void tmr_printSuspended(lua_State* L){
swtmr_print_suspended();
}
static void tmr_printTimerlist(lua_State* L){
swtmr_print_timer_list();
}
#endif
// Module function map // Module function map
static const LUA_REG_TYPE tmr_dyn_map[] = { static const LUA_REG_TYPE tmr_dyn_map[] = {
......
This diff is collapsed.
...@@ -570,9 +570,9 @@ static int packet_map_lookup(lua_State *L) { ...@@ -570,9 +570,9 @@ static int packet_map_lookup(lua_State *L) {
} }
// Now search the packet function map // Now search the packet function map
const TValue *res = luaR_findentry((void *) packet_function_map, field, 0, NULL); lua_pushrotable(L, (void *)packet_function_map);
if (res) { lua_getfield(L, -1, field);
luaA_pushobject(L, res); if (!lua_isnil(L, -1)) {
return 1; return 1;
} }
} }
......
...@@ -126,12 +126,16 @@ void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buff ...@@ -126,12 +126,16 @@ void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buff
connection->buffer_length = buffer_length; connection->buffer_length = buffer_length;
} }
int mqtt_get_total_length(uint8_t* buffer, uint16_t length) // Returns total length of message, or -1 if not enough bytes are available
int32_t mqtt_get_total_length(uint8_t* buffer, uint16_t buffer_length)
{ {
int i; int i;
int totlen = 0; int totlen = 0;
for(i = 1; i < length; ++i) if(buffer_length == 1)
return -1;
for(i = 1; i < buffer_length; ++i)
{ {
totlen += (buffer[i] & 0x7f) << (7 * (i - 1)); totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
if((buffer[i] & 0x80) == 0) if((buffer[i] & 0x80) == 0)
...@@ -139,19 +143,23 @@ int mqtt_get_total_length(uint8_t* buffer, uint16_t length) ...@@ -139,19 +143,23 @@ int mqtt_get_total_length(uint8_t* buffer, uint16_t length)
++i; ++i;
break; break;
} }
if(i == buffer_length)
return -1;
} }
totlen += i; totlen += i;
return totlen; return totlen;
} }
const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length) const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* buffer_length)
{ {
int i; int i;
int totlen = 0; int totlen = 0;
int topiclen; int topiclen;
for(i = 1; i < *length; ++i) for(i = 1; i < *buffer_length; ++i)
{ {
totlen += (buffer[i] & 0x7f) << (7 * (i -1)); totlen += (buffer[i] & 0x7f) << (7 * (i -1));
if((buffer[i] & 0x80) == 0) if((buffer[i] & 0x80) == 0)
...@@ -162,25 +170,25 @@ const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length) ...@@ -162,25 +170,25 @@ const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length)
} }
totlen += i; totlen += i;
if(i + 2 > *length) if(i + 2 > *buffer_length)
return NULL; return NULL;
topiclen = buffer[i++] << 8; topiclen = buffer[i++] << 8;
topiclen |= buffer[i++]; topiclen |= buffer[i++];
if(i + topiclen > *length) if(i + topiclen > *buffer_length)
return NULL; return NULL;
*length = topiclen; *buffer_length = topiclen;
return (const char*)(buffer + i); return (const char*)(buffer + i);
} }
const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length) const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* buffer_length)
{ {
int i; int i;
int totlen = 0; int totlen = 0;
int topiclen; int topiclen;
for(i = 1; i < *length; ++i) for(i = 1; i < *buffer_length; ++i)
{ {
totlen += (buffer[i] & 0x7f) << (7 * (i - 1)); totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
if((buffer[i] & 0x80) == 0) if((buffer[i] & 0x80) == 0)
...@@ -191,20 +199,20 @@ const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length) ...@@ -191,20 +199,20 @@ const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length)
} }
totlen += i; totlen += i;
if(i + 2 > *length) if(i + 2 > *buffer_length)
return NULL; return NULL;
topiclen = buffer[i++] << 8; topiclen = buffer[i++] << 8;
topiclen |= buffer[i++]; topiclen |= buffer[i++];
if(i + topiclen > *length){ if(i + topiclen > *buffer_length){
*length = 0; *buffer_length = 0;
return NULL; return NULL;
} }
i += topiclen; i += topiclen;
if(mqtt_get_qos(buffer) > 0) if(mqtt_get_qos(buffer) > 0)
{ {
if(i + 2 > *length) if(i + 2 > *buffer_length)
return NULL; return NULL;
i += 2; i += 2;
} }
...@@ -212,16 +220,16 @@ const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length) ...@@ -212,16 +220,16 @@ const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length)
if(totlen < i) if(totlen < i)
return NULL; return NULL;
if(totlen <= *length) if(totlen <= *buffer_length)
*length = totlen - i; *buffer_length = totlen - i;
else else
*length = *length - i; *buffer_length = *buffer_length - i;
return (const char*)(buffer + i); return (const char*)(buffer + i);
} }
uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length) uint16_t mqtt_get_id(uint8_t* buffer, uint16_t buffer_length)
{ {
if(length < 1) if(buffer_length < 1)
return 0; return 0;
switch(mqtt_get_type(buffer)) switch(mqtt_get_type(buffer))
...@@ -234,7 +242,7 @@ uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length) ...@@ -234,7 +242,7 @@ uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length)
if(mqtt_get_qos(buffer) <= 0) if(mqtt_get_qos(buffer) <= 0)
return 0; return 0;
for(i = 1; i < length; ++i) for(i = 1; i < buffer_length; ++i)
{ {
if((buffer[i] & 0x80) == 0) if((buffer[i] & 0x80) == 0)
{ {
...@@ -243,16 +251,16 @@ uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length) ...@@ -243,16 +251,16 @@ uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length)
} }
} }
if(i + 2 > length) if(i + 2 > buffer_length)
return 0; return 0;
topiclen = buffer[i++] << 8; topiclen = buffer[i++] << 8;
topiclen |= buffer[i++]; topiclen |= buffer[i++];
if(i + topiclen > length) if(i + topiclen > buffer_length)
return 0; return 0;
i += topiclen; i += topiclen;
if(i + 2 > length) if(i + 2 > buffer_length)
return 0; return 0;
return (buffer[i] << 8) | buffer[i + 1]; return (buffer[i] << 8) | buffer[i + 1];
...@@ -267,7 +275,7 @@ uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length) ...@@ -267,7 +275,7 @@ uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length)
{ {
// This requires the remaining length to be encoded in 1 byte, // This requires the remaining length to be encoded in 1 byte,
// which it should be. // which it should be.
if(length >= 4 && (buffer[1] & 0x80) == 0) if(buffer_length >= 4 && (buffer[1] & 0x80) == 0)
return (buffer[2] << 8) | buffer[3]; return (buffer[2] << 8) | buffer[3];
else else
return 0; return 0;
......
...@@ -108,21 +108,22 @@ typedef struct mqtt_connect_info ...@@ -108,21 +108,22 @@ typedef struct mqtt_connect_info
int will_qos; int will_qos;
int will_retain; int will_retain;
int clean_session; int clean_session;
uint16_t max_message_length;
} mqtt_connect_info_t; } mqtt_connect_info_t;
static inline int mqtt_get_type(uint8_t* buffer) { return (buffer[0] & 0xf0) >> 4; } static inline uint8_t mqtt_get_type(uint8_t* buffer) { return (buffer[0] & 0xf0) >> 4; }
static inline int mqtt_get_dup(uint8_t* buffer) { return (buffer[0] & 0x08) >> 3; } static inline uint8_t mqtt_get_dup(uint8_t* buffer) { return (buffer[0] & 0x08) >> 3; }
static inline int mqtt_get_qos(uint8_t* buffer) { return (buffer[0] & 0x06) >> 1; } static inline uint8_t mqtt_get_qos(uint8_t* buffer) { return (buffer[0] & 0x06) >> 1; }
static inline int mqtt_get_retain(uint8_t* buffer) { return (buffer[0] & 0x01); } static inline uint8_t mqtt_get_retain(uint8_t* buffer) { return (buffer[0] & 0x01); }
static inline int mqtt_get_connect_ret_code(uint8_t* buffer) { return (buffer[3]); } static inline uint8_t mqtt_get_connect_ret_code(uint8_t* buffer) { return (buffer[3]); }
void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length); void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length);
int mqtt_get_total_length(uint8_t* buffer, uint16_t length); int32_t mqtt_get_total_length(uint8_t* buffer, uint16_t buffer_length);
const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length); const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* buffer_length);
const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length); const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* buffer_length);
uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length); uint16_t mqtt_get_id(uint8_t* buffer, uint16_t buffer_length);
mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info); mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info);
mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id); mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id);
......
...@@ -44,6 +44,7 @@ INCLUDES += -I ../spiffs ...@@ -44,6 +44,7 @@ INCLUDES += -I ../spiffs
INCLUDES += -I ../libc INCLUDES += -I ../libc
INCLUDES += -I ../lua INCLUDES += -I ../lua
INCLUDES += -I ../u8g2lib/u8g2/src/clib INCLUDES += -I ../u8g2lib/u8g2/src/clib
INCLUDES += -I ../ucglib/ucg/src/clib
PDIR := ../$(PDIR) PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile sinclude $(PDIR)Makefile
...@@ -13,6 +13,42 @@ ...@@ -13,6 +13,42 @@
#define U8X8_USE_PINS #define U8X8_USE_PINS
#include "u8x8_nodemcu_hal.h" #include "u8x8_nodemcu_hal.h"
// static variables containing info about the i2c link
// TODO: move to user space in u8x8_t once available
typedef struct {
uint8_t id;
} hal_i2c_t;
// static variables containing info about the spi link
// TODO: move to user space in u8x8_t once available
typedef struct {
uint8_t host;
//spi_device_handle_t device;
uint8_t last_dc;
struct {
uint8_t *data;
size_t size, used;
} buffer;
} hal_spi_t;
static void flush_buffer_spi( hal_spi_t *hal )
{
if (hal->buffer.data && hal->buffer.used > 0) {
platform_spi_blkwrite( hal->host, hal->buffer.used, hal->buffer.data );
hal->buffer.used = 0;
}
}
static void force_flush_buffer(u8x8_t *u8x8)
{
// spi hal has a buffer that can be flushed
if (u8x8->byte_cb == u8x8_byte_nodemcu_spi) {
hal_spi_t *hal = ((u8g2_nodemcu_t *)u8x8)->hal;
flush_buffer_spi( hal );
}
}
uint8_t u8x8_gpio_and_delay_nodemcu(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) uint8_t u8x8_gpio_and_delay_nodemcu(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{ {
...@@ -35,24 +71,30 @@ uint8_t u8x8_gpio_and_delay_nodemcu(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, ...@@ -35,24 +71,30 @@ uint8_t u8x8_gpio_and_delay_nodemcu(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int,
break; break;
case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
force_flush_buffer(u8x8);
os_delay_us( 1 ); os_delay_us( 1 );
break; break;
case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
force_flush_buffer(u8x8);
temp = arg_int * 100; temp = arg_int * 100;
temp /= 1000; temp /= 1000;
os_delay_us( temp > 0 ? temp : 1 ); os_delay_us( temp > 0 ? temp : 1 );
break; break;
case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
force_flush_buffer(u8x8);
os_delay_us( arg_int * 10 ); os_delay_us( arg_int * 10 );
break; break;
case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
force_flush_buffer(u8x8);
os_delay_us( arg_int * 1000 ); os_delay_us( arg_int * 1000 );
system_soft_wdt_feed();
break; break;
case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
force_flush_buffer(u8x8);
temp = 5000 / arg_int; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us temp = 5000 / arg_int; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
temp /= 1000; temp /= 1000;
os_delay_us( temp > 0 ? temp : 1 ); os_delay_us( temp > 0 ? temp : 1 );
...@@ -119,12 +161,6 @@ uint8_t u8x8_gpio_and_delay_nodemcu(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, ...@@ -119,12 +161,6 @@ uint8_t u8x8_gpio_and_delay_nodemcu(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int,
} }
// static variables containing info about the i2c link
// TODO: move to user space in u8x8_t once available
typedef struct {
uint8_t id;
} hal_i2c_t;
uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{ {
uint8_t *data; uint8_t *data;
...@@ -132,11 +168,11 @@ uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * ...@@ -132,11 +168,11 @@ uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *
switch(msg) { switch(msg) {
case U8X8_MSG_BYTE_SEND: case U8X8_MSG_BYTE_SEND:
if (hal->id == 0) { if (hal->id < NUM_I2C) {
data = (uint8_t *)arg_ptr; data = (uint8_t *)arg_ptr;
while( arg_int > 0 ) { while( arg_int > 0 ) {
platform_i2c_send_byte( 0, *data ); platform_i2c_send_byte( hal->id, *data );
data++; data++;
arg_int--; arg_int--;
} }
...@@ -163,9 +199,9 @@ uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * ...@@ -163,9 +199,9 @@ uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *
break; break;
case U8X8_MSG_BYTE_START_TRANSFER: case U8X8_MSG_BYTE_START_TRANSFER:
if (hal->id == 0) { if (hal->id < NUM_I2C) {
platform_i2c_send_start( 0 ); platform_i2c_send_start( hal->id );
platform_i2c_send_address( 0, u8x8_GetI2CAddress(u8x8), PLATFORM_I2C_DIRECTION_TRANSMITTER ); platform_i2c_send_address( hal->id, u8x8_GetI2CAddress(u8x8), PLATFORM_I2C_DIRECTION_TRANSMITTER );
} else { } else {
// invalid id // invalid id
...@@ -174,8 +210,8 @@ uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * ...@@ -174,8 +210,8 @@ uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *
break; break;
case U8X8_MSG_BYTE_END_TRANSFER: case U8X8_MSG_BYTE_END_TRANSFER:
if (hal->id == 0) { if (hal->id < NUM_I2C) {
platform_i2c_send_stop( 0 ); platform_i2c_send_stop( hal->id );
} else { } else {
// invalid id // invalid id
...@@ -191,27 +227,6 @@ uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * ...@@ -191,27 +227,6 @@ uint8_t u8x8_byte_nodemcu_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *
} }
// static variables containing info about the spi link
// TODO: move to user space in u8x8_t once available
typedef struct {
uint8_t host;
//spi_device_handle_t device;
uint8_t last_dc;
struct {
uint8_t *data;
size_t size, used;
} buffer;
} hal_spi_t;
static void flush_buffer_spi( hal_spi_t *hal )
{
if (hal->buffer.used > 0) {
platform_spi_blkwrite( hal->host, hal->buffer.used, hal->buffer.data );
hal->buffer.used = 0;
}
}
uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{ {
hal_spi_t *hal = ((u8g2_nodemcu_t *)u8x8)->hal; hal_spi_t *hal = ((u8g2_nodemcu_t *)u8x8)->hal;
...@@ -228,6 +243,7 @@ uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * ...@@ -228,6 +243,7 @@ uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *
return 0; return 0;
hal->host = host; hal->host = host;
((u8g2_nodemcu_t *)u8x8)->hal = hal; ((u8g2_nodemcu_t *)u8x8)->hal = hal;
hal->buffer.data = NULL;
hal->last_dc = 0; hal->last_dc = 0;
} }
...@@ -279,6 +295,7 @@ uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * ...@@ -279,6 +295,7 @@ uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *
u8x8_gpio_SetCS( u8x8, u8x8->display_info->chip_disable_level ); u8x8_gpio_SetCS( u8x8, u8x8->display_info->chip_disable_level );
c_free( hal->buffer.data ); c_free( hal->buffer.data );
hal->buffer.data = NULL;
break; break;
default: default:
......
// Do not use the code from ucg submodule and skip the complete source here
// if the ucg module is not selected.
// Reason: The whole ucg submodule code tree might not even exist in this case.
#include "user_modules.h"
#ifdef LUA_USE_MODULES_UCG
#include <string.h>
#include "c_stdlib.h"
#include "platform.h"
#define USE_PIN_LIST
#include "ucg_nodemcu_hal.h"
#define delayMicroseconds os_delay_us
static spi_data_type cache;
static uint8_t cached;
#define CACHED_TRANSFER(dat, num) cache = cached = 0; \
while( arg > 0 ) { \
if (cached == 4) { \
platform_spi_transaction( 1, 0, 0, 32, cache, 0, 0, 0 ); \
cache = cached = 0; \
} \
cache = (cache << num*8) | dat; \
cached += num; \
arg--; \
} \
if (cached > 0) { \
platform_spi_transaction( 1, 0, 0, cached * 8, cache, 0, 0, 0 ); \
}
int16_t ucg_com_nodemcu_hw_spi(ucg_t *ucg, int16_t msg, uint16_t arg, uint8_t *data)
{
switch(msg)
{
case UCG_COM_MSG_POWER_UP:
/* "data" is a pointer to ucg_com_info_t structure with the following information: */
/* ((ucg_com_info_t *)data)->serial_clk_speed value in nanoseconds */
/* ((ucg_com_info_t *)data)->parallel_clk_speed value in nanoseconds */
/* setup pins */
// we assume that the SPI interface was already initialized
// just care for the /CS and D/C pins
//platform_gpio_write( ucg->pin_list[0], value );
if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
platform_gpio_mode( ucg->pin_list[UCG_PIN_RST], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
platform_gpio_mode( ucg->pin_list[UCG_PIN_CD], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
platform_gpio_mode( ucg->pin_list[UCG_PIN_CS], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
break;
case UCG_COM_MSG_POWER_DOWN:
break;
case UCG_COM_MSG_DELAY:
delayMicroseconds(arg);
break;
case UCG_COM_MSG_CHANGE_RESET_LINE:
if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
platform_gpio_write( ucg->pin_list[UCG_PIN_RST], arg );
break;
case UCG_COM_MSG_CHANGE_CS_LINE:
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
platform_gpio_write( ucg->pin_list[UCG_PIN_CS], arg );
break;
case UCG_COM_MSG_CHANGE_CD_LINE:
platform_gpio_write( ucg->pin_list[UCG_PIN_CD], arg );
break;
case UCG_COM_MSG_SEND_BYTE:
platform_spi_send( 1, 8, arg );
break;
case UCG_COM_MSG_REPEAT_1_BYTE:
CACHED_TRANSFER(data[0], 1);
break;
case UCG_COM_MSG_REPEAT_2_BYTES:
CACHED_TRANSFER((data[0] << 8) | data[1], 2);
break;
case UCG_COM_MSG_REPEAT_3_BYTES:
while( arg > 0 ) {
platform_spi_transaction( 1, 0, 0, 24, (data[0] << 16) | (data[1] << 8) | data[2], 0, 0, 0 );
arg--;
}
break;
case UCG_COM_MSG_SEND_STR:
CACHED_TRANSFER(*data++, 1);
break;
case UCG_COM_MSG_SEND_CD_DATA_SEQUENCE:
while(arg > 0)
{
if ( *data != 0 )
{
/* set the data line directly, ignore the setting from UCG_CFG_CD */
if ( *data == 1 )
{
platform_gpio_write( ucg->pin_list[UCG_PIN_CD], 0 );
}
else
{
platform_gpio_write( ucg->pin_list[UCG_PIN_CD], 1 );
}
}
data++;
platform_spi_send( 1, 8, *data );
data++;
arg--;
}
break;
}
return 1;
}
#endif /* LUA_USE_MODULES_UCG */
#ifndef _UCG_NODEMCU_HAL_H
#define _UCG_NODEMCU_HAL_H
#include "ucg.h"
// extend standard ucg_t struct with info that's needed in the communication callbacks
typedef struct {
ucg_t ucg;
void *hal;
} ucg_nodemcu_t;
int16_t ucg_com_nodemcu_hw_spi(ucg_t *ucg, int16_t msg, uint16_t arg, uint8_t *data);
#endif /* _UCG_NODEMCU_HAL_H */
Subproject commit 7f2fc42af3d01fdfe2cc19320bdcfe693dd2b20d Subproject commit d4da8254220adf39db44faa52a0842967095d230
...@@ -24,7 +24,7 @@ STD_CFLAGS=-std=gnu11 -Wimplicit ...@@ -24,7 +24,7 @@ STD_CFLAGS=-std=gnu11 -Wimplicit
# makefile at its root level - these are then overridden # makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein # for a subtree within the makefile rooted therein
# #
#DEFINES += DEFINES += -DUSE_PIN_LIST
############################################################# #############################################################
# Recursion Magic - Don't touch this!! # Recursion Magic - Don't touch this!!
...@@ -38,7 +38,10 @@ STD_CFLAGS=-std=gnu11 -Wimplicit ...@@ -38,7 +38,10 @@ STD_CFLAGS=-std=gnu11 -Wimplicit
# Required for each makefile to inherit from the parent # Required for each makefile to inherit from the parent
# #
CSRCS := $(wildcard ucg/src/clib/*.c *.c)
INCLUDES := $(INCLUDES) -I $(PDIR)include INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ucg/src/clib
INCLUDES += -I ./ INCLUDES += -I ./
INCLUDES += -I ../libc INCLUDES += -I ../libc
PDIR := ../$(PDIR) PDIR := ../$(PDIR)
......
Subproject commit e21641a6c1ddb0e71f7b9e01501fa739786c68b1
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment