Commit 82e126eb authored by devsaurus's avatar devsaurus
Browse files

Merge branch 'master' into dev

parents 1740841e 9f0b1678
......@@ -22,7 +22,7 @@ ifeq ($(OS),Windows_NT)
else
# It is gcc, may be cygwin
# Can we use -fdata-sections?
CCFLAGS += -Os -ffunction-sections -fno-jump-tables
CCFLAGS += -Os -ffunction-sections -fno-jump-tables -fdata-sections
AR = xtensa-lx106-elf-ar
CC = xtensa-lx106-elf-gcc
NM = xtensa-lx106-elf-nm
......@@ -49,7 +49,7 @@ else
else
ESPPORT = $(COMPORT)
endif
CCFLAGS += -Os -ffunction-sections -fno-jump-tables
CCFLAGS += -Os -ffunction-sections -fno-jump-tables -fdata-sections
AR = xtensa-lx106-elf-ar
CC = xtensa-lx106-elf-gcc
NM = xtensa-lx106-elf-nm
......
......@@ -31,6 +31,7 @@ SUBDIRS= \
libc \
lua \
mqtt \
u8glib \
smart \
wofs \
modules \
......@@ -77,6 +78,7 @@ COMPONENTS_eagle.app.v6 = \
libc/liblibc.a \
lua/liblua.a \
mqtt/mqtt.a \
u8glib/u8glib.a \
smart/smart.a \
wofs/wofs.a \
spiffs/spiffs.a \
......@@ -84,6 +86,8 @@ COMPONENTS_eagle.app.v6 = \
LINKFLAGS_eagle.app.v6 = \
-L../lib \
-Wl,--gc-sections \
-Xlinker -Map=mapfile \
-nostdlib \
-T$(LD_FILE) \
-Wl,--no-check-sections \
......
......@@ -61,9 +61,10 @@
#define LUA_USE_MODULES_TMR
#define LUA_USE_MODULES_ADC
#define LUA_USE_MODULES_UART
#define LUA_USE_MODULES_OW
#undef LUA_USE_MODULES_OW
#define LUA_USE_MODULES_BIT
#define LUA_USE_MODULES_MQTT
#undef LUA_USE_MODULES_MQTT
#define LUA_USE_MODULES_U8G
#endif /* LUA_USE_MODULES */
// #define LUA_NUMBER_INTEGRAL
......@@ -87,4 +88,13 @@
#define LED_HIGH_COUNT_DEFAULT 10
#define LED_LOW_COUNT_DEFAULT 0
// Configure U8glib fonts
// add a U8G_FONT_TABLE_ENTRY for each font you want to compile into the image
#define U8G_FONT_TABLE_ENTRY(font)
#define U8G_FONT_TABLE \
U8G_FONT_TABLE_ENTRY(font_6x10) \
U8G_FONT_TABLE_ENTRY(font_chikita)
#undef U8G_FONT_TABLE_ENTRY
#endif /* __USER_CONFIG_H__ */
......@@ -40,6 +40,7 @@ INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
INCLUDES += -I ../libc
INCLUDES += -I ../mqtt
INCLUDES += -I ../u8glib
INCLUDES += -I ../lua
INCLUDES += -I ../platform
INCLUDES += -I ../wofs
......
......@@ -64,6 +64,9 @@ LUALIB_API int ( luaopen_wifi )( lua_State *L );
#define AUXLIB_MQTT "mqtt"
LUALIB_API int ( luaopen_mqtt )( lua_State *L );
#define AUXLIB_U8G "u8g"
LUALIB_API int ( luaopen_u8g )( lua_State *L );
#define AUXLIB_NODE "node"
LUALIB_API int ( luaopen_node )( lua_State *L );
......
......@@ -45,6 +45,14 @@
#define ROM_MODULES_MQTT
#endif
#if defined(LUA_USE_MODULES_U8G)
#define MODULES_U8G "u8g"
#define ROM_MODULES_U8G \
_ROM(MODULES_U8G, luaopen_u8g, lu8g_map)
#else
#define ROM_MODULES_U8G
#endif
#if defined(LUA_USE_MODULES_I2C)
#define MODULES_I2C "i2c"
#define ROM_MODULES_I2C \
......@@ -121,7 +129,8 @@
ROM_MODULES_GPIO \
ROM_MODULES_PWM \
ROM_MODULES_WIFI \
ROM_MODULES_MQTT \
ROM_MODULES_MQTT \
ROM_MODULES_U8G \
ROM_MODULES_I2C \
ROM_MODULES_SPI \
ROM_MODULES_TMR \
......
// Module for U8glib
//#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "platform.h"
#include "auxmods.h"
#include "lrotable.h"
//#include "c_string.h"
//#include "c_stdlib.h"
#include "u8g.h"
typedef struct lu8g_userdata
{
uint8_t i2c_addr;
u8g_t u8g;
} lu8g_userdata_t;
// Font look-up array
static const u8g_fntpgm_uint8_t *font_array[] =
{
#undef U8G_FONT_TABLE_ENTRY
#define U8G_FONT_TABLE_ENTRY(font) u8g_ ## font ,
U8G_FONT_TABLE
NULL
};
// function to read 4-byte aligned from program memory AKA irom0
u8g_pgm_uint8_t u8g_pgm_read(const u8g_pgm_uint8_t *adr)
{
uint32_t iadr = (uint32_t)adr;
// set up pointer to 4-byte aligned memory location
uint32_t *ptr = (uint32_t *)(iadr & ~0x3);
// read 4-byte aligned
uint32_t pgm_data = *ptr;
// return the correct byte within the retrieved 32bit word
return pgm_data >> ((iadr % 4) * 8);
}
// helper function: retrieve and check userdata argument
static lu8g_userdata_t *get_lud( lua_State *L )
{
lu8g_userdata_t *lud = (lu8g_userdata_t *)luaL_checkudata(L, 1, "u8g.display");
luaL_argcheck(L, lud, 1, "u8g.display expected");
return lud;
}
// helper function: retrieve given number of integer arguments
static void lu8g_get_int_args( lua_State *L, uint8_t stack, uint8_t num, u8g_uint_t *args)
{
while (num-- > 0)
{
*args++ = luaL_checkinteger( L, stack++ );
}
}
// Lua: u8g.setFont( self, font )
static int lu8g_setFont( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
lua_Integer fontnr = luaL_checkinteger( L, 2 );
if ((fontnr >= 0) && (fontnr < (sizeof( font_array ) / sizeof( u8g_fntpgm_uint8_t ))))
u8g_SetFont( &(lud->u8g), font_array[fontnr] );
return 0;
}
// Lua: u8g.setFontRefHeightExtendedText( self )
static int lu8g_setFontRefHeightExtendedText( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_SetFontRefHeightExtendedText( &(lud->u8g) );
return 0;
}
// Lua: u8g.setDefaultForegroundColor( self )
static int lu8g_setDefaultForegroundColor( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_SetDefaultForegroundColor( &(lud->u8g) );
return 0;
}
// Lua: u8g.setFontPosTop( self )
static int lu8g_setFontPosTop( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_SetFontPosTop( &(lud->u8g) );
return 0;
}
static int lu8g_generic_drawStr( lua_State *L, uint8_t rot )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[2];
lu8g_get_int_args( L, 2, 2, args );
const char *s = luaL_checkstring( L, (1+2) + 1 );
if (s == NULL)
return 0;
switch (rot)
{
case 1:
lua_pushinteger( L, u8g_DrawStr90( &(lud->u8g), args[0], args[1], s ) );
break;
case 2:
lua_pushinteger( L, u8g_DrawStr180( &(lud->u8g), args[0], args[1], s ) );
break;
case 3:
lua_pushinteger( L, u8g_DrawStr270( &(lud->u8g), args[0], args[1], s ) );
break;
default:
lua_pushinteger( L, u8g_DrawStr( &(lud->u8g), args[0], args[1], s ) );
break;
}
return 1;
}
// Lua: pix_len = u8g.drawStr( self, x, y, string )
static int lu8g_drawStr( lua_State *L )
{
lu8g_userdata_t *lud;
return lu8g_generic_drawStr( L, 0 );
}
// Lua: pix_len = u8g.drawStr90( self, x, y, string )
static int lu8g_drawStr90( lua_State *L )
{
lu8g_userdata_t *lud;
return lu8g_generic_drawStr( L, 1 );
}
// Lua: pix_len = u8g.drawStr180( self, x, y, string )
static int lu8g_drawStr180( lua_State *L )
{
lu8g_userdata_t *lud;
return lu8g_generic_drawStr( L, 2 );
}
// Lua: pix_len = u8g.drawStr270( self, x, y, string )
static int lu8g_drawStr270( lua_State *L )
{
lu8g_userdata_t *lud;
return lu8g_generic_drawStr( L, 3 );
}
// Lua: u8g.drawLine( self, x1, y1, x2, y2 )
static int lu8g_drawLine( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[4];
lu8g_get_int_args( L, 2, 4, args );
u8g_DrawLine( &(lud->u8g), args[0], args[1], args[2], args[3] );
return 0;
}
// Lua: u8g.drawTriangle( self, x0, y0, x1, y1, x2, y2 )
static int lu8g_drawTriangle( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[6];
lu8g_get_int_args( L, 2, 6, args );
u8g_DrawTriangle( &(lud->u8g), args[0], args[1], args[2], args[3], args[4], args[5] );
return 0;
}
// Lua: u8g.drawBox( self, x, y, width, height )
static int lu8g_drawBox( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[4];
lu8g_get_int_args( L, 2, 4, args );
u8g_DrawBox( &(lud->u8g), args[0], args[1], args[2], args[3] );
return 0;
}
// Lua: u8g.drawRBox( self, x, y, width, height, radius )
static int lu8g_drawRBox( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[5];
lu8g_get_int_args( L, 2, 5, args );
u8g_DrawRBox( &(lud->u8g), args[0], args[1], args[2], args[3], args[4] );
return 0;
}
// Lua: u8g.drawFrame( self, x, y, width, height )
static int lu8g_drawFrame( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[4];
lu8g_get_int_args( L, 2, 4, args );
u8g_DrawFrame( &(lud->u8g), args[0], args[1], args[2], args[3] );
return 0;
}
// Lua: u8g.drawRFrame( self, x, y, width, height, radius )
static int lu8g_drawRFrame( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[5];
lu8g_get_int_args( L, 2, 5, args );
u8g_DrawRFrame( &(lud->u8g), args[0], args[1], args[2], args[3], args[4] );
return 0;
}
// Lua: u8g.drawDisc( self, x0, y0, rad, opt = U8G_DRAW_ALL )
static int lu8g_drawDisc( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[3];
lu8g_get_int_args( L, 2, 3, args );
u8g_uint_t opt = luaL_optinteger( L, (1+3) + 1, U8G_DRAW_ALL );
u8g_DrawDisc( &(lud->u8g), args[0], args[1], args[2], opt );
return 0;
}
// Lua: u8g.drawCircle( self, x0, y0, rad, opt = U8G_DRAW_ALL )
static int lu8g_drawCircle( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_uint_t args[3];
lu8g_get_int_args( L, 2, 3, args );
u8g_uint_t opt = luaL_optinteger( L, (1+3) + 1, U8G_DRAW_ALL );
u8g_DrawCircle( &(lud->u8g), args[0], args[1], args[2], opt );
return 0;
}
// Lua: u8g.firstPage( self )
static int lu8g_firstPage( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
u8g_FirstPage( &(lud->u8g) );
return 0;
}
// Lua: bool = u8g.nextPage( self )
static int lu8g_nextPage( lua_State *L )
{
lu8g_userdata_t *lud;
if ((lud = get_lud( L )) == NULL)
return 0;
lua_pushboolean( L, u8g_NextPage( &(lud->u8g) ) );
return 1;
}
// ------------------------------------------------------------
// comm functions
//
#define I2C_SLA 0x3c
#define I2C_CMD_MODE 0x000
#define I2C_DATA_MODE 0x040
#define ESP_I2C_ID 0
static uint8_t do_i2c_start(uint8_t id, uint8_t sla)
{
platform_i2c_send_start( id );
// ignore return value -> tolerate missing ACK
platform_i2c_send_address( id, sla, PLATFORM_I2C_DIRECTION_TRANSMITTER );
return 1;
}
static uint8_t u8g_com_esp8266_ssd_start_sequence(u8g_t *u8g)
{
/* are we requested to set the a0 state? */
if ( u8g->pin_list[U8G_PI_SET_A0] == 0 )
return 1;
/* setup bus, might be a repeated start */
if ( do_i2c_start( ESP_I2C_ID, I2C_SLA ) == 0 )
return 0;
if ( u8g->pin_list[U8G_PI_A0_STATE] == 0 )
{
// ignore return value -> tolerate missing ACK
if ( platform_i2c_send_byte( ESP_I2C_ID, I2C_CMD_MODE ) == 0 )
; //return 0;
}
else
{
platform_i2c_send_byte( ESP_I2C_ID, I2C_DATA_MODE );
}
u8g->pin_list[U8G_PI_SET_A0] = 0;
return 1;
}
uint8_t u8g_com_esp8266_ssd_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
{
switch(msg)
{
case U8G_COM_MSG_INIT:
// we assume that the i2c bus was already initialized
//u8g_i2c_init(u8g->pin_list[U8G_PI_I2C_OPTION]);
break;
case U8G_COM_MSG_STOP:
break;
case U8G_COM_MSG_RESET:
/* Currently disabled, but it could be enable. Previous restrictions have been removed */
/* u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val); */
break;
case U8G_COM_MSG_CHIP_SELECT:
u8g->pin_list[U8G_PI_A0_STATE] = 0;
u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again, also forces start condition */
if ( arg_val == 0 )
{
/* disable chip, send stop condition */
platform_i2c_send_stop( ESP_I2C_ID );
}
else
{
/* enable, do nothing: any byte writing will trigger the i2c start */
}
break;
case U8G_COM_MSG_WRITE_BYTE:
//u8g->pin_list[U8G_PI_SET_A0] = 1;
if ( u8g_com_esp8266_ssd_start_sequence(u8g) == 0 )
return platform_i2c_stop( ESP_I2C_ID ), 0;
// ignore return value -> tolerate missing ACK
if ( platform_i2c_send_byte( ESP_I2C_ID, arg_val) == 0 )
; //return platform_i2c_send_stop( ESP_I2C_ID ), 0;
// platform_i2c_send_stop( ESP_I2C_ID );
break;
case U8G_COM_MSG_WRITE_SEQ:
//u8g->pin_list[U8G_PI_SET_A0] = 1;
if ( u8g_com_esp8266_ssd_start_sequence(u8g) == 0 )
return platform_i2c_send_stop( ESP_I2C_ID ), 0;
{
register uint8_t *ptr = arg_ptr;
while( arg_val > 0 )
{
// ignore return value -> tolerate missing ACK
if ( platform_i2c_send_byte( ESP_I2C_ID, *ptr++) == 0 )
; //return platform_i2c_send_stop( ESP_I2C_ID ), 0;
arg_val--;
}
}
// platform_i2c_send_stop( ESP_I2C_ID );
break;
case U8G_COM_MSG_WRITE_SEQ_P:
//u8g->pin_list[U8G_PI_SET_A0] = 1;
if ( u8g_com_esp8266_ssd_start_sequence(u8g) == 0 )
return platform_i2c_send_stop( ESP_I2C_ID ), 0;
{
register uint8_t *ptr = arg_ptr;
while( arg_val > 0 )
{
// ignore return value -> tolerate missing ACK
if ( platform_i2c_send_byte( ESP_I2C_ID, u8g_pgm_read(ptr) ) == 0 )
; //return 0;
ptr++;
arg_val--;
}
}
// platform_i2c_send_stop( ESP_I2C_ID );
break;
case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again */
break;
}
return 1;
}
// device constructors
// Lua: speed = u8g.ssd1306_128x64_i2c( i2c_addr )
static int lu8g_ssd1306_128x64_i2c( lua_State *L )
{
unsigned addr = luaL_checkinteger( L, 1 );
if (addr == 0)
return luaL_error( L, "i2c address required" );
lu8g_userdata_t *userdata = (lu8g_userdata_t *) lua_newuserdata( L, sizeof( lu8g_userdata_t ) );
userdata->i2c_addr = (uint8_t)addr;
u8g_InitI2C( &(userdata->u8g), &u8g_dev_ssd1306_128x64_i2c, U8G_I2C_OPT_NONE);
// set its metatable
luaL_getmetatable(L, "u8g.display");
lua_setmetatable(L, -2);
return 1;
}
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
static const LUA_REG_TYPE lu8g_display_map[] =
{
{ LSTRKEY( "setFont" ), LFUNCVAL( lu8g_setFont ) },
{ LSTRKEY( "setFontRefHeightExtendedText" ), LFUNCVAL( lu8g_setFontRefHeightExtendedText ) },
{ LSTRKEY( "setDefaultForegroundColor" ), LFUNCVAL( lu8g_setDefaultForegroundColor ) },
{ LSTRKEY( "setFontPosTop" ), LFUNCVAL( lu8g_setFontPosTop ) },
{ LSTRKEY( "drawStr" ), LFUNCVAL( lu8g_drawStr ) },
{ LSTRKEY( "drawStr90" ), LFUNCVAL( lu8g_drawStr90 ) },
{ LSTRKEY( "drawStr180" ), LFUNCVAL( lu8g_drawStr180 ) },
{ LSTRKEY( "drawStr270" ), LFUNCVAL( lu8g_drawStr270 ) },
{ LSTRKEY( "drawBox" ), LFUNCVAL( lu8g_drawBox ) },
{ LSTRKEY( "drawLine" ), LFUNCVAL( lu8g_drawLine ) },
{ LSTRKEY( "drawTriangle" ), LFUNCVAL( lu8g_drawTriangle ) },
{ LSTRKEY( "drawRBox" ), LFUNCVAL( lu8g_drawRBox ) },
{ LSTRKEY( "drawFrame" ), LFUNCVAL( lu8g_drawFrame ) },
{ LSTRKEY( "drawRFrame" ), LFUNCVAL( lu8g_drawRFrame ) },
{ LSTRKEY( "drawDisc" ), LFUNCVAL( lu8g_drawDisc ) },
{ LSTRKEY( "drawCircle" ), LFUNCVAL( lu8g_drawCircle ) },
{ LSTRKEY( "firstPage" ), LFUNCVAL( lu8g_firstPage ) },
{ LSTRKEY( "nextPage" ), LFUNCVAL( lu8g_nextPage ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "__index" ), LROVAL ( lu8g_display_map ) },
#endif
{ LNILKEY, LNILVAL }
};
const LUA_REG_TYPE lu8g_map[] =
{
{ LSTRKEY( "ssd1306_128x64_i2c" ), LFUNCVAL ( lu8g_ssd1306_128x64_i2c ) },
#if LUA_OPTIMIZE_MEMORY > 0
//{ LSTRKEY( "font_6x10" ), LNUMVAL( LU8G_FONT_6X10 ) },
// Register fonts
#undef U8G_FONT_TABLE_ENTRY
#define U8G_FONT_TABLE_ENTRY(font) { LSTRKEY( #font ), LNUMVAL( __COUNTER__ ) },
U8G_FONT_TABLE
{ LSTRKEY( "__metatable" ), LROVAL( lu8g_map ) },
#endif
{ LNILKEY, LNILVAL }
};
LUALIB_API int ICACHE_FLASH_ATTR luaopen_u8g( lua_State *L )
{
#if LUA_OPTIMIZE_MEMORY > 0
luaL_rometatable(L, "u8g.display", (void *)lu8g_display_map); // create metatable
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
int n;
luaL_register( L, AUXLIB_U8G, lu8g_map );
// Set it as its own metatable
lua_pushvalue( L, -1 );
lua_setmetatable( L, -2 );
// Module constants
// Register fonts
#undef U8G_FONT_TABLE_ENTRY
#define U8G_FONT_TABLE_ENTRY(font) MOD_REG_NUMBER( L, #font, __COUNTER__ );
U8G_FONT_TABLE
// create metatable
luaL_newmetatable(L, "u8g.display");
// metatable.__index = metatable
lua_pushliteral(L, "__index");
lua_pushvalue(L,-2);
lua_rawset(L,-3);
// Setup the methods inside metatable
luaL_register( L, NULL, u8g_display_map );
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}
#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
ifndef PDIR
GEN_LIBS = u8glib.a
endif
#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
#DEFINES +=
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
INCLUDES += -I ../libc
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile
This diff is collapsed.
This diff is collapsed.
/*
u8g_bitmap.c
Universal 8bit Graphics Library
Copyright (c) 2011, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "u8g.h"
void u8g_DrawHBitmap(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, const uint8_t *bitmap)
{
while( cnt > 0 )
{
u8g_Draw8Pixel(u8g, x, y, 0, *bitmap);
bitmap++;
cnt--;
x+=8;
}
}
void u8g_DrawBitmap(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, u8g_uint_t h, const uint8_t *bitmap)
{
if ( u8g_IsBBXIntersection(u8g, x, y, cnt*8, h) == 0 )
return;
while( h > 0 )
{
u8g_DrawHBitmap(u8g, x, y, cnt, bitmap);
bitmap += cnt;
y++;
h--;
}
}
void u8g_DrawHBitmapP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, const u8g_pgm_uint8_t *bitmap)
{
while( cnt > 0 )
{
u8g_Draw8Pixel(u8g, x, y, 0, u8g_pgm_read(bitmap));
bitmap++;
cnt--;
x+=8;
}
}
void u8g_DrawBitmapP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, u8g_uint_t h, const u8g_pgm_uint8_t *bitmap)
{
if ( u8g_IsBBXIntersection(u8g, x, y, cnt*8, h) == 0 )
return;
while( h > 0 )
{
u8g_DrawHBitmapP(u8g, x, y, cnt, bitmap);
bitmap += cnt;
y++;
h--;
}
}
/*=========================================================================*/
static void u8g_DrawHXBM(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, const uint8_t *bitmap)
{
uint8_t d;
x+=7;
while( w >= 8 )
{
u8g_Draw8Pixel(u8g, x, y, 2, *bitmap);
bitmap++;
w-= 8;
x+=8;
}
if ( w > 0 )
{
d = *bitmap;
x -= 7;
do
{
if ( d & 1 )
u8g_DrawPixel(u8g, x, y);
x++;
w--;
d >>= 1;
} while ( w > 0 );
}
}
void u8g_DrawXBM(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, const uint8_t *bitmap)
{
u8g_uint_t b;
b = w;
b += 7;
b >>= 3;
if ( u8g_IsBBXIntersection(u8g, x, y, w, h) == 0 )
return;
while( h > 0 )
{
u8g_DrawHXBM(u8g, x, y, w, bitmap);
bitmap += b;
y++;
h--;
}
}
static void u8g_DrawHXBMP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, const u8g_pgm_uint8_t *bitmap)
{
uint8_t d;
x+=7;
while( w >= 8 )
{
u8g_Draw8Pixel(u8g, x, y, 2, u8g_pgm_read(bitmap));
bitmap++;
w-= 8;
x+=8;
}
if ( w > 0 )
{
d = u8g_pgm_read(bitmap);
x -= 7;
do
{
if ( d & 1 )
u8g_DrawPixel(u8g, x, y);
x++;
w--;
d >>= 1;
} while ( w > 0 );
}
}
void u8g_DrawXBMP(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, const u8g_pgm_uint8_t *bitmap)
{
u8g_uint_t b;
b = w;
b += 7;
b >>= 3;
if ( u8g_IsBBXIntersection(u8g, x, y, w, h) == 0 )
return;
while( h > 0 )
{
u8g_DrawHXBMP(u8g, x, y, w, bitmap);
bitmap += b;
y++;
h--;
}
}
/*
u8g_circle.c
Utility to draw empty and filled circles.
Universal 8bit Graphics Library
Copyright (c) 2011, bjthom@gmail.com
u8g_DrawCircle & u8g_DrawDisc by olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Addition to the U8G Library 02/25/12
*/
#include "u8g.h"
#ifdef OLD_CODE
void circ_upperRight(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
u8g_DrawPixel(u8g, x0 + x, y0 - y);
u8g_DrawPixel(u8g, x0 + y, y0 - x);
}
void circ_upperLeft(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
u8g_DrawPixel(u8g, x0 - x, y0 - y);
u8g_DrawPixel(u8g, x0 - y, y0 - x);
}
void circ_lowerRight(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
u8g_DrawPixel(u8g, x0 + x, y0 + y);
u8g_DrawPixel(u8g, x0 + y, y0 + x);
}
void circ_lowerLeft(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
u8g_DrawPixel(u8g, x0 - x, y0 + y);
u8g_DrawPixel(u8g, x0 - y, y0 + x);
}
void circ_all(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0) {
circ_upperRight(u8g, x, y, x0, y0);
circ_upperLeft(u8g, x, y, x0, y0);
circ_lowerRight(u8g, x, y, x0, y0);
circ_lowerLeft(u8g, x, y, x0, y0);
}
void u8g_DrawEmpCirc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
if ( u8g_IsBBXIntersection(u8g, x0-rad-1, y0-rad-1, 2*rad+1, 2*rad+1) == 0)
return;
int f = 1 - rad;
int ddF_x = 1;
int ddF_y = -2*rad;
uint8_t x = 0;
uint8_t y = rad;
void ( *circ_util )(u8g_t *, u8g_uint_t, u8g_uint_t, u8g_uint_t, u8g_uint_t);
switch (option)
{
case U8G_CIRC_UPPER_RIGHT:
u8g_DrawPixel(u8g, x0, y0 - rad);
u8g_DrawPixel(u8g, x0 + rad, y0);
circ_util = circ_upperRight;
break;
case U8G_CIRC_UPPER_LEFT:
u8g_DrawPixel(u8g, x0, y0 - rad);
u8g_DrawPixel(u8g, x0 - rad, y0);
circ_util = circ_upperLeft;
break;
case U8G_CIRC_LOWER_RIGHT:
u8g_DrawPixel(u8g, x0, y0 + rad);
u8g_DrawPixel(u8g, x0 + rad, y0);
circ_util = circ_lowerRight;
break;
case U8G_CIRC_LOWER_LEFT:
u8g_DrawPixel(u8g, x0, y0 + rad);
u8g_DrawPixel(u8g, x0 - rad, y0);
circ_util = circ_lowerLeft;
break;
default:
case U8G_CIRC_ALL:
u8g_DrawPixel(u8g, x0, y0 + rad);
u8g_DrawPixel(u8g, x0, y0 - rad);
u8g_DrawPixel(u8g, x0 + rad, y0);
u8g_DrawPixel(u8g, x0 - rad, y0);
circ_util = circ_all;
break;
}
while( x < y )
{
if(f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
circ_util(u8g, x, y, x0, y0);
}
}
void u8g_DrawFillCirc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
if ( u8g_IsBBXIntersection(u8g, x0-rad-1, y0-rad-1, 2*rad+1, 2*rad+1) == 0)
return;
int f = 1 - rad;
int ddF_x = 1;
int ddF_y = -2*rad;
uint8_t x = 0;
uint8_t y = rad;
// Draw vertical diameter at the horiz. center
// u8g_DrawVLine(u8g, x0, y0 - rad, 2*rad+1);
if (option == U8G_CIRC_UPPER_LEFT || option == U8G_CIRC_UPPER_RIGHT) {
u8g_DrawVLine(u8g, x0, y0 - rad, rad+1);
}
else if (option == U8G_CIRC_LOWER_LEFT || option == U8G_CIRC_LOWER_RIGHT) {
u8g_DrawVLine(u8g, x0, y0, rad+1);
}
else {
u8g_DrawVLine(u8g, x0, y0 - rad, 2*rad+1);
}
while( x < y )
{
if(f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
//Draw vertical lines from one point to another
switch (option)
{
case U8G_CIRC_UPPER_RIGHT:
u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
u8g_DrawVLine(u8g, x0+y, y0-x, x+1);
break;
case U8G_CIRC_UPPER_LEFT:
u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
u8g_DrawVLine(u8g, x0-y, y0-x, x+1);
break;
case U8G_CIRC_LOWER_RIGHT:
u8g_DrawVLine(u8g, x0+x, y0, y+1);
u8g_DrawVLine(u8g, x0+y, y0, x+1);
break;
case U8G_CIRC_LOWER_LEFT:
u8g_DrawVLine(u8g, x0-x, y0, y+1);
u8g_DrawVLine(u8g, x0-y, y0, x+1);
break;
case U8G_CIRC_ALL:
u8g_DrawVLine(u8g, x0+x, y0-y, 2*y+1);
u8g_DrawVLine(u8g, x0-x, y0-y, 2*y+1);
u8g_DrawVLine(u8g, x0+y, y0-x, 2*x+1);
u8g_DrawVLine(u8g, x0-y, y0-x, 2*x+1);
break;
}
}
}
#endif
/*=========================================================================*/
static void u8g_draw_circle_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option) U8G_NOINLINE;
static void u8g_draw_circle_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
{
/* upper right */
if ( option & U8G_DRAW_UPPER_RIGHT )
{
u8g_DrawPixel(u8g, x0 + x, y0 - y);
u8g_DrawPixel(u8g, x0 + y, y0 - x);
}
/* upper left */
if ( option & U8G_DRAW_UPPER_LEFT )
{
u8g_DrawPixel(u8g, x0 - x, y0 - y);
u8g_DrawPixel(u8g, x0 - y, y0 - x);
}
/* lower right */
if ( option & U8G_DRAW_LOWER_RIGHT )
{
u8g_DrawPixel(u8g, x0 + x, y0 + y);
u8g_DrawPixel(u8g, x0 + y, y0 + x);
}
/* lower left */
if ( option & U8G_DRAW_LOWER_LEFT )
{
u8g_DrawPixel(u8g, x0 - x, y0 + y);
u8g_DrawPixel(u8g, x0 - y, y0 + x);
}
}
void u8g_draw_circle(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
u8g_int_t f;
u8g_int_t ddF_x;
u8g_int_t ddF_y;
u8g_uint_t x;
u8g_uint_t y;
f = 1;
f -= rad;
ddF_x = 1;
ddF_y = 0;
ddF_y -= rad;
ddF_y *= 2;
x = 0;
y = rad;
u8g_draw_circle_section(u8g, x, y, x0, y0, option);
while ( x < y )
{
if (f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
u8g_draw_circle_section(u8g, x, y, x0, y0, option);
}
}
void u8g_DrawCircle(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
/* check for bounding box */
{
u8g_uint_t radp, radp2;
radp = rad;
radp++;
radp2 = radp;
radp2 *= 2;
if ( u8g_IsBBXIntersection(u8g, x0-radp, y0-radp, radp2, radp2) == 0)
return;
}
/* draw circle */
u8g_draw_circle(u8g, x0, y0, rad, option);
}
static void u8g_draw_disc_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option) U8G_NOINLINE;
static void u8g_draw_disc_section(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t x0, u8g_uint_t y0, uint8_t option)
{
/* upper right */
if ( option & U8G_DRAW_UPPER_RIGHT )
{
u8g_DrawVLine(u8g, x0+x, y0-y, y+1);
u8g_DrawVLine(u8g, x0+y, y0-x, x+1);
}
/* upper left */
if ( option & U8G_DRAW_UPPER_LEFT )
{
u8g_DrawVLine(u8g, x0-x, y0-y, y+1);
u8g_DrawVLine(u8g, x0-y, y0-x, x+1);
}
/* lower right */
if ( option & U8G_DRAW_LOWER_RIGHT )
{
u8g_DrawVLine(u8g, x0+x, y0, y+1);
u8g_DrawVLine(u8g, x0+y, y0, x+1);
}
/* lower left */
if ( option & U8G_DRAW_LOWER_LEFT )
{
u8g_DrawVLine(u8g, x0-x, y0, y+1);
u8g_DrawVLine(u8g, x0-y, y0, x+1);
}
}
void u8g_draw_disc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
u8g_int_t f;
u8g_int_t ddF_x;
u8g_int_t ddF_y;
u8g_uint_t x;
u8g_uint_t y;
f = 1;
f -= rad;
ddF_x = 1;
ddF_y = 0;
ddF_y -= rad;
ddF_y *= 2;
x = 0;
y = rad;
u8g_draw_disc_section(u8g, x, y, x0, y0, option);
while ( x < y )
{
if (f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
u8g_draw_disc_section(u8g, x, y, x0, y0, option);
}
}
void u8g_DrawDisc(u8g_t *u8g, u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t option)
{
/* check for bounding box */
{
u8g_uint_t radp, radp2;
radp = rad;
radp++;
radp2 = radp;
radp2 *= 2;
if ( u8g_IsBBXIntersection(u8g, x0-radp, y0-radp, radp2, radp2) == 0)
return;
}
/* draw disc */
u8g_draw_disc(u8g, x0, y0, rad, option);
}
/*
u8g_clip.c
procedures for clipping
taken over from procs in u8g_pb.c
Universal 8bit Graphics Library
Copyright (c) 2012, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Notes
This is one of the most critical parts of u8glib. It must be fast, but still reliable.
Based on the intersection program (see tools folder), there is minimized version of
the condition for the intersaction test:
minimized version
---1----0 1 b1 <= a2 && b1 > b2
-----1--0 1 b2 >= a1 && b1 > b2
---1-1--- 1 b1 <= a2 && b2 >= a1
It includes the assumption, that a1 <= a2 is always true (correct, because
a1, a2 are the page dimensions.
The direct implementation of the above result is done in:
uint8_t u8g_is_intersection_boolean(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
However, this is slower than a decision tree version:
static uint8_t u8g_is_intersection_decision_tree(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
Also suprising is, that the macro implementation is slower than the inlined version.
The decision tree is based on the expansion of the truth table.
*/
#include "u8g.h"
#ifdef __GNUC__
#define U8G_ALWAYS_INLINE __inline__ __attribute__((always_inline))
#else
#define U8G_ALWAYS_INLINE
#endif
/*
intersection assumptions:
a1 <= a2 is always true
minimized version
---1----0 1 b1 <= a2 && b1 > b2
-----1--0 1 b2 >= a1 && b1 > b2
---1-1--- 1 b1 <= a2 && b2 >= a1
*/
#ifdef OLD_CODE_WHICH_IS_TOO_SLOW
static uint8_t u8g_is_intersection_boolean(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
{
uint8_t c1, c2, c3, tmp;
c1 = v0 <= a1;
c2 = v1 >= a0;
c3 = v0 > v1;
tmp = c1;
c1 &= c2;
c2 &= c3;
c3 &= tmp;
c1 |= c2;
c1 |= c3;
return c1 & 1;
}
#endif
#define U8G_IS_INTERSECTION_MACRO(a0,a1,v0,v1) ((uint8_t)( (v0) <= (a1) ) ? ( ( (v1) >= (a0) ) ? ( 1 ) : ( (v0) > (v1) ) ) : ( ( (v1) >= (a0) ) ? ( (v0) > (v1) ) : ( 0 ) ))
//static uint8_t u8g_is_intersection_decision_tree(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1) U8G_ALWAYS_INLINE;
static uint8_t U8G_ALWAYS_INLINE u8g_is_intersection_decision_tree(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
{
/* surprisingly the macro leads to larger code */
/* return U8G_IS_INTERSECTION_MACRO(a0,a1,v0,v1); */
if ( v0 <= a1 )
{
if ( v1 >= a0 )
{
return 1;
}
else
{
if ( v0 > v1 )
{
return 1;
}
else
{
return 0;
}
}
}
else
{
if ( v1 >= a0 )
{
if ( v0 > v1 )
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
}
uint8_t u8g_IsBBXIntersection(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h)
{
register u8g_uint_t tmp;
tmp = y;
tmp += h;
tmp--;
if ( u8g_is_intersection_decision_tree(u8g->current_page.y0, u8g->current_page.y1, y, tmp) == 0 )
return 0;
tmp = x;
tmp += w;
tmp--;
return u8g_is_intersection_decision_tree(u8g->current_page.x0, u8g->current_page.x1, x, tmp);
}
/*
u8g_com_api.c
Universal 8bit Graphics Library
Copyright (c) 2011, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "u8g.h"
uint8_t u8g_InitCom(u8g_t *u8g, u8g_dev_t *dev, uint8_t clk_cycle_time)
{
return dev->com_fn(u8g, U8G_COM_MSG_INIT, clk_cycle_time, NULL);
}
void u8g_StopCom(u8g_t *u8g, u8g_dev_t *dev)
{
dev->com_fn(u8g, U8G_COM_MSG_STOP, 0, NULL);
}
/* cs contains the chip number, which should be enabled */
void u8g_SetChipSelect(u8g_t *u8g, u8g_dev_t *dev, uint8_t cs)
{
dev->com_fn(u8g, U8G_COM_MSG_CHIP_SELECT, cs, NULL);
}
void u8g_SetResetLow(u8g_t *u8g, u8g_dev_t *dev)
{
dev->com_fn(u8g, U8G_COM_MSG_RESET, 0, NULL);
}
void u8g_SetResetHigh(u8g_t *u8g, u8g_dev_t *dev)
{
dev->com_fn(u8g, U8G_COM_MSG_RESET, 1, NULL);
}
void u8g_SetAddress(u8g_t *u8g, u8g_dev_t *dev, uint8_t address)
{
dev->com_fn(u8g, U8G_COM_MSG_ADDRESS, address, NULL);
}
uint8_t u8g_WriteByte(u8g_t *u8g, u8g_dev_t *dev, uint8_t val)
{
return dev->com_fn(u8g, U8G_COM_MSG_WRITE_BYTE, val, NULL);
}
uint8_t u8g_WriteSequence(u8g_t *u8g, u8g_dev_t *dev, uint8_t cnt, uint8_t *seq)
{
return dev->com_fn(u8g, U8G_COM_MSG_WRITE_SEQ, cnt, seq);
}
uint8_t u8g_WriteSequenceP(u8g_t *u8g, u8g_dev_t *dev, uint8_t cnt, const uint8_t *seq)
{
return dev->com_fn(u8g, U8G_COM_MSG_WRITE_SEQ_P, cnt, (void *)seq);
}
/*
sequence := { direct_value | escape_sequence }
direct_value := 0..254
escape_sequence := value_255 | sequence_end | delay | adr | cs | not_used
value_255 := 255 255
sequence_end = 255 254
delay := 255 0..127
adr := 255 0x0e0 .. 0x0ef
cs := 255 0x0d0 .. 0x0df
not_used := 255 101..254
#define U8G_ESC_DLY(x) 255, ((x) & 0x7f)
#define U8G_ESC_CS(x) 255, (0xd0 | ((x)&0x0f))
#define U8G_ESC_ADR(x) 255, (0xe0 | ((x)&0x0f))
#define U8G_ESC_VCC(x) 255, (0xbe | ((x)&0x01))
#define U8G_ESC_END 255, 254
#define U8G_ESC_255 255, 255
#define U8G_ESC_RST(x) 255, (0xc0 | ((x)&0x0f))
*/
uint8_t u8g_WriteEscSeqP(u8g_t *u8g, u8g_dev_t *dev, const uint8_t *esc_seq)
{
uint8_t is_escape = 0;
uint8_t value;
for(;;)
{
value = u8g_pgm_read(esc_seq);
if ( is_escape == 0 )
{
if ( value != 255 )
{
if ( u8g_WriteByte(u8g, dev, value) == 0 )
return 0;
}
else
{
is_escape = 1;
}
}
else
{
if ( value == 255 )
{
if ( u8g_WriteByte(u8g, dev, value) == 0 )
return 0;
}
else if ( value == 254 )
{
break;
}
else if ( value >= 0x0f0 )
{
/* not yet used, do nothing */
}
else if ( value >= 0xe0 )
{
u8g_SetAddress(u8g, dev, value & 0x0f);
}
else if ( value >= 0xd0 )
{
u8g_SetChipSelect(u8g, dev, value & 0x0f);
}
else if ( value >= 0xc0 )
{
u8g_SetResetLow(u8g, dev);
value &= 0x0f;
value <<= 4;
value+=2;
u8g_Delay(value);
u8g_SetResetHigh(u8g, dev);
u8g_Delay(value);
}
else if ( value >= 0xbe )
{
/* not yet implemented */
/* u8g_SetVCC(u8g, dev, value & 0x01); */
}
else if ( value <= 127 )
{
u8g_Delay(value);
}
is_escape = 0;
}
esc_seq++;
}
return 1;
}
/*
u8g_com_api_16gr.c
Extension of the com api for devices with 16 graylevels (4 bit per pixel).
This should fit to the 8h and 16h architectures (pb8v1, pb8v2, pb16v1, pb16v2),
mainly intended for SSD OLEDs
Universal 8bit Graphics Library
Copyright (c) 2011, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "u8g.h"
/* interpret b as a monochrome bit pattern, write value 15 for high bit and value 0 for a low bit */
/* topbit (msb) is sent last */
/* example: b = 0x083 will send 0xff, 0x00, 0x00, 0xf0 */
uint8_t u8g_WriteByteBWTo16GrDevice(u8g_t *u8g, u8g_dev_t *dev, uint8_t b)
{
static uint8_t buf[4];
static uint8_t map[4] = { 0, 0x00f, 0x0f0, 0x0ff };
buf [3] = map[b & 3];
b>>=2;
buf [2] = map[b & 3];
b>>=2;
buf [1] = map[b & 3];
b>>=2;
buf [0] = map[b & 3];
return dev->com_fn(u8g, U8G_COM_MSG_WRITE_SEQ, 4, buf);
}
uint8_t u8g_WriteSequenceBWTo16GrDevice(u8g_t *u8g, u8g_dev_t *dev, uint8_t cnt, uint8_t *ptr)
{
do
{
if ( u8g_WriteByteBWTo16GrDevice(u8g, dev, *ptr++) == 0 )
return 0;
cnt--;
} while( cnt != 0 );
return 1;
}
/* interpret b as a 4L bit pattern, write values 0x000, 0x004, 0x008, 0x00c */
uint8_t u8g_WriteByte4LTo16GrDevice(u8g_t *u8g, u8g_dev_t *dev, uint8_t b)
{
//static uint8_t map[16] = { 0x000, 0x004, 0x008, 0x00c, 0x040, 0x044, 0x048, 0x04c, 0x080, 0x084, 0x088, 0x08c, 0x0c0, 0x0c4, 0x0c8, 0x0cc};
//static uint8_t map[16] = { 0x000, 0x004, 0x00a, 0x00f, 0x040, 0x044, 0x04a, 0x04f, 0x0a0, 0x0a4, 0x0aa, 0x0af, 0x0f0, 0x0f4, 0x0fa, 0x0ff};
static uint8_t map[16] = { 0x000, 0x040, 0x0a0, 0x0f0, 0x004, 0x044, 0x0a4, 0x0f4, 0x00a, 0x04a, 0x0aa, 0x0fa, 0x00f, 0x04f, 0x0af, 0x0ff};
uint8_t bb;
bb = b;
bb &= 15;
b>>=4;
dev->com_fn(u8g, U8G_COM_MSG_WRITE_BYTE, map[bb], NULL);
return dev->com_fn(u8g, U8G_COM_MSG_WRITE_BYTE, map[b], NULL);
}
uint8_t u8g_WriteSequence4LTo16GrDevice(u8g_t *u8g, u8g_dev_t *dev, uint8_t cnt, uint8_t *ptr)
{
do
{
if ( u8g_WriteByte4LTo16GrDevice(u8g, dev, *ptr++) == 0 )
return 0;
cnt--;
} while( cnt != 0 );
return 1;
}
/*
u8g_com_i2c.c
generic i2c interface
Universal 8bit Graphics Library
Copyright (c) 2011, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "u8g.h"
//#define U8G_I2C_WITH_NO_ACK
static uint8_t u8g_i2c_err_code;
static uint8_t u8g_i2c_opt; /* U8G_I2C_OPT_NO_ACK, SAM: U8G_I2C_OPT_DEV_1 */
/*
position values
1: start condition
2: sla transfer
*/
static uint8_t u8g_i2c_err_pos;
void u8g_i2c_clear_error(void)
{
u8g_i2c_err_code = U8G_I2C_ERR_NONE;
u8g_i2c_err_pos = 0;
}
uint8_t u8g_i2c_get_error(void)
{
return u8g_i2c_err_code;
}
uint8_t u8g_i2c_get_err_pos(void)
{
return u8g_i2c_err_pos;
}
static void u8g_i2c_set_error(uint8_t code, uint8_t pos)
{
if ( u8g_i2c_err_code > 0 )
return;
u8g_i2c_err_code |= code;
u8g_i2c_err_pos = pos;
}
#if defined(__AVR__)
#define U8G_ATMEGA_HW_TWI
/* remove the definition for attiny */
#if __AVR_ARCH__ == 2
#undef U8G_ATMEGA_HW_TWI
#endif
#if __AVR_ARCH__ == 25
#undef U8G_ATMEGA_HW_TWI
#endif
#endif
#if defined(U8G_ATMEGA_HW_TWI)
#include <avr/io.h>
#include <util/twi.h>
void u8g_i2c_init(uint8_t options)
{
/*
TWBR: bit rate register
TWSR: status register (contains preselector bits)
prescalar
0 1
1 4
2 16
3 64
f = F_CPU/(16+2*TWBR*prescalar)
F_CPU = 16MHz
TWBR = 152;
TWSR = 0;
--> 50KHz
TWBR = 72;
TWSR = 0;
--> 100KHz
TWBR = 12;
TWSR = 0;
--> 400KHz
F_CPU/(2*100000)-8 --> calculate TWBR value for 100KHz
*/
u8g_i2c_opt = options;
TWSR = 0;
if ( options & U8G_I2C_OPT_FAST )
{
TWBR = F_CPU/(2*400000)-8;
}
else
{
TWBR = F_CPU/(2*100000)-8;
}
u8g_i2c_clear_error();
}
uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
{
volatile uint16_t cnt = 2000; /* timout value should be > 280 for 50KHz Bus and 16 Mhz CPU, however the start condition might need longer */
while( !(TWCR & mask) )
{
if ( cnt == 0 )
{
if ( u8g_i2c_opt & U8G_I2C_OPT_NO_ACK )
{
return 1; /* all ok */
}
else
{
u8g_i2c_set_error(U8G_I2C_ERR_TIMEOUT, pos);
return 0; /* error */
}
}
cnt--;
}
return 1; /* all ok */
}
/* sla includes all 8 bits (with r/w bit), assums master transmit */
uint8_t u8g_i2c_start(uint8_t sla)
{
register uint8_t status;
/* send start */
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
/* wait */
if ( u8g_i2c_wait(_BV(TWINT), 1) == 0 )
return 0;
status = TW_STATUS;
/* check status after start */
if ( status != TW_START && status != TW_REP_START )
{
u8g_i2c_set_error(U8G_I2C_ERR_BUS, 1);
return 0;
}
/* set slave address */
TWDR = sla;
/* enable sla transfer */
TWCR = _BV(TWINT) | _BV(TWEN);
/* wait */
if ( u8g_i2c_wait(_BV(TWINT), 2) == 0 )
return 0;
if ( u8g_i2c_opt & U8G_I2C_OPT_NO_ACK )
{
/* do not check for ACK */
}
else
{
status = TW_STATUS;
/* check status after sla */
if ( status != TW_MT_SLA_ACK )
{
u8g_i2c_set_error(U8G_I2C_ERR_BUS, 2);
return 0;
}
}
return 1;
}
uint8_t u8g_i2c_send_byte(uint8_t data)
{
register uint8_t status;
TWDR = data;
TWCR = _BV(TWINT) | _BV(TWEN);
if ( u8g_i2c_wait(_BV(TWINT), 3) == 0 )
return 0;
if ( u8g_i2c_opt & U8G_I2C_OPT_NO_ACK )
{
/* do not check for ACK */
}
else
{
status = TW_STATUS;
if ( status != TW_MT_DATA_ACK )
{
u8g_i2c_set_error(U8G_I2C_ERR_BUS, 3);
return 0;
}
}
return 1;
}
void u8g_i2c_stop(void)
{
/* write stop */
TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWSTO);
/* no error is checked for the stop condition */
u8g_i2c_wait(_BV(TWSTO), 4);
}
/*
void twi_send(uint8_t adr, uint8_t data1, uint8_t data2)
{
u8g_i2c_start(adr<<1);
u8g_i2c_send_byte(data1);
u8g_i2c_send_byte(data2);
u8g_i2c_stop();
}
*/
#elif defined(ARDUINO) && defined(__SAM3X8E__)
/* Arduino Due */
#include "Arduino.h"
#include "sam.h"
/*
Controller
TWI0 TWCK0 PA18 A DUE PCB: SCL1
TWI0 TWD0 PA17 A DUE PCB: SDA1
TWI1 TWCK1 PB13 A DUE PCB: SCL 21
TWI1 TWD1 PB12 A DUE PCB: SDA 20
Arduino definitions
#define PIN_WIRE_SDA (20u)
#define PIN_WIRE_SCL (21u)
#define WIRE_INTERFACE TWI1
#define WIRE_INTERFACE_ID ID_TWI1
#define WIRE_ISR_HANDLER TWI1_Handler
#define PIN_WIRE1_SDA (70u)
#define PIN_WIRE1_SCL (71u)
#define WIRE1_INTERFACE TWI0
#define WIRE1_INTERFACE_ID ID_TWI0
#define WIRE1_ISR_HANDLER TWI0_Handler
*/
static void i2c_400KHz_delay(void)
{
/* should be at least 4 */
/* should be 5 for 100KHz transfer speed */
/*
Arduino Due
0x NOP: 470KHz
4x NOP: 450KHz
8x NOP: 430KHz
16x NOP: 400KHz
*/
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
static void i2c_100KHz_delay(void)
{
/*
1x u8g_MicroDelay() ca. 130KHz
2x u8g_MicroDelay() ca. 80KHz
*/
u8g_MicroDelay();
u8g_MicroDelay();
}
uint32_t i2c_started = 0;
uint32_t i2c_scl_pin = 0;
uint32_t i2c_sda_pin = 0;
void (*i2c_delay)(void) = i2c_100KHz_delay;
const PinDescription *i2c_scl_pin_desc;
const PinDescription *i2c_sda_pin_desc;
/* maybe this can be optimized */
static void i2c_init(void)
{
i2c_sda_pin_desc = &(g_APinDescription[i2c_sda_pin]);
i2c_scl_pin_desc = &(g_APinDescription[i2c_scl_pin]);
pinMode(i2c_sda_pin, OUTPUT);
digitalWrite(i2c_sda_pin, HIGH);
pinMode(i2c_scl_pin, OUTPUT);
digitalWrite(i2c_scl_pin, HIGH);
PIO_Configure( i2c_sda_pin_desc->pPort, PIO_OUTPUT_0, i2c_sda_pin_desc->ulPin, PIO_OPENDRAIN );
PIO_Configure( i2c_scl_pin_desc->pPort, PIO_OUTPUT_0, i2c_scl_pin_desc->ulPin, PIO_OPENDRAIN );
PIO_Clear( i2c_sda_pin_desc->pPort, i2c_sda_pin_desc->ulPin) ;
PIO_Clear( i2c_scl_pin_desc->pPort, i2c_scl_pin_desc->ulPin) ;
PIO_Configure( i2c_sda_pin_desc->pPort, PIO_INPUT, i2c_sda_pin_desc->ulPin, PIO_DEFAULT ) ;
PIO_Configure( i2c_scl_pin_desc->pPort, PIO_INPUT, i2c_scl_pin_desc->ulPin, PIO_DEFAULT ) ;
i2c_delay();
}
/* actually, the scl line is not observed, so this procedure does not return a value */
static void i2c_read_scl_and_delay(void)
{
uint32_t dwMask = i2c_scl_pin_desc->ulPin;
//PIO_Configure( i2c_scl_pin_desc->pPort, PIO_INPUT, i2c_scl_pin_desc->ulPin, PIO_DEFAULT ) ;
//PIO_SetInput( i2c_scl_pin_desc->pPort, i2c_scl_pin_desc->ulPin, PIO_DEFAULT ) ;
/* set as input */
i2c_scl_pin_desc->pPort->PIO_ODR = dwMask ;
i2c_scl_pin_desc->pPort->PIO_PER = dwMask ;
i2c_delay();
}
static void i2c_clear_scl(void)
{
uint32_t dwMask = i2c_scl_pin_desc->ulPin;
/* set open collector and drive low */
//PIO_Configure( i2c_scl_pin_desc->pPort, PIO_OUTPUT_0, i2c_scl_pin_desc->ulPin, PIO_OPENDRAIN );
//PIO_SetOutput( i2c_scl_pin_desc->pPort, i2c_scl_pin_desc->ulPin, 0, 1, 0);
/* open drain, zero default output */
i2c_scl_pin_desc->pPort->PIO_MDER = dwMask;
i2c_scl_pin_desc->pPort->PIO_CODR = dwMask;
i2c_scl_pin_desc->pPort->PIO_OER = dwMask;
i2c_scl_pin_desc->pPort->PIO_PER = dwMask;
//PIO_Clear( i2c_scl_pin_desc->pPort, i2c_scl_pin_desc->ulPin) ;
}
static uint8_t i2c_read_sda(void)
{
uint32_t dwMask = i2c_sda_pin_desc->ulPin;
//PIO_Configure( i2c_sda_pin_desc->pPort, PIO_INPUT, i2c_sda_pin_desc->ulPin, PIO_DEFAULT ) ;
//PIO_SetInput( i2c_sda_pin_desc->pPort, i2c_sda_pin_desc->ulPin, PIO_DEFAULT ) ;
/* set as input */
i2c_sda_pin_desc->pPort->PIO_ODR = dwMask ;
i2c_sda_pin_desc->pPort->PIO_PER = dwMask ;
return 1;
}
static void i2c_clear_sda(void)
{
uint32_t dwMask = i2c_sda_pin_desc->ulPin;
/* set open collector and drive low */
//PIO_Configure( i2c_sda_pin_desc->pPort, PIO_OUTPUT_0, i2c_sda_pin_desc->ulPin, PIO_OPENDRAIN );
//PIO_SetOutput( i2c_sda_pin_desc->pPort, i2c_sda_pin_desc->ulPin, 0, 1, 0);
/* open drain, zero default output */
i2c_sda_pin_desc->pPort->PIO_MDER = dwMask ;
i2c_sda_pin_desc->pPort->PIO_CODR = dwMask ;
i2c_sda_pin_desc->pPort->PIO_OER = dwMask ;
i2c_sda_pin_desc->pPort->PIO_PER = dwMask ;
//PIO_Clear( i2c_sda_pin_desc->pPort, i2c_sda_pin_desc->ulPin) ;
}
static void i2c_start(void)
{
if ( i2c_started != 0 )
{
/* if already started: do restart */
i2c_read_sda(); /* SDA = 1 */
i2c_delay();
i2c_read_scl_and_delay();
}
i2c_read_sda();
/*
if (i2c_read_sda() == 0)
{
// do something because arbitration is lost
}
*/
/* send the start condition, both lines go from 1 to 0 */
i2c_clear_sda();
i2c_delay();
i2c_clear_scl();
i2c_started = 1;
}
static void i2c_stop(void)
{
/* set SDA to 0 */
i2c_clear_sda();
i2c_delay();
/* now release all lines */
i2c_read_scl_and_delay();
/* set SDA to 1 */
i2c_read_sda();
i2c_delay();
i2c_started = 0;
}
static void i2c_write_bit(uint8_t val)
{
if (val)
i2c_read_sda();
else
i2c_clear_sda();
i2c_delay();
i2c_read_scl_and_delay();
i2c_clear_scl();
}
static uint8_t i2c_read_bit(void)
{
uint8_t val;
/* do not drive SDA */
i2c_read_sda();
i2c_delay();
i2c_read_scl_and_delay();
val = i2c_read_sda();
i2c_delay();
i2c_clear_scl();
return val;
}
static uint8_t i2c_write_byte(uint8_t b)
{
i2c_write_bit(b & 128);
i2c_write_bit(b & 64);
i2c_write_bit(b & 32);
i2c_write_bit(b & 16);
i2c_write_bit(b & 8);
i2c_write_bit(b & 4);
i2c_write_bit(b & 2);
i2c_write_bit(b & 1);
/* read ack from client */
/* 0: ack was given by client */
/* 1: nothing happend during ack cycle */
return i2c_read_bit();
}
void u8g_i2c_init(uint8_t options)
{
u8g_i2c_opt = options;
u8g_i2c_clear_error();
if ( u8g_i2c_opt & U8G_I2C_OPT_FAST )
{
i2c_delay = i2c_400KHz_delay;
}
else
{
i2c_delay = i2c_100KHz_delay;
}
if ( u8g_i2c_opt & U8G_I2C_OPT_DEV_1 )
{
i2c_scl_pin = PIN_WIRE1_SCL;
i2c_sda_pin = PIN_WIRE1_SDA;
//REG_PIOA_PDR = PIO_PB12A_TWD1 | PIO_PB13A_TWCK1;
}
else
{
i2c_scl_pin = PIN_WIRE_SCL;
i2c_sda_pin = PIN_WIRE_SDA;
//REG_PIOA_PDR = PIO_PA17A_TWD0 | PIO_PA18A_TWCK0;
}
i2c_init();
}
/* sla includes also the r/w bit */
uint8_t u8g_i2c_start(uint8_t sla)
{
i2c_start();
i2c_write_byte(sla);
return 1;
}
uint8_t u8g_i2c_send_byte(uint8_t data)
{
return i2c_write_byte(data);
}
void u8g_i2c_stop(void)
{
i2c_stop();
}
#elif defined(U8G_RASPBERRY_PI)
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define I2C_SLA 0x3c
static int fd=-1;
static uint8_t i2cMode = 0;
void u8g_i2c_init(uint8_t options) {
u8g_i2c_clear_error();
u8g_i2c_opt = options;
if (wiringPiSetup() == -1) {
printf("wiringPi-Error\n");
exit(1);
}
fd = wiringPiI2CSetup(I2C_SLA);
if (fd < 0) {
printf ("Unable to open I2C device 0: %s\n", strerror (errno)) ;
exit (1) ;
}
//u8g_SetPIOutput(u8g, U8G_PI_RESET);
//u8g_SetPIOutput(u8g, U8G_PI_A0);
}
uint8_t u8g_i2c_start(uint8_t sla) {
u8g_i2c_send_mode(0);
return 1;
}
void u8g_i2c_stop(void) {
}
uint8_t u8g_i2c_send_mode(uint8_t mode) {
i2cMode = mode;
}
uint8_t u8g_i2c_send_byte(uint8_t data) {
wiringPiI2CWriteReg8(fd, i2cMode, data);
return 1;
}
uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
{
return 1;
}
#else
/* empty interface */
void u8g_i2c_init(uint8_t options)
{
u8g_i2c_clear_error();
}
uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
{
return 1;
}
uint8_t u8g_i2c_start(uint8_t sla)
{
return 1;
}
uint8_t u8g_i2c_send_byte(uint8_t data)
{
return 1;
}
void u8g_i2c_stop(void)
{
}
#endif
This diff is collapsed.
/*
u8g_com_null.c
communication null device
Universal 8bit Graphics Library
Copyright (c) 2011, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "u8g.h"
uint8_t u8g_com_null_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
{
switch(msg)
{
case U8G_COM_MSG_INIT:
break;
case U8G_COM_MSG_STOP:
break;
case U8G_COM_MSG_CHIP_SELECT:
/* arg_val contains the chip number, which should be enabled */
break;
case U8G_COM_MSG_WRITE_BYTE:
break;
case U8G_COM_MSG_WRITE_SEQ:
break;
}
return 1;
}
/*
u8g_com_raspberrypi_hw_spi.c
Universal 8bit Graphics Library
Copyright (c) 2012, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Assumes, that
MOSI is at PORTB, Pin 3
and
SCK is at PORTB, Pin 5
Update for ATOMIC operation done (01 Jun 2013)
U8G_ATOMIC_OR(ptr, val)
U8G_ATOMIC_AND(ptr, val)
U8G_ATOMIC_START()
U8G_ATOMIC_END()
*/
#include "u8g.h"
#if defined(U8G_RASPBERRY_PI)
#include <wiringPiSPI.h>
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
uint8_t u8g_com_raspberrypi_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
{
switch(msg)
{
case U8G_COM_MSG_STOP:
break;
case U8G_COM_MSG_INIT:
// check wiringPi setup
if (wiringPiSetup() == -1)
{
printf("wiringPi-Error\n");
exit(1);
}
if (wiringPiSPISetup (0, 100000) < 0)
{
printf ("Unable to open SPI device 0: %s\n", strerror (errno)) ;
exit (1) ;
}
u8g_SetPIOutput(u8g, U8G_PI_RESET);
u8g_SetPIOutput(u8g, U8G_PI_A0);
break;
case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
u8g_SetPILevel(u8g, U8G_PI_A0, arg_val);
break;
case U8G_COM_MSG_CHIP_SELECT:
/* Done by the SPI hardware */
break;
case U8G_COM_MSG_RESET:
u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
break;
case U8G_COM_MSG_WRITE_BYTE:
wiringPiSPIDataRW (0, &arg_val, 1) ;
break;
case U8G_COM_MSG_WRITE_SEQ:
wiringPiSPIDataRW (0, arg_ptr, arg_val);
break;
case U8G_COM_MSG_WRITE_SEQ_P:
wiringPiSPIDataRW (0, arg_ptr, arg_val);
break;
}
return 1;
}
#else
uint8_t u8g_com_raspberrypi_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
{
return 1;
}
#endif
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