Skip to content

Commit 0d73852

Browse files
authored
Merge pull request #3 from amperka/feat-source-files
Add source files and examples
2 parents cda554f + 22c2b1e commit 0d73852

File tree

8 files changed

+292
-0
lines changed

8 files changed

+292
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Two libraries have to be included
2+
// to work with the module. Download at
3+
// https://amperka.com/my/octofet
4+
#include <Octofet.h>
5+
#include <SPI.h>
6+
7+
// any GPIO pin can be used as pinCS
8+
constexpr auto pinCS = 10;
9+
// the number of Octofet boards connected in a daisy-chain
10+
constexpr auto deviceCount = 3;
11+
12+
// Create an object of Octofet type
13+
// to communicate with Octofet board via hardware SPI
14+
Octofet octofet(pinCS, deviceCount);
15+
16+
// If software SPI needed
17+
// add MOSI and SCK pins numbers, like this:
18+
// constexpr auto pinMOSI = 7;
19+
// constexpr auto pinSCK = 5;
20+
// any GPIO pins can be used as pinMOSI and pinSCK
21+
// Octofet octofet(pinCS, pinMOSI, pinSCK, deviceCount);
22+
23+
void setup() {
24+
// Start the board for communication
25+
octofet.begin();
26+
}
27+
28+
void loop() {
29+
// Set the "on" state for each switch of each device one by one
30+
for (int t = 0; t < deviceCount; t++) {
31+
for (int i = 0; i < 8; i++) {
32+
octofet.digitalWrite(i, HIGH, t);
33+
delay(1000);
34+
}
35+
}
36+
// Similarly, set switches to "off" state
37+
for (int t = 0; t < deviceCount; t++) {
38+
for (int i = 0; i < 8; i++) {
39+
octofet.digitalWrite(i, LOW, t);
40+
delay(1000);
41+
}
42+
}
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Two libraries have to be included
2+
// to work with the module. Download at
3+
// https://amperka.com/my/octofet
4+
#include <Octofet.h>
5+
#include <SPI.h>
6+
7+
// any GPIO pin can be used as pinCS
8+
constexpr auto pinCS = 10;
9+
10+
// Create an object of Octofet type
11+
// to communicate with Octofet board via hardware SPI
12+
Octofet octofet(pinCS);
13+
14+
// If software SPI needed
15+
// add MOSI and SCK pins numbers, like this:
16+
17+
// constexpr auto pinMOSI = 7;
18+
// constexpr auto pinSCK = 5;
19+
// any GPIO pins can be used as pinMOSI and pinSCK
20+
// Octofet octofet(pinCS, pinMOSI, pinSCK);
21+
22+
void setup() {
23+
// Start the board for communication
24+
octofet.begin();
25+
}
26+
27+
void loop() {
28+
// Set each power switch to "on" state
29+
// one by one with 1 second interval
30+
for (int i = 0; i < 8; i++) {
31+
octofet.digitalWrite(i, HIGH);
32+
delay(1000);
33+
}
34+
// Similarly, set switches to "off" state
35+
for (int i = 0; i < 8; i++) {
36+
octofet.digitalWrite(i, LOW);
37+
delay(1000);
38+
}
39+
// Set all power switches at once with one method
40+
// One bit for one channel: 1 is "on" and 0 is "off"
41+
// channels 0, 2, 4, 6 to "on" state
42+
// channels 1, 3, 5, 7 continue to "off" state
43+
octofet.digitalWrite8(0b01010101);
44+
delay(1000);
45+
// Set all of 8 power switches at once to "off" state
46+
octofet.digitalWrite8(0b00000000);
47+
delay(1000);
48+
}

keywords.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#######################################
2+
# Syntax Coloring Map Octofet
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Octofet KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
begin KEYWORD2
16+
digitalWrite KEYWORD2
17+
digitalWrite8 KEYWORD2
18+
getChannelState KEYWORD2
19+
getChannelState8 KEYWORD2
20+
#######################################
21+
# Constants (LITERAL1)
22+
#######################################

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Octofet
2+
version=1.0.0
3+
author=Igor Dementiev <igor@amperka.ru>
4+
maintainer=Amperka <dev@amperka.com>
5+
sentence=Amperka Octofet board (eight-channel power switch) interface library.
6+
paragraph=Control multiple high-power loads through the Amperka Octofet board using hardware or software SPI interface.
7+
category=Device Control
8+
url=https://github.com/amperka/Octofet
9+
architectures=*

src/Octofet.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "Octofet.h"
2+
3+
Octofet::Octofet(uint8_t pinCS, uint8_t deviceCount)
4+
: _spi(pinCS, deviceCount)
5+
, _deviceCount(deviceCount) {
6+
}
7+
8+
Octofet::Octofet(uint8_t pinCS, uint8_t pinMOSI, uint8_t pinSCK, uint8_t deviceCount)
9+
: _spi(pinCS, pinMOSI, pinSCK, deviceCount)
10+
, _deviceCount(deviceCount) {
11+
}
12+
13+
void Octofet::begin() {
14+
_spi.begin();
15+
for (uint8_t i = 0; i < _deviceCount; i++) {
16+
digitalWrite8(0, i);
17+
}
18+
}
19+
20+
void Octofet::digitalWrite(uint8_t channel, bool value, uint8_t device) {
21+
_spi.writeBit(channel, value, device);
22+
_spi.update();
23+
}
24+
25+
void Octofet::digitalWrite8(uint8_t value, uint8_t device) {
26+
_spi.writeByte(value, device);
27+
_spi.update();
28+
}
29+
30+
bool Octofet::getChannelState(uint8_t channel, uint8_t device) const {
31+
return _spi.readBit(channel, device);
32+
}
33+
34+
uint8_t Octofet::getChannelState8(uint8_t device) const {
35+
return _spi.readByte(device);
36+
}

src/Octofet.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef __OCTOFET_H__
2+
#define __OCTOFET_H__
3+
4+
#include "spi2parallel.h"
5+
#include <Arduino.h>
6+
#include <SPI.h>
7+
8+
class Octofet {
9+
public:
10+
Octofet(uint8_t pinCS, uint8_t deviceCount = 1);
11+
Octofet(uint8_t pinCS, uint8_t pinMOSI, uint8_t pinSCK, uint8_t deviceCount = 1);
12+
void begin();
13+
void digitalWrite(uint8_t channel, bool value, uint8_t device = 0);
14+
void digitalWrite8(uint8_t value, uint8_t device = 0);
15+
bool getChannelState(uint8_t channel, uint8_t device = 0) const;
16+
uint8_t getChannelState8(uint8_t device = 0) const;
17+
18+
private:
19+
Spi2Parallel _spi;
20+
uint8_t _deviceCount;
21+
};
22+
23+
#endif // __OCTOFET_H__

src/spi2parallel.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "spi2parallel.h"
2+
3+
Spi2Parallel::Spi2Parallel(uint8_t pinCS, uint8_t deviceCount, uint8_t bitOrder)
4+
: _pinCS(pinCS)
5+
, _deviceCount(deviceCount)
6+
, _bitOrder(bitOrder)
7+
, _deviceChain(new uint8_t[_deviceCount])
8+
, _hardwareSPI(true) {
9+
}
10+
11+
Spi2Parallel::Spi2Parallel(uint8_t pinCS, uint8_t pinMOSI, uint8_t pinSCK, uint8_t deviceCount, uint8_t bitOrder)
12+
: _pinCS(pinCS)
13+
, _pinMOSI(pinMOSI)
14+
, _pinSCK(pinSCK)
15+
, _deviceCount(deviceCount)
16+
, _bitOrder(bitOrder)
17+
, _deviceChain(new uint8_t[_deviceCount])
18+
, _hardwareSPI(false) {
19+
}
20+
21+
Spi2Parallel::~Spi2Parallel() {
22+
if (_deviceChain != nullptr) {
23+
delete[] _deviceChain;
24+
}
25+
}
26+
27+
void Spi2Parallel::begin() {
28+
pinMode(_pinCS, OUTPUT);
29+
digitalWrite(_pinCS, LOW);
30+
if (_hardwareSPI) {
31+
//SPI.beginTransaction(SPISettings(1000000, _bitOrder, SPI_MODE0));
32+
SPI.begin();
33+
SPI.setBitOrder(_bitOrder);
34+
} else {
35+
pinMode(_pinSCK, OUTPUT);
36+
pinMode(_pinMOSI, OUTPUT);
37+
digitalWrite(_pinSCK, LOW);
38+
digitalWrite(_pinMOSI, LOW);
39+
}
40+
}
41+
42+
void Spi2Parallel::writeBit(uint8_t bit, bool value, uint8_t device) {
43+
if (value)
44+
_deviceChain[device] |= (1 << bit);
45+
else
46+
_deviceChain[device] &= ~(1 << bit);
47+
}
48+
49+
void Spi2Parallel::writeByte(uint8_t value, uint8_t device) {
50+
_deviceChain[device] = value;
51+
}
52+
53+
bool Spi2Parallel::readBit(uint8_t bit, uint8_t device) const {
54+
return (_deviceChain[device] >> bit) & 1;
55+
}
56+
57+
uint8_t Spi2Parallel::readByte(uint8_t device) const {
58+
return _deviceChain[device];
59+
}
60+
61+
void Spi2Parallel::update() {
62+
// start condition
63+
if (!_hardwareSPI) {
64+
digitalWrite(_pinSCK, LOW);
65+
digitalWrite(_pinMOSI, LOW);
66+
}
67+
digitalWrite(_pinCS, LOW);
68+
// bit stream output
69+
for (int8_t i = _deviceCount - 1; i >= 0; i--) {
70+
if (_hardwareSPI)
71+
SPI.transfer(_deviceChain[i]);
72+
else
73+
shiftOut(_pinMOSI, _pinSCK, _bitOrder, _deviceChain[i]);
74+
}
75+
// end condition
76+
digitalWrite(_pinCS, HIGH);
77+
if (!_hardwareSPI) {
78+
digitalWrite(_pinMOSI, LOW);
79+
digitalWrite(_pinSCK, LOW);
80+
}
81+
}

src/spi2parallel.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef __SPI2PARALLEL_H__
2+
#define __SPI2PARALLEL_H__
3+
4+
#include <Arduino.h>
5+
#include <SPI.h>
6+
7+
class Spi2Parallel {
8+
public:
9+
Spi2Parallel(uint8_t pinCS, uint8_t deviceCount, uint8_t bitOrder = MSBFIRST);
10+
Spi2Parallel(uint8_t pinCS, uint8_t pinMOSI, uint8_t pinSCK, uint8_t deviceCount, uint8_t bitOrder = MSBFIRST);
11+
~Spi2Parallel();
12+
void begin();
13+
void writeBit(uint8_t bit, bool value, uint8_t device);
14+
void writeByte(uint8_t value, uint8_t device);
15+
bool readBit(uint8_t bit, uint8_t device) const;
16+
uint8_t readByte(uint8_t device) const;
17+
void update();
18+
uint8_t chainLength() const { return _deviceCount; }
19+
20+
private:
21+
uint8_t _pinCS;
22+
uint8_t _pinMOSI;
23+
uint8_t _pinSCK;
24+
uint8_t _deviceCount;
25+
uint8_t _bitOrder;
26+
uint8_t* _deviceChain;
27+
bool _hardwareSPI;
28+
};
29+
30+
#endif // __SPI2PARALLEL_H__

0 commit comments

Comments
 (0)