Commit 2433bd30 authored by Markéta Jedličková's avatar Markéta Jedličková
Browse files

Library UcgLib added

parent d7344ab0
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
BSD license, all text above must be included in any redistribution BSD license, all text above must be included in any redistribution
***************************************************************************/ ***************************************************************************/
#include "Adafruit_BME280.h" #include "Adafruit_BME280.h"
#include "Arduino.h" #include "Arduino.h"
#include <Wire.h> #include <Wire.h>
#include <SPI.h> #include <SPI.h>
......
...@@ -13,11 +13,11 @@ ...@@ -13,11 +13,11 @@
Written by Kevin Townsend for Adafruit Industries. Written by Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution BSD license, all text above must be included in any redistribution
***************************************************************************/ ***************************************************************************/
#include "Adafruit_BMP280.h"
#include "Arduino.h" #include "Arduino.h"
#include <Wire.h> #include <Wire.h>
#include <SPI.h> #include <SPI.h>
#include "Adafruit_BMP280.h"
/*************************************************************************** /***************************************************************************
PRIVATE FUNCTIONS PRIVATE FUNCTIONS
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
* BSD license, all text here must be included in any redistribution. * BSD license, all text here must be included in any redistribution.
* *
*/ */
#include "Adafruit_INA219.h"
#if ARDUINO >= 100 #if ARDUINO >= 100
#include "Arduino.h" #include "Arduino.h"
...@@ -32,7 +33,7 @@ ...@@ -32,7 +33,7 @@
#include <Wire.h> #include <Wire.h>
#include "Adafruit_INA219.h"
/**************************************************************************/ /**************************************************************************/
/*! /*!
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#define _OCSGRAPHICS_HH #define _OCSGRAPHICS_HH
#include <Arduino.h> #include <Arduino.h>
#include "src/Ucglib.h" #include "Ucglib.h"
class OcsGraphics class OcsGraphics
{ {
......
/*
Ucglib.cpp
ucglib = universal color graphics library
ucglib = micro controller graphics library
Universal uC Color Graphics Library
Copyright (c) 2014, 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 s_t ource 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 <SPI.h>
#include "Ucglib.h"
/*=========================================================================*/
/* 8 Bit SW SPI */
#if defined(__SAM3X8E__)
//#elif defined(__SAM3X8E__)
//#define setbit(pio, mask) PIO_Set( (pio), (mask) )
//#define clrbit(pio, mask) PIO_Clear( (pio), (mask) )
#define setbit(pio, mask) ((pio)->PIO_SODR = (mask))
#define clrbit(pio, mask) ((pio)->PIO_CODR = (mask))
static void ucg_nano_delay(void)
{
volatile uint32_t i;
for( i = 0; i < 1; i++ )
{
__NOP;
}
//delayMicroseconds(1);
}
static void ucg_com_arduino_send_generic_SW_SPI(ucg_t *ucg, uint8_t data)
{
uint32_t sda_pin = ucg->pin_list[UCG_PIN_SDA];
uint32_t scl_pin = ucg->pin_list[UCG_PIN_SCL];
Pio *sda_port = g_APinDescription[sda_pin].pPort;
Pio *scl_port = g_APinDescription[scl_pin].pPort;
uint8_t i = 8;
sda_pin = g_APinDescription[sda_pin].ulPin;
scl_pin = g_APinDescription[scl_pin].ulPin;
do
{
if ( data & 128 )
{
setbit( sda_port, sda_pin) ;
}
else
{
clrbit( sda_port, sda_pin) ;
}
//delayMicroseconds(1);
ucg_nano_delay();
setbit( scl_port, scl_pin);
//delayMicroseconds(1);
ucg_nano_delay();
i--;
clrbit( scl_port, scl_pin) ;
data <<= 1;
} while( i > 0 );
}
#elif defined(__AVR__)
uint8_t u8g_bitData, u8g_bitNotData;
uint8_t u8g_bitClock, u8g_bitNotClock;
volatile uint8_t *u8g_outData;
volatile uint8_t *u8g_outClock;
static void ucg_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
{
u8g_outData = portOutputRegister(digitalPinToPort(dataPin));
u8g_outClock = portOutputRegister(digitalPinToPort(clockPin));
u8g_bitData = digitalPinToBitMask(dataPin);
u8g_bitClock = digitalPinToBitMask(clockPin);
u8g_bitNotClock = u8g_bitClock;
u8g_bitNotClock ^= 0x0ff;
u8g_bitNotData = u8g_bitData;
u8g_bitNotData ^= 0x0ff;
}
static void ucg_com_arduino_send_generic_SW_SPI(ucg_t *ucg, uint8_t val) UCG_NOINLINE;
static void ucg_com_arduino_send_generic_SW_SPI(ucg_t *ucg, uint8_t val)
{
uint8_t cnt = 8;
uint8_t bitData = u8g_bitData;
uint8_t bitNotData = u8g_bitNotData;
uint8_t bitClock = u8g_bitClock;
uint8_t bitNotClock = u8g_bitNotClock;
volatile uint8_t *outData = u8g_outData;
volatile uint8_t *outClock = u8g_outClock;
UCG_ATOMIC_START();
do
{
if ( val & 128 )
*outData |= bitData;
else
*outData &= bitNotData;
*outClock |= bitClock;
val <<= 1;
cnt--;
*outClock &= bitNotClock;
} while( cnt != 0 );
UCG_ATOMIC_END();
}
#else
static void ucg_com_arduino_send_generic_SW_SPI(ucg_t *ucg, uint8_t data)
{
uint8_t i = 8;
do
{
if ( data & 128 )
{
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 1 );
}
else
{
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 0 );
}
// no delay required, also Arduino Due is slow enough
// delay required for ESP32
delayMicroseconds(1);
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 1 );
delayMicroseconds(1);
i--;
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 0 );
delayMicroseconds(1);
data <<= 1;
} while( i > 0 );
}
#endif
static int16_t ucg_com_arduino_generic_SW_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 */
#ifdef __AVR__
ucg_com_arduino_init_shift_out(ucg->pin_list[UCG_PIN_SDA], ucg->pin_list[UCG_PIN_SCL]);
#endif
/* setup pins */
pinMode(ucg->pin_list[UCG_PIN_CD], OUTPUT);
pinMode(ucg->pin_list[UCG_PIN_SDA], OUTPUT);
pinMode(ucg->pin_list[UCG_PIN_SCL], OUTPUT);
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
pinMode(ucg->pin_list[UCG_PIN_CS], OUTPUT);
if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
pinMode(ucg->pin_list[UCG_PIN_RST], OUTPUT);
digitalWrite(ucg->pin_list[UCG_PIN_CD], 1);
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 1);
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 0);
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
digitalWrite(ucg->pin_list[UCG_PIN_CS], 1);
if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
digitalWrite(ucg->pin_list[UCG_PIN_RST], 1);
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 )
digitalWrite(ucg->pin_list[UCG_PIN_RST], arg);
break;
case UCG_COM_MSG_CHANGE_CS_LINE:
#ifdef __AVR__
ucg_com_arduino_init_shift_out(ucg->pin_list[UCG_PIN_SDA], ucg->pin_list[UCG_PIN_SCL]);
#endif
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
digitalWrite(ucg->pin_list[UCG_PIN_CS], arg);
break;
case UCG_COM_MSG_CHANGE_CD_LINE:
digitalWrite(ucg->pin_list[UCG_PIN_CD], arg);
break;
case UCG_COM_MSG_SEND_BYTE:
ucg_com_arduino_send_generic_SW_SPI(ucg, arg);
break;
case UCG_COM_MSG_REPEAT_1_BYTE:
while( arg > 0 ) {
ucg_com_arduino_send_generic_SW_SPI(ucg, data[0]);
arg--;
}
break;
case UCG_COM_MSG_REPEAT_2_BYTES:
while( arg > 0 ) {
ucg_com_arduino_send_generic_SW_SPI(ucg, data[0]);
ucg_com_arduino_send_generic_SW_SPI(ucg, data[1]);
arg--;
}
break;
case UCG_COM_MSG_REPEAT_3_BYTES:
while( arg > 0 ) {
ucg_com_arduino_send_generic_SW_SPI(ucg, data[0]);
ucg_com_arduino_send_generic_SW_SPI(ucg, data[1]);
ucg_com_arduino_send_generic_SW_SPI(ucg, data[2]);
arg--;
}
break;
case UCG_COM_MSG_SEND_STR:
while( arg > 0 ) {
ucg_com_arduino_send_generic_SW_SPI(ucg, *data++);
arg--;
}
break;
case UCG_COM_MSG_SEND_CD_DATA_SEQUENCE:
while(arg > 0)
{
if ( *data != 0 )
{
if ( *data == 1 )
{
digitalWrite(ucg->pin_list[UCG_PIN_CD], 0);
}
else
{
digitalWrite(ucg->pin_list[UCG_PIN_CD], 1);
}
}
data++;
ucg_com_arduino_send_generic_SW_SPI(ucg, *data);
data++;
arg--;
}
break;
}
return 1;
}
void Ucglib4WireSWSPI::begin(uint8_t is_transparent)
{
ucg_Init(&ucg, dev_cb, ext_cb, ucg_com_arduino_generic_SW_SPI);
ucg_SetFontMode(&ucg, is_transparent);
}
/*=========================================================================*/
/* 8 Bit SW SPI for ILI9325 (mode IM3=0, IM2=1, IM1=0, IM0=0 */
static int16_t ucg_com_arduino_illi9325_SW_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 */
#ifdef __AVR__
ucg_com_arduino_init_shift_out(ucg->pin_list[UCG_PIN_SDA], ucg->pin_list[UCG_PIN_SCL]);
#endif
/* setup pins */
pinMode(ucg->pin_list[UCG_PIN_CD], OUTPUT);
pinMode(ucg->pin_list[UCG_PIN_SDA], OUTPUT);
pinMode(ucg->pin_list[UCG_PIN_SCL], OUTPUT);
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
pinMode(ucg->pin_list[UCG_PIN_CS], OUTPUT);
if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
pinMode(ucg->pin_list[UCG_PIN_RST], OUTPUT);
digitalWrite(ucg->pin_list[UCG_PIN_CD], 1);
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 1);
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 0);
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
digitalWrite(ucg->pin_list[UCG_PIN_CS], 1);
if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
digitalWrite(ucg->pin_list[UCG_PIN_RST], 1);
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 )
digitalWrite(ucg->pin_list[UCG_PIN_RST], arg);
break;
case UCG_COM_MSG_CHANGE_CS_LINE:
#ifdef __AVR__
ucg_com_arduino_init_shift_out(ucg->pin_list[UCG_PIN_SDA], ucg->pin_list[UCG_PIN_SCL]);
#endif
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
digitalWrite(ucg->pin_list[UCG_PIN_CS], arg);
break;
case UCG_COM_MSG_CHANGE_CD_LINE:
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
{
digitalWrite(ucg->pin_list[UCG_PIN_CS], 1);
digitalWrite(ucg->pin_list[UCG_PIN_CS], 0);
}
if ( ucg->com_status & UCG_COM_STATUS_MASK_CD )
ucg_com_arduino_send_generic_SW_SPI(ucg, 0x072);
else
ucg_com_arduino_send_generic_SW_SPI(ucg, 0x070);
break;
case UCG_COM_MSG_SEND_BYTE:
ucg_com_arduino_send_generic_SW_SPI(ucg, arg);
break;
case UCG_COM_MSG_REPEAT_1_BYTE:
while( arg > 0 ) {
ucg_com_arduino_send_generic_SW_SPI(ucg, data[0]);
arg--;
}
break;
case UCG_COM_MSG_REPEAT_2_BYTES:
while( arg > 0 ) {
ucg_com_arduino_send_generic_SW_SPI(ucg, data[0]);
ucg_com_arduino_send_generic_SW_SPI(ucg, data[1]);
arg--;
}
break;
case UCG_COM_MSG_REPEAT_3_BYTES:
while( arg > 0 ) {
ucg_com_arduino_send_generic_SW_SPI(ucg, data[0]);
ucg_com_arduino_send_generic_SW_SPI(ucg, data[1]);
ucg_com_arduino_send_generic_SW_SPI(ucg, data[2]);
arg--;
}
break;
case UCG_COM_MSG_SEND_STR:
while( arg > 0 ) {
ucg_com_arduino_send_generic_SW_SPI(ucg, *data++);
arg--;
}
break;
case UCG_COM_MSG_SEND_CD_DATA_SEQUENCE:
while(arg > 0)
{
if ( *data != 0 )
{
if ( *data == 1 )
{
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
{
digitalWrite(ucg->pin_list[UCG_PIN_CS], 1);
digitalWrite(ucg->pin_list[UCG_PIN_CS], 0);
}
ucg_com_arduino_send_generic_SW_SPI(ucg, 0x070);
}
else
{
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
{
digitalWrite(ucg->pin_list[UCG_PIN_CS], 1);
digitalWrite(ucg->pin_list[UCG_PIN_CS], 0);
}
ucg_com_arduino_send_generic_SW_SPI(ucg, 0x072);
}
}
data++;
ucg_com_arduino_send_generic_SW_SPI(ucg, *data);
data++;
arg--;
}
break;
}
return 1;
}
void Ucglib3WireILI9325SWSPI::begin(uint8_t is_transparent)
{
ucg_Init(&ucg, dev_cb, ext_cb, ucg_com_arduino_illi9325_SW_SPI);
ucg_SetFontMode(&ucg, is_transparent);
}
/*=========================================================================*/
/* 9 Bit SW SPI */
static void ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg_t *ucg, uint8_t first_bit, uint8_t data)
{
uint8_t i;
if ( first_bit != 0 )
{
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 1 );
}
else
{
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 0 );
}
// no delay required, also Arduino Due is slow enough
//delayMicroseconds(1);
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 1 );
//delayMicroseconds(1);
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 0 );
//delayMicroseconds(1);
i = 8;
do
{
if ( data & 128 )
{
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 1 );
}
else
{
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 0 );
}
// no delay required, also Arduino Due is slow enough
//delayMicroseconds(1);
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 1 );
//delayMicroseconds(1);
i--;
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 0 );
//delayMicroseconds(1);
data <<= 1;
} while( i > 0 );
}
static int16_t ucg_com_arduino_3wire_9bit_SW_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 */
pinMode(ucg->pin_list[UCG_PIN_SDA], OUTPUT);
pinMode(ucg->pin_list[UCG_PIN_SCL], OUTPUT);
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
pinMode(ucg->pin_list[UCG_PIN_CS], OUTPUT);
if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
pinMode(ucg->pin_list[UCG_PIN_RST], OUTPUT);
digitalWrite(ucg->pin_list[UCG_PIN_SDA], 1);
digitalWrite(ucg->pin_list[UCG_PIN_SCL], 0);
if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
digitalWrite(ucg->pin_list[UCG_PIN_CS], 1);
if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
digitalWrite(ucg->pin_list[UCG_PIN_RST], 1);
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 )
digitalWrite(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 )
digitalWrite(ucg->pin_list[UCG_PIN_CS], arg);
break;
case UCG_COM_MSG_CHANGE_CD_LINE:
/* ignored, there is not CD line */
break;
case UCG_COM_MSG_SEND_BYTE:
ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg, ucg->com_status &UCG_COM_STATUS_MASK_CD, arg);
break;
case UCG_COM_MSG_REPEAT_1_BYTE:
while( arg > 0 ) {
ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg, ucg->com_status &UCG_COM_STATUS_MASK_CD, data[0]);
arg--;
}
break;
case UCG_COM_MSG_REPEAT_2_BYTES:
while( arg > 0 ) {
ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg, ucg->com_status &UCG_COM_STATUS_MASK_CD, data[0]);
ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg, ucg->com_status &UCG_COM_STATUS_MASK_CD, data[1]);
arg--;
}
break;
case UCG_COM_MSG_REPEAT_3_BYTES:
while( arg > 0 ) {
ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg, ucg->com_status &UCG_COM_STATUS_MASK_CD, data[0]);
ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg, ucg->com_status &UCG_COM_STATUS_MASK_CD, data[1]);
ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg, ucg->com_status &UCG_COM_STATUS_MASK_CD, data[2]);
arg--;
}
break;
case UCG_COM_MSG_SEND_STR:
while( arg > 0 ) {
ucg_com_arduino_send_3wire_9bit_SW_SPI(ucg, ucg->com_status &UCG_COM_STATUS_MASK_CD, *data++);
arg--;
}
break;
case UCG_COM_MSG_SEND_CD_DATA_SEQUENCE:
{
uint8_t last_cd = ucg->com_status &UCG_COM_STATUS_MASK_CD;
while(arg > 0)
{
if ( *data != 0 )
{
if ( *data == 1 )
{