Bundle SoftwareI2C library (with edits)

This commit is contained in:
ThePetrovich 2025-09-22 20:00:44 +08:00
parent 63583ce984
commit 70798b6bf7
13 changed files with 1390 additions and 0 deletions

View file

@ -0,0 +1,22 @@
# Auto detect text files and perform LF normalization
* text=auto
# Custom for Visual Studio
*.cs diff=csharp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union
# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain

View file

@ -0,0 +1,215 @@
#################
## Eclipse
#################
*.pydevproject
.project
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# PDT-specific
.buildpath
#################
## Visual Studio
#################
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.sln.docstates
# Build results
[Dd]ebug/
[Rr]elease/
x64/
build/
[Bb]in/
[Oo]bj/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.scc
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile
# Visual Studio profiler
*.psess
*.vsp
*.vspx
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
*.ncrunch*
.*crunch*.local.xml
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.Publish.xml
*.pubxml
# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/
# Windows Azure Build Output
csx
*.build.csdef
# Windows Store app package directory
AppPackages/
# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.publishsettings
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
App_Data/*.mdf
App_Data/*.ldf
#############
## Windows detritus
#############
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Mac crap
.DS_Store
#############
## Python
#############
*.py[co]
# Packages
*.egg
*.egg-info
dist/
build/
eggs/
parts/
var/
sdist/
develop-eggs/
.installed.cfg
# Installer logs
pip-log.txt
# Unit test / coverage reports
.coverage
.tox
#Translations
*.mo
#Mr Developer
.mr.developer.cfg

View file

@ -0,0 +1,8 @@
build:
tags:
- nas
script:
- wget -c https://files.seeedstudio.com/arduino/seeed-arduino-ci.sh
- chmod +x seeed-arduino-ci.sh
- bash $PWD/seeed-arduino-ci.sh test

View file

@ -0,0 +1,21 @@
language: generic
dist: bionic
sudo: false
cache:
directories:
- ~/arduino_ide
- ~/.arduino15/packages/
before_install:
- wget -c https://files.seeedstudio.com/arduino/seeed-arduino-ci.sh
script:
- chmod +x seeed-arduino-ci.sh
- cat $PWD/seeed-arduino-ci.sh
- bash $PWD/seeed-arduino-ci.sh test
notifications:
email:
on_success: change
on_failure: change

View file

@ -0,0 +1,30 @@
Arduino_SoftWareI2C [![Build Status](https://travis-ci.com/Seeed-Studio/Arduino_Software_I2C.svg?branch=master)](https://travis-ci.com/Seeed-Studio/Arduino_Software_I2C)
===============
This is an Arduino software I2C Library, you can use the other pin as I2C interface, such D2, D3.
But, you should know that this library can only act as Master.
And, Wire.available() is not correctly supported.
This library include:
void begin(int Sda, int Scl); // setup
uchar beginTransmission(uchar addr); // start
uchar endTransmission(); // end
uchar available(); // bytes available for reading
uchar write(uchar dta); // write a byte
uchar write(uchar len, uchar *dta); // write a array
uchar requestFrom(uchar addr, uchar len); // request some data
uchar read(); // read a byte
begin() is something different from <Wire.h>, you have to set pin here.
beginTransmission() return the uchar, original not.
available() return the length set by requestFrom().
The others function is just like the hard I2C library.

View file

@ -0,0 +1,493 @@
/*
SeeedGrayOLED.cpp
SSD1327 Gray OLED Driver Library
Copyright (c) 2011 seeed technology inc.
Author : Visweswara R
Create Time : Dec 2011
Change Log :
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Arduino.h"
#include <SoftwareI2C.h>
#include "SeeedGrayOLED.h"
#if defined(ESP32)
#include <pgmspace.h>
#else
#include <avr/pgmspace.h>
#endif
#if defined(__arm__) && !defined(PROGMEM)
#define PROGMEM
#define pgm_read_byte(STR) STR
#endif
// 8x8 Font ASCII 32 - 127 Implemented
// Users can modify this to support more characters(glyphs)
// BasicFont is placed in code memory.
// This font can be freely used without any restriction(It is placed in public domain)
const unsigned char BasicFont[][8] PROGMEM = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00},
{0x00, 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00, 0x00},
{0x00, 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00, 0x00},
{0x00, 0x23, 0x13, 0x08, 0x64, 0x62, 0x00, 0x00},
{0x00, 0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x00},
{0x00, 0x00, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x1C, 0x22, 0x41, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x41, 0x22, 0x1C, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x08, 0x2A, 0x1C, 0x2A, 0x08, 0x00, 0x00},
{0x00, 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, 0x00},
{0x00, 0xA0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00},
{0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x00, 0x00},
{0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00, 0x00},
{0x00, 0x00, 0x42, 0x7F, 0x40, 0x00, 0x00, 0x00},
{0x00, 0x62, 0x51, 0x49, 0x49, 0x46, 0x00, 0x00},
{0x00, 0x22, 0x41, 0x49, 0x49, 0x36, 0x00, 0x00},
{0x00, 0x18, 0x14, 0x12, 0x7F, 0x10, 0x00, 0x00},
{0x00, 0x27, 0x45, 0x45, 0x45, 0x39, 0x00, 0x00},
{0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00, 0x00},
{0x00, 0x01, 0x71, 0x09, 0x05, 0x03, 0x00, 0x00},
{0x00, 0x36, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00},
{0x00, 0x06, 0x49, 0x49, 0x29, 0x1E, 0x00, 0x00},
{0x00, 0x00, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0xAC, 0x6C, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00, 0x00},
{0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00, 0x00},
{0x00, 0x41, 0x22, 0x14, 0x08, 0x00, 0x00, 0x00},
{0x00, 0x02, 0x01, 0x51, 0x09, 0x06, 0x00, 0x00},
{0x00, 0x32, 0x49, 0x79, 0x41, 0x3E, 0x00, 0x00},
{0x00, 0x7E, 0x09, 0x09, 0x09, 0x7E, 0x00, 0x00},
{0x00, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00},
{0x00, 0x3E, 0x41, 0x41, 0x41, 0x22, 0x00, 0x00},
{0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C, 0x00, 0x00},
{0x00, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x00, 0x00},
{0x00, 0x7F, 0x09, 0x09, 0x09, 0x01, 0x00, 0x00},
{0x00, 0x3E, 0x41, 0x41, 0x51, 0x72, 0x00, 0x00},
{0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00, 0x00},
{0x00, 0x41, 0x7F, 0x41, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x20, 0x40, 0x41, 0x3F, 0x01, 0x00, 0x00},
{0x00, 0x7F, 0x08, 0x14, 0x22, 0x41, 0x00, 0x00},
{0x00, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00},
{0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F, 0x00, 0x00},
{0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00, 0x00},
{0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00, 0x00},
{0x00, 0x7F, 0x09, 0x09, 0x09, 0x06, 0x00, 0x00},
{0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00, 0x00},
{0x00, 0x7F, 0x09, 0x19, 0x29, 0x46, 0x00, 0x00},
{0x00, 0x26, 0x49, 0x49, 0x49, 0x32, 0x00, 0x00},
{0x00, 0x01, 0x01, 0x7F, 0x01, 0x01, 0x00, 0x00},
{0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00, 0x00},
{0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00, 0x00},
{0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00, 0x00},
{0x00, 0x63, 0x14, 0x08, 0x14, 0x63, 0x00, 0x00},
{0x00, 0x03, 0x04, 0x78, 0x04, 0x03, 0x00, 0x00},
{0x00, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00, 0x00},
{0x00, 0x7F, 0x41, 0x41, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, 0x00},
{0x00, 0x41, 0x41, 0x7F, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x04, 0x02, 0x01, 0x02, 0x04, 0x00, 0x00},
{0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00},
{0x00, 0x01, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x20, 0x54, 0x54, 0x54, 0x78, 0x00, 0x00},
{0x00, 0x7F, 0x48, 0x44, 0x44, 0x38, 0x00, 0x00},
{0x00, 0x38, 0x44, 0x44, 0x28, 0x00, 0x00, 0x00},
{0x00, 0x38, 0x44, 0x44, 0x48, 0x7F, 0x00, 0x00},
{0x00, 0x38, 0x54, 0x54, 0x54, 0x18, 0x00, 0x00},
{0x00, 0x08, 0x7E, 0x09, 0x02, 0x00, 0x00, 0x00},
{0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C, 0x00, 0x00},
{0x00, 0x7F, 0x08, 0x04, 0x04, 0x78, 0x00, 0x00},
{0x00, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x80, 0x84, 0x7D, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x7F, 0x10, 0x28, 0x44, 0x00, 0x00, 0x00},
{0x00, 0x41, 0x7F, 0x40, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x7C, 0x04, 0x18, 0x04, 0x78, 0x00, 0x00},
{0x00, 0x7C, 0x08, 0x04, 0x7C, 0x00, 0x00, 0x00},
{0x00, 0x38, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00},
{0x00, 0xFC, 0x24, 0x24, 0x18, 0x00, 0x00, 0x00},
{0x00, 0x18, 0x24, 0x24, 0xFC, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x7C, 0x08, 0x04, 0x00, 0x00, 0x00},
{0x00, 0x48, 0x54, 0x54, 0x24, 0x00, 0x00, 0x00},
{0x00, 0x04, 0x7F, 0x44, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x3C, 0x40, 0x40, 0x7C, 0x00, 0x00, 0x00},
{0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00, 0x00},
{0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00, 0x00},
{0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00, 0x00},
{0x00, 0x1C, 0xA0, 0xA0, 0x7C, 0x00, 0x00, 0x00},
{0x00, 0x44, 0x64, 0x54, 0x4C, 0x44, 0x00, 0x00},
{0x00, 0x08, 0x36, 0x41, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x41, 0x36, 0x08, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x00},
{0x00, 0x02, 0x05, 0x05, 0x02, 0x00, 0x00, 0x00}
};
void SeeedGrayOLED::init(int IC) {
Drive_IC = IC;
if (Drive_IC == SSD1327) {
sendCommand(0xFD); // Unlock OLED driver IC MCU interface from entering command. i.e: Accept commands
sendCommand(0x12);
sendCommand(0xAE); // Set display off
sendCommand(0xA8); // set multiplex ratio
sendCommand(0x5F); // 96
sendCommand(0xA1); // set display start line
sendCommand(0x00);
sendCommand(0xA2); // set display offset
sendCommand(0x60);
sendCommand(0xA0); // set remap
sendCommand(0x46);
sendCommand(0xAB); // set vdd internal
sendCommand(0x01); //
sendCommand(0x81); // set contrasr
sendCommand(0x53); // 100 nit
sendCommand(0xB1); // Set Phase Length
sendCommand(0X51); //
sendCommand(0xB3); // Set Display Clock Divide Ratio/Oscillator Frequency
sendCommand(0x01);
sendCommand(0xB9); //
sendCommand(0xBC); // set pre_charge voltage/VCOMH
sendCommand(0x08); // (0x08);
sendCommand(0xBE); // set VCOMH
sendCommand(0X07); // (0x07);
sendCommand(0xB6); // Set second pre-charge period
sendCommand(0x01); //
sendCommand(0xD5); // enable second precharge and enternal vsl
sendCommand(0X62); // (0x62);
sendCommand(0xA4); // Set Normal Display Mode
sendCommand(0x2E); // Deactivate Scroll
sendCommand(0xAF); // Switch on display
delay(100);
// Row Address
sendCommand(0x75); // Set Row Address
sendCommand(0x00); // Start 0
sendCommand(0x5f); // End 95
// Column Address
sendCommand(0x15); // Set Column Address
sendCommand(0x08); // Start from 8th Column of driver IC. This is 0th Column for OLED
sendCommand(0x37); // End at (8 + 47)th column. Each Column has 2 pixels(segments)
// Init gray level for text. Default:Brightest White
grayH = 0xF0;
grayL = 0x0F;
} else if (Drive_IC == SH1107G) {
sendCommand(0xae); //Display OFF
sendCommand(0xd5); // Set Dclk
sendCommand(0x50); // 100Hz
sendCommand(0x20); // Set row address
sendCommand(0x81); // Set contrast control
sendCommand(0x80);
sendCommand(0xa0); // Segment remap
sendCommand(0xa4); // Set Entire Display ON
sendCommand(0xa6); // Normal display
sendCommand(0xad); // Set external VCC
sendCommand(0x80);
sendCommand(0xc0); // Set Common scan direction
sendCommand(0xd9); // Set phase leghth
sendCommand(0x1f);
sendCommand(0xdb); // Set Vcomh voltage
sendCommand(0x27);
sendCommand(0xaf); //Display ON
sendCommand(0xb0);
sendCommand(0x00);
sendCommand(0x11);
}
}
void SeeedGrayOLED::sendCommand(unsigned char command) {
Wire->beginTransmission(SeeedGrayOLED_Address); // begin I2C communication
Wire->write(SeeedGrayOLED_Command_Mode); // Set OLED Command mode
Wire->write(command);
Wire->endTransmission(); // End I2C communication
}
void SeeedGrayOLED::setContrastLevel(unsigned char ContrastLevel) {
sendCommand(SeeedGrayOLED_Set_ContrastLevel_Cmd);
sendCommand(ContrastLevel);
}
void SeeedGrayOLED::setHorizontalMode() {
if (Drive_IC == SSD1327) {
sendCommand(0xA0); // remap to
sendCommand(0x42); // horizontal mode
// Row Address
sendCommand(0x75); // Set Row Address
sendCommand(0x00); // Start 0
sendCommand(0x5f); // End 95
// Column Address
sendCommand(0x15); // Set Column Address
sendCommand(0x08); // Start from 8th Column of driver IC. This is 0th Column for OLED
sendCommand(0x37); // End at (8 + 47)th column. Each Column has 2 pixels(or segments)
} else if (Drive_IC == SH1107G) {
sendCommand(0xA0);
sendCommand(0xC8);
}
}
void SeeedGrayOLED::setVerticalMode() {
if (Drive_IC == SSD1327) {
sendCommand(0xA0); // remap to
sendCommand(0x46); // Vertical mode
} else if (Drive_IC == SH1107G) {
sendCommand(0xA0);
sendCommand(0xC0);
}
}
void SeeedGrayOLED::setTextXY(unsigned char Row, unsigned char Column) {
if (Drive_IC == SSD1327) {
//Column Address
sendCommand(0x15); /* Set Column Address */
sendCommand(0x08 + (Column * 4)); /* Start Column: Start from 8 */
sendCommand(0x37); /* End Column */
// Row Address
sendCommand(0x75); /* Set Row Address */
sendCommand(0x00 + (Row * 8)); /* Start Row*/
sendCommand(0x07 + (Row * 8)); /* End Row*/
} else if (Drive_IC == SH1107G) {
uint8_t col_l;
Column % 2 ? col_l = 0x08 : col_l = 0x00;
sendCommand(0xb0 + Row);
sendCommand(col_l);
sendCommand(0x11 + (Column / 2));
}
}
void SeeedGrayOLED::clearDisplay() {
unsigned char i, j;
if (Drive_IC == SSD1327) {
for (j = 0; j < 48; j++) {
for (i = 0; i < 96; i++) { //clear all columns
sendData(0x00);
}
}
} else if (Drive_IC == SH1107G) {
for (i = 0; i < 16; i++) {
sendCommand(0xb0 + i);
sendCommand(0x0);
sendCommand(0x10);
for (j = 0; j < 128; j++) {
sendData(0x00);
}
}
}
}
void SeeedGrayOLED::sendData(unsigned char Data) {
Wire->beginTransmission(SeeedGrayOLED_Address); // begin I2C transmission
Wire->write(SeeedGrayOLED_Data_Mode); // data mode
Wire->write(Data);
Wire->endTransmission(); // stop I2C transmission
}
void SeeedGrayOLED::setGrayLevel(unsigned char grayLevel) {
grayH = (grayLevel << 4) & 0xF0;
grayL = grayLevel & 0x0F;
}
void SeeedGrayOLED::putChar(unsigned char C) {
if (C < 32 || C > 127) { //Ignore non-printable ASCII characters. This can be modified for multilingual font.
C = ' '; //Space
}
if (Drive_IC == SSD1327) {
for (char i = 0; i < 8; i = i + 2) {
for (char j = 0; j < 8; j++) {
// Character is constructed two pixel at a time using vertical mode from the default 8x8 font
char c = 0x00;
char bit1 = (pgm_read_byte(&BasicFont[C - 32][(uint8_t) i]) >> j) & 0x01;
char bit2 = (pgm_read_byte(&BasicFont[C - 32][(uint8_t) i + 1]) >> j) & 0x01;
// Each bit is changed to a nibble
c |= (bit1) ? grayH : 0x00;
c |= (bit2) ? grayL : 0x00;
sendData(c);
}
}
} else if (Drive_IC == SH1107G) {
for (int i = 0; i < 8; i++) {
//read bytes from code memory
sendData(pgm_read_byte(&BasicFont[C - 32][i])); //font array starts at 0, ASCII starts at 32. Hence the translation
}
}
}
void SeeedGrayOLED::putString(const char* String) {
unsigned char i = 0;
while (String[i]) {
putChar(String[i]);
i++;
}
}
unsigned char SeeedGrayOLED::putNumber(long long_num) {
unsigned char char_buffer[10] = "";
unsigned char i = 0;
unsigned char f = 0;
if (long_num < 0) {
f = 1;
putChar('-');
long_num = -long_num;
} else if (long_num == 0) {
f = 1;
putChar('0');
return f;
}
while (long_num > 0) {
char_buffer[i++] = long_num % 10;
long_num /= 10;
}
f = f + i;
for (; i > 0; i--) {
putChar('0' + char_buffer[i - 1]);
}
return f;
}
void SeeedGrayOLED::drawBitmap(const unsigned char* bitmaparray, int bytes) {
if (Drive_IC == SSD1327) {
char localAddressMode = addressingMode;
if (addressingMode != HORIZONTAL_MODE) {
//Bitmap is drawn in horizontal mode
setHorizontalMode();
}
for (int i = 0; i < bytes; i++) {
for (int j = 0; j < 8; j = j + 2) {
char c = 0x00;
char bit1 = pgm_read_byte(&bitmaparray[i]) << j & 0x80;
char bit2 = pgm_read_byte(&bitmaparray[i]) << (j + 1) & 0x80;
// Each bit is changed to a nibble
c |= (bit1) ? grayH : 0x00;
// Each bit is changed to a nibble
c |= (bit2) ? grayL : 0x00;
sendData(c);
}
}
if (localAddressMode == VERTICAL_MODE) {
//If Vertical Mode was used earlier, restore it.
setVerticalMode();
}
} else if (Drive_IC == SH1107G) {
int Row = 0, column_l = 0x00, column_h = 0x11;
setHorizontalMode();
for (int i = 0; i < bytes; i++) {
sendCommand(0xb0 + Row);
sendCommand(column_l);
sendCommand(column_h);
byte bits = (byte)pgm_read_byte(&bitmaparray[i]);
byte tmp = 0x00;
for (int b = 0; b < 8; b++) {
tmp |= ((bits >> (7 - b)) & 0x01) << b;
}
sendData(tmp);
Row++;
if (Row >= 12) {
Row = 0;
column_l++;
if (column_l >= 16) {
column_l = 0x00;
column_h += 0x01;
}
}
}
}
}
void SeeedGrayOLED::setHorizontalScrollProperties(bool direction, unsigned char startRow, unsigned char endRow,
unsigned char startColumn, unsigned char endColumn, unsigned char scrollSpeed) {
/*
Use the following defines for 'direction' :
Scroll_Left
Scroll_Right
Use the following defines for 'scrollSpeed' :
Scroll_2Frames
Scroll_3Frames
Scroll_4Frames
Scroll_5Frames
Scroll_25Frames
Scroll_64Frames
Scroll_128Frames
Scroll_256Frames
*/
if (Scroll_Right == direction) {
//Scroll Right
sendCommand(0x27);
} else {
//Scroll Left
sendCommand(0x26);
}
sendCommand(0x00); //Dummmy byte
sendCommand(startRow);
sendCommand(scrollSpeed);
sendCommand(endRow);
sendCommand(startColumn + 8);
sendCommand(endColumn + 8);
sendCommand(0x00); //Dummmy byte
}
void SeeedGrayOLED::activateScroll() {
sendCommand(SeeedGrayOLED_Activate_Scroll_Cmd);
}
void SeeedGrayOLED::deactivateScroll() {
sendCommand(SeeedGrayOLED_Dectivate_Scroll_Cmd);
}
void SeeedGrayOLED::setNormalDisplay() {
sendCommand(SeeedGrayOLED_Normal_Display_Cmd);
}
void SeeedGrayOLED::setInverseDisplay() {
sendCommand(SeeedGrayOLED_Inverse_Display_Cmd);
}
void SeeedGrayOLED::initSoftwareI2C(SoftwareI2C* w, int __sda, int __scl) {
Wire = w;
Wire->begin(__sda, __scl);
}
SeeedGrayOLED SeeedGrayOled; // Preinstantiate Objects

View file

@ -0,0 +1,118 @@
/*
SeeedGrayOLED.cpp
SSD1327 Gray OLED Driver Library
Copyright (c) 2011 seeed technology inc.
Author : Visweswara R
Create Time : Dec 2011
Change Log :
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SeeedGrayOLED_data_H
#define SeeedGrayOLED_data_H
// SeeedGrayOLED Instruction set addresses
#include "Arduino.h"
#include <SoftwareI2C.h>
#define SH1107G 1
#define SSD1327 2
#define VERTICAL_MODE 01
#define HORIZONTAL_MODE 02
#define SeeedGrayOLED_Address 0x3c
#define SeeedGrayOLED_Command_Mode 0x80
#define SeeedGrayOLED_Data_Mode 0x40
#define SeeedGrayOLED_Display_Off_Cmd 0xAE
#define SeeedGrayOLED_Display_On_Cmd 0xAF
#define SeeedGrayOLED_Normal_Display_Cmd 0xA4
#define SeeedGrayOLED_Inverse_Display_Cmd 0xA7
#define SeeedGrayOLED_Activate_Scroll_Cmd 0x2F
#define SeeedGrayOLED_Dectivate_Scroll_Cmd 0x2E
#define SeeedGrayOLED_Set_ContrastLevel_Cmd 0x81
#define Scroll_Left 0x00
#define Scroll_Right 0x01
#define Scroll_2Frames 0x7
#define Scroll_3Frames 0x4
#define Scroll_4Frames 0x5
#define Scroll_5Frames 0x0
#define Scroll_25Frames 0x6
#define Scroll_64Frames 0x1
#define Scroll_128Frames 0x2
#define Scroll_256Frames 0x3
class SeeedGrayOLED {
public:
char addressingMode;
void init(int IC);
void setNormalDisplay();
void setInverseDisplay();
void sendCommand(unsigned char command);
void sendData(unsigned char Data);
void setGrayLevel(unsigned char grayLevel);
void setVerticalMode();
void setHorizontalMode();
void setTextXY(unsigned char Row, unsigned char Column);
void clearDisplay();
void setContrastLevel(unsigned char ContrastLevel);
void putChar(unsigned char c);
void putString(const char* String);
unsigned char putNumber(long n);
unsigned char putFloat(float floatNumber, unsigned char decimal);
unsigned char putFloat(float floatNumber);
void drawBitmap(const unsigned char* bitmaparray, int bytes);
void setHorizontalScrollProperties(bool direction, unsigned char startRow, unsigned char endRow,
unsigned char startColumn, unsigned char endColumn, unsigned char scrollSpeed);
void activateScroll();
void deactivateScroll();
void initSoftwareI2C(SoftwareI2C* w, int __sda, int __scl);
private:
unsigned char grayH;
unsigned char grayL;
int Drive_IC;
SoftwareI2C* Wire;
};
extern SeeedGrayOLED SeeedGrayOled; // SeeedGrayOLED object
#endif

View file

@ -0,0 +1,256 @@
/*
SoftwareI2C.cpp
2012 Copyright (c) Seeed Technology Inc. All right reserved.
Author:Loovee
Author:Loovee
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
#include "SoftwareI2C.h"
/*************************************************************************************************
Function Name: begin
Description: config IO
Parameters: Sda: Scl:
Return: none
*************************************************************************************************/
void SoftwareI2C::begin(int Sda, int Scl) {
pinSda = Sda;
pinScl = Scl;
pinMode(pinScl, OUTPUT);
pinMode(pinSda, OUTPUT);
sda_in_out = OUTPUT;
digitalWrite(pinScl, HIGH);
digitalWrite(pinSda, HIGH);
}
/*************************************************************************************************
Function Name: sdaSet
Description: set sda
Parameters: ucDta: HIGH or LOW
Return: none
*************************************************************************************************/
void SoftwareI2C::sdaSet(uchar ucDta) {
if (sda_in_out != OUTPUT) {
sda_in_out = OUTPUT;
pinMode(pinSda, OUTPUT);
}
digitalWrite(pinSda, ucDta);
}
/*************************************************************************************************
Function Name: sclSet
Description: set scl
Parameters: ucDta: HIGH or LOW
Return: none
*************************************************************************************************/
void SoftwareI2C::sclSet(uchar ucDta) {
digitalWrite(pinScl, ucDta);
}
/*************************************************************************************************
Function Name: getAck
Description: get ack
Parameters: None
Return: 0 Nak; 1 Ack
*************************************************************************************************/
uchar SoftwareI2C::getAck(void) {
sclSet(LOW);
pinMode(pinSda, INPUT);
sda_in_out = INPUT;
sclSet(HIGH);
unsigned long timer_t = micros();
while (1) {
if (!digitalRead(pinSda)) { // get ack
return GETACK;
}
if (micros() - timer_t > 100) {
return GETNAK;
}
}
}
/*************************************************************************************************
Function Name: sendStart
Description: send start clock
Parameters: None
Return: None
*************************************************************************************************/
void SoftwareI2C::sendStart(void) {
sdaSet(LOW);
}
/*************************************************************************************************
Function Name: setClockDivider
Description: setup clock divider for spi bus
Parameters: divider clock divider
Return: 0 setup ok; 1 setup failed
*************************************************************************************************/
void SoftwareI2C::sendStop(void) {
sclSet(LOW);
sdaSet(LOW);
sclSet(HIGH);
sdaSet(HIGH);
}
/*************************************************************************************************
Function Name: sendByte
Description: send a byte
Parameters: ucDta: data to send
Return: None
*************************************************************************************************/
void SoftwareI2C::sendByte(uchar ucDta) {
for (int i = 0; i < 8; i++) {
sclSet(LOW);
sdaSet((ucDta & 0x80) != 0);
ucDta <<= 0;
sclSet(HIGH);
sdaSet((ucDta & 0x80) != 0);
ucDta <<= 1;
}
}
/*************************************************************************************************
Function Name: sendByteAck
Description: send a byte and get ack signal
Parameters: ucDta: data to send
Return: 0: get nak 1: get ack
*************************************************************************************************/
uchar SoftwareI2C::sendByteAck(uchar ucDta) {
sendByte(ucDta);
return getAck();
}
/*************************************************************************************************
Function Name: beginTransmission
Description: send begin signal
Parameters: divider clock divider
Return: 0: get nak 1: get ack
*************************************************************************************************/
uchar SoftwareI2C::beginTransmission(uchar addr) {
sendStart(); // start signal
uchar ret = sendByteAck(addr << 1); // send write address and get ack
//sclSet(LOW);
return ret;
}
/*************************************************************************************************
Function Name: endTransmission
Description: send stop signal
Parameters: None
Return: None
*************************************************************************************************/
uchar SoftwareI2C::endTransmission() {
sendStop();
return 0;
}
/*************************************************************************************************
Function Name: write
Description: send a byte
Parameters: dta: data to send
Return: 0: get nak 1: get ack
*************************************************************************************************/
uchar SoftwareI2C::write(uchar dta) {
return sendByteAck(dta);
}
/*************************************************************************************************
Function Name: write
Description: write array
Parameters: len - length of the array
dta - array to be sent
Return: 0: get nak 1: get ack
*************************************************************************************************/
uchar SoftwareI2C::write(uchar len, uchar* dta) {
for (int i = 0; i < len; i++) {
if (GETACK != write(dta[i])) {
return GETNAK;
}
}
return GETACK;
}
/*************************************************************************************************
Function Name: requestFrom
Description: request data from slave
Parameters: addr - address of slave
len - length of request
Return: 0: get nak 1: get ack
*************************************************************************************************/
uchar SoftwareI2C::requestFrom(uchar addr, uchar len) {
sendStart(); // start signal
recv_len = len;
uchar ret = sendByteAck((addr << 1) + 1); // send write address and get ack
//sclSet(LOW);
return ret;
}
/*************************************************************************************************
Function Name: read
Description: read a byte from i2c
Parameters: None
Return: data get
*************************************************************************************************/
uchar SoftwareI2C::read() {
if (!recv_len) {
return 0;
}
uchar ucRt = 0;
pinMode(pinSda, INPUT);
sda_in_out = INPUT;
for (int i = 0; i < 8; i++) {
unsigned char ucBit;
sclSet(LOW);
sclSet(HIGH);
ucBit = digitalRead(pinSda);
ucRt = (ucRt << 1) + ucBit;
}
uchar dta = ucRt;
recv_len--;
if (recv_len > 0) { // send ACK
sclSet(LOW); // sclSet(HIGH)
sdaSet(LOW); // sdaSet(LOW)
sclSet(HIGH); // sclSet(LOW)
sclSet(LOW);
} else { // send NAK
sclSet(LOW); // sclSet(HIGH)
sdaSet(HIGH); // sdaSet(LOW)
sclSet(HIGH); // sclSet(LOW)
sclSet(LOW);
sendStop();
}
return dta;
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/

View file

@ -0,0 +1,83 @@
/*
SoftwareI2C.h
2012 Copyright (c) Seeed Technology Inc. All right reserved.
Author:Loovee
2013-11-1
This is a Software I2C Library, can act as I2c master mode.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _SOFTWAREI2C_H_
#define _SOFTWAREI2C_H_
#define GETACK 1 // get ack
#define GETNAK 0 // get nak
#ifndef HIGH
#define HIGH 1
#endif
#ifndef LOW
#define LOW 0
#endif
#ifndef uchar
#define uchar unsigned char
#endif
class SoftwareI2C {
private:
int pinSda;
int pinScl;
int recv_len;
int sda_in_out;
private:
inline void sdaSet(uchar ucDta);
inline void sclSet(uchar ucDta);
inline void sendStart(void);
inline void sendStop(void);
inline uchar getAck(void);
inline void sendByte(uchar ucDta);
inline uchar sendByteAck(uchar ucDta); // send byte and get ack
public:
//SoftwareI2C();
void begin(int Sda, int Scl);
uchar beginTransmission(uchar addr);
uchar endTransmission();
uchar write(uchar dta);
uchar write(uchar len, uchar* dta);
uchar requestFrom(uchar addr, uchar len);
uchar read();
uchar available() {
return recv_len;
}
};
#endif
/*********************************************************************************************************
END FILE
*********************************************************************************************************/

View file

@ -0,0 +1,50 @@
#include "SoftwareI2C.h"
SoftwareI2C WireS1;
SoftwareI2C WireS2;
#include "SeeedGrayOLED.h"
#include <avr/pgmspace.h>
SeeedGrayOLED SeeedGrayOled1;
SeeedGrayOLED SeeedGrayOled2;
void setup() {
SeeedGrayOled1.initSoftwareI2C(&WireS1, 3, 2); // wire, sda, scl
SeeedGrayOled1.init(SSD1327);
SeeedGrayOled1.clearDisplay(); //Clear Display.
SeeedGrayOled1.setNormalDisplay(); //Set Normal Display Mode
SeeedGrayOled1.setVerticalMode(); // Set to vertical mode for displaying text
for (char i = 0; i < 12 ; i++) {
SeeedGrayOled1.setTextXY(i, 0); //set Cursor to ith line, 0th column
SeeedGrayOled1.setGrayLevel(i); //Set Grayscale level. Any number between 0 - 15.
SeeedGrayOled1.putString("11111111"); //Print Hello World
}
SeeedGrayOled2.initSoftwareI2C(&WireS2, 5, 4); // wire, sda, scl
SeeedGrayOled2.init(SSD1327); //initialize SEEED OLED display
SeeedGrayOled2.clearDisplay(); //Clear Display.
SeeedGrayOled2.setNormalDisplay(); //Set Normal Display Mode
SeeedGrayOled2.setVerticalMode(); // Set to vertical mode for displaying text
for (char i = 0; i < 12 ; i++) {
SeeedGrayOled2.setTextXY(i, 0); //set Cursor to ith line, 0th column
SeeedGrayOled2.setGrayLevel(i); //Set Grayscale level. Any number between 0 - 15.
SeeedGrayOled2.putString("00000000"); //Print Hello World
}
}
void loop() {
}

View file

@ -0,0 +1,26 @@
// i2c scan
#include "SoftwareI2C.h"
SoftwareI2C softwarei2c;
void setup() {
Serial.begin(115200);
softwarei2c.begin(3, 2); // sda, scl
Serial.println("begin to scan...");
}
void loop() {
for (unsigned char i = 1; i <= 127; i++) {
if (softwarei2c.beginTransmission(i)) {
Serial.print("0x");
Serial.println(i, HEX);
while (1);
}
softwarei2c.endTransmission();
}
Serial.println("find nothing");
while (1);
}

View file

@ -0,0 +1,59 @@
#######################################
# Syntax Coloring Map For Seeed OLED
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
addressingMode KEYWORD1
SeeedGrayOled KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
init KEYWORD1
setNormalDisplay KEYWORD1
setInverseDisplay KEYWORD1
sendCommand KEYWORD1
setGrayLevel KEYWORD1
setVerticalMode KEYWORD1
setHorizontalMode KEYWORD1
setGrayLevel KEYWORD1
setTextXY KEYWORD1
clearDisplay KEYWORD1
setContrastLevel KEYWORD1
putChar KEYWORD1
putString KEYWORD1
putNumber KEYWORD1
drawBitmap KEYWORD1
setHorizontalScrollProperties KEYWORD1
activateScroll KEYWORD1
deactivateScroll KEYWORD1
#######################################
# Constants (LITERAL1)
#######################################
Scroll_Left LITERAL1
Scroll_Right LITERAL1
Scroll_2Frames LITERAL1
Scroll_3Frames LITERAL1
Scroll_4Frames LITERAL1
Scroll_5Frames LITERAL1
Scroll_25Frames LITERAL1
Scroll_64Frames LITERAL1
Scroll_128Frames LITERAL1
Scroll_256Frames LITERAL1
#######################################
# Instances (KEYWORD2)
#######################################
SeeedGrayOLED KEYWORD2

View file

@ -0,0 +1,9 @@
name=Arduino Software I2C
version=1.0.0
author=Seeed Studio
maintainer=Seeed Studio <techsupport@seeed.cc>
sentence=Arduino library to use Software I2C.
paragraph=Arduino library to use Software I2C.
category=Communication
url=https://github.com/Seeed-Studio/Arduino_Software_I2C
architectures=*