Skip to content

Commit

Permalink
Version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruemelbahn committed Nov 28, 2023
1 parent c3376f3 commit 87056f6
Show file tree
Hide file tree
Showing 16 changed files with 1,984 additions and 0 deletions.
91 changes: 91 additions & 0 deletions BounceSimplepcf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Please read BounceSimplepcf.h for information about the licence and authors

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include "BounceSimplepcf.h"

extern "C" {
#include "utility\i2c.h"
}

#define DEBOUNCED_STATE 0
#define UNSTABLE_STATE 1
#define STATE_CHANGED 3


BounceSimplePcf::BounceSimplePcf()
: previous_millis(0)
, interval_millis(10)
, state(0)
, pin(0)
, pcfAddress(0)
{}

void BounceSimplePcf::attach(uint8_t pcfAddress, int pin, uint16_t interval_millis) {
this->pin = pin;
this->pcfAddress = pcfAddress;
this->interval_millis = interval_millis;

state = readPins(pin) ? _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE) : 0;
previous_millis = millis();
}

void BounceSimplePcf::interval(uint16_t interval_millis)
{
this->interval_millis = interval_millis;
}

bool BounceSimplePcf::update()
{
// Read the state of the switch in a temporary variable.
bool currentState = readPins(pin);
state &= ~_BV(STATE_CHANGED);

// If the reading is different from last reading, reset the debounce counter
if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) {
previous_millis = millis();
state ^= _BV(UNSTABLE_STATE);
} else
if ( millis() - previous_millis >= interval_millis ) {
// We have passed the threshold time, so the input is now stable
// If it is different from last state, set the STATE_CHANGED flag
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
previous_millis = millis();
state ^= _BV(DEBOUNCED_STATE);
state |= _BV(STATE_CHANGED);
}
}

return state & _BV(STATE_CHANGED);
}

bool BounceSimplePcf::read()
{
return state & _BV(DEBOUNCED_STATE);
}

bool BounceSimplePcf::rose()
{
return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
}

bool BounceSimplePcf::fell()
{
return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
}

bool BounceSimplePcf::readPins(uint8_t pin)
{
register uint8_t iRetValue(0xff);
if(pcfAddress)
{
i2c_start(pcfAddress + 1);
iRetValue = i2c_readNAck();
i2c_stop();
}
return (((~iRetValue) >> pin) & 0x01 ? true : false); // since buttons are switch to GND, we invert the state
}
68 changes: 68 additions & 0 deletions BounceSimplepcf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/***************************************************
* BounceSimplePCF.h v1.0 - Debouncing for PCF8574
*
* Copyright (c) 2018 Michael Zimmermann <http://www.kruemelsoft.privat.t-online.de>
* All rights reserved.
*
* Main code taken from Bounce2Mcp by Thomas O Fredericks (tof@t-o-f.info)
* Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
*
* LICENSE
* -------
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
****************************************************/

#ifndef _KS_BounceSimple_h
#define _KS_BounceSimple_h

#include <inttypes.h>

class BounceSimplePcf
{
public:
// Create an instance of the bounce library
BounceSimplePcf();

// Attach to a PCF object, a pin (and also sets initial state), and set debounce interval.
void attach(uint8_t pcfAddress, int pin, uint16_t interval_millis);

// Sets the debounce interval
void interval(uint16_t interval_millis);

// Updates the pin
// Returns 1 if the state changed
// Returns 0 if the state did not change
bool update();

// Returns the updated pin state
bool read();

// Returns the falling pin state
bool fell();

// Returns the rising pin state
bool rose();

protected:
bool readPins(uint8_t pin);

unsigned long previous_millis;
uint16_t interval_millis;
uint8_t state;
uint8_t pin;
uint8_t pcfAddress;
};

#endif
60 changes: 60 additions & 0 deletions Examples/HelloWorld/HelloWorld.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*********************
Example code for the OLEDPanel Library
This code displays text on the shield, and also reads the buttons on the keypad.
**********************/

#define PCF8574_ADDR 0x21 << 1

// include the library code:
#include <OLEDPanel.h>
OLEDPanel oled = OLEDPanel();

void setup() {
// Debugging output
Serial.begin(57600);

if(oled.detect_i2c(PCF8574_ADDR) != 0)
Serial.println("OLED-Panel missing...");

// init pcf8574:
oled.begin();

// Print a message to the OLED. We track how long it takes since
oled.setCursor(0,0);
int time = millis();
oled.print("Hello, world!");
time = millis() - time;
Serial.print("Took "); Serial.print(time); Serial.println(" ms");
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
oled.setCursor(0, 1);
// print the number of seconds since reset:
oled.print(millis()/1000);

uint8_t buttons = oled.readButtons();

if (buttons) {
oled.setCursor(0,2);
if (buttons & BUTTON_UP) {
oled.print("UP ");
}
if (buttons & BUTTON_DOWN) {
oled.print("DOWN ");
}
if (buttons & BUTTON_LEFT) {
oled.print("LEFT ");
}
if (buttons & BUTTON_RIGHT) {
oled.print("RIGHT ");
}
if (buttons & BUTTON_SELECT) {
oled.print("SELECT");
}
}
}
Binary file added Hardware/OLED-Panel.pdf
Binary file not shown.
Loading

0 comments on commit 87056f6

Please sign in to comment.