Skip to content

Commit

Permalink
updating code for chp 10
Browse files Browse the repository at this point in the history
  • Loading branch information
derekmolloy committed Sep 23, 2018
1 parent 8ac5fae commit 59f9cdc
Show file tree
Hide file tree
Showing 33 changed files with 458 additions and 109 deletions.
10 changes: 2 additions & 8 deletions chp10/gpioExpander/bus/BusDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
/*
* BusDevice.cpp Created on: 23 May 2014
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring Raspberry Pi"
*/

#include "BusDevice.h"

namespace exploringRPi {
namespace exploringBB {

BusDevice::BusDevice(unsigned int bus, unsigned int device) {
this->bus = bus;
Expand All @@ -16,4 +10,4 @@ BusDevice::BusDevice(unsigned int bus, unsigned int device) {

BusDevice::~BusDevice() {}

} /* namespace exploringRPi */
} /* namespace exploringBB */
10 changes: 2 additions & 8 deletions chp10/gpioExpander/bus/BusDevice.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
/*
* BusDevice.h Created on: 23 May 2015
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring Raspberry Pi"
*/

#ifndef BUSDEVICE_H_
#define BUSDEVICE_H_

namespace exploringRPi {
namespace exploringBB {

/**
* @class BusDevice
Expand All @@ -33,6 +27,6 @@ class BusDevice {
virtual ~BusDevice();
};

} /* namespace exploringRPi */
} /* namespace exploringBB */

#endif /* BUSDEVICE_H_ */
33 changes: 5 additions & 28 deletions chp10/gpioExpander/bus/I2CDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
/*
* I2C.cpp Created on: 17 May 2015
* Copyright (c) 2015 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring Raspberry Pi"
* See: www.exploringrpi.com
* Licensed under the EUPL V.1.1
*
* This Software is provided to You under the terms of the European
* Union Public License (the "EUPL") version 1.1 as published by the
* European Union. Any use of this Software, other than as authorized
* under this License is strictly prohibited (to the extent such use
* is covered by a right of the copyright holder of this Software).
*
* This Software is provided under the License on an "AS IS" basis and
* without warranties of any kind concerning the Software, including
* without limitation merchantability, fitness for a particular purpose,
* absence of defects or errors, accuracy, and non-infringement of
* intellectual property rights other than copyright. This disclaimer
* of warranty is an essential part of the License and a condition for
* the grant of any rights to this Software.
*
* For more details, see http://www.derekmolloy.ie/
*/

#include"I2CDevice.h"
#include<iostream>
#include<sstream>
Expand All @@ -36,7 +12,7 @@ using namespace std;

#define HEX(x) setw(2) << setfill('0') << hex << (int)(x)

namespace exploringRPi {
namespace exploringBB {

/**
* Constructor for the I2CDevice class. It requires the bus number and device number. The constructor
Expand All @@ -55,8 +31,9 @@ I2CDevice::I2CDevice(unsigned int bus, unsigned int device):
*/
int I2CDevice::open(){
string name;
if(this->bus==0) name = RPI_I2C_0;
else name = RPI_I2C_1;
if(this->bus==0) name = I2C_0;
else if(this->bus==1) name = I2C_1;
else name = I2C_2;

if((this->file=::open(name.c_str(), O_RDWR)) < 0){
perror("I2C: failed to open the bus\n");
Expand Down Expand Up @@ -169,4 +146,4 @@ I2CDevice::~I2CDevice() {
if(file!=-1) this->close();
}

} /* namespace exploringRPi */
} /* namespace exploringBB */
33 changes: 5 additions & 28 deletions chp10/gpioExpander/bus/I2CDevice.h
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
/*
* I2C.h Created on: 17 May 2015
* Copyright (c) 2015 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring Raspberry Pi"
* See: www.exploringrpi.com
* Licensed under the EUPL V.1.1
*
* This Software is provided to You under the terms of the European
* Union Public License (the "EUPL") version 1.1 as published by the
* European Union. Any use of this Software, other than as authorized
* under this License is strictly prohibited (to the extent such use
* is covered by a right of the copyright holder of this Software).
*
* This Software is provided under the License on an "AS IS" basis and
* without warranties of any kind concerning the Software, including
* without limitation merchantability, fitness for a particular purpose,
* absence of defects or errors, accuracy, and non-infringement of
* intellectual property rights other than copyright. This disclaimer
* of warranty is an essential part of the License and a condition for
* the grant of any rights to this Software.
*
* For more details, see http://www.derekmolloy.ie/
*/

#ifndef I2C_H_
#define I2C_H_
#include"BusDevice.h"

#define RPI_I2C_0 "/dev/i2c-0"
#define RPI_I2C_1 "/dev/i2c-1"
#define I2C_0 "/dev/i2c-0"
#define I2C_1 "/dev/i2c-1"
#define I2C_2 "/dev/i2c-2"

namespace exploringRPi{
namespace exploringBB{

/**
* @class I2CDevice
Expand All @@ -49,6 +26,6 @@ class I2CDevice:public BusDevice{
virtual ~I2CDevice();
};

} /* namespace exploringRPi */
} /* namespace exploringBB */

#endif /* I2C_H_ */
9 changes: 2 additions & 7 deletions chp10/gpioExpander/bus/SPIDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
* SPIDevice.cpp Created on: 22 June 2015
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring Raspberry Pi"
*/

#include "SPIDevice.h"
#include <iostream>
Expand All @@ -22,7 +17,7 @@ using namespace std;

#define HEX(x) setw(2) << setfill('0') << hex << (int)(x)

namespace exploringRPi {
namespace exploringBB {

/**
* The constructor for the SPIDevice that sets up and opens the SPI connection.
Expand Down Expand Up @@ -190,4 +185,4 @@ SPIDevice::~SPIDevice() {
this->close();
}

} /* namespace exploringRPi */
} /* namespace exploringBB */
10 changes: 2 additions & 8 deletions chp10/gpioExpander/bus/SPIDevice.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* SPIDevice.h Created on: 22 May 2015
* Copyright (c) 2015 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring Raspberry Pi"
*/

#ifndef SPIDEVICE_H_
#define SPIDEVICE_H_
#include<string>
Expand All @@ -12,7 +6,7 @@

#define SPI_PATH "/dev/spidev"

namespace exploringRPi{
namespace exploringBB{

/**
* @class SPIDevice
Expand Down Expand Up @@ -55,6 +49,6 @@ class SPIDevice:public BusDevice {
uint16_t delay;
};

} /* namespace exploringRPi */
} /* namespace exploringBB */

#endif /* SPIDEVICE_H_ */
Binary file modified chp10/gpioExpander/display
Binary file not shown.
2 changes: 1 addition & 1 deletion chp10/gpioExpander/displayRegisters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "gpioExpander.h"

using namespace std;
using namespace exploringRPi;
using namespace exploringBB;

int main(){
unsigned char receive; // used to store the send/rec data
Expand Down
Binary file modified chp10/gpioExpander/example
Binary file not shown.
2 changes: 1 addition & 1 deletion chp10/gpioExpander/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "gpioExpander.h"

using namespace std;
using namespace exploringRPi;
using namespace exploringBB;

int main(){
cout << "Starting the GPIO Expander Example" << endl;
Expand Down
11 changes: 2 additions & 9 deletions chp10/gpioExpander/gpioExpander.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
* gpioExpander.cpp Created on: 17 May 2015
* Copyright (c) 2015 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring Raspberry Pi"
* See: www.exploringrpi.com
* Licensed under the EUPL V.1.1 */

#include "gpioExpander.h"
#include <iostream>
#include <unistd.h>
Expand All @@ -15,7 +8,7 @@
#include <iomanip>
using namespace std;

namespace exploringRPi {
namespace exploringBB {

//From Table 1-3 for the MCP23X17 Data sheet
#define IODIRA 0x00 //< Input/Output direction Port A Register
Expand Down Expand Up @@ -275,4 +268,4 @@ void GPIOExpander::dumpRegisters(){
}
}

} /* namespace exploringRPi */
} /* namespace exploringBB */
11 changes: 2 additions & 9 deletions chp10/gpioExpander/gpioExpander.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
* gpioExpander.h Created on: 17 May 2015
* Copyright (c) 2015 Derek Molloy (www.derekmolloy.ie)
* Made available for the book "Exploring Raspberry Pi"
* See: www.exploringrpi.com
* Licensed under the EUPL V.1.1 */

#ifndef GPIOEXPANDER_H_
#define GPIOEXPANDER_H_
#include "bus/SPIDevice.h"
Expand All @@ -13,7 +6,7 @@
/// The MCP23X17 has 11 register pairs (registers 0x00 to 0x1A)
#define BUFFER_SIZE 0x1A

namespace exploringRPi {
namespace exploringBB {

/**
* @class GPIOExpander
Expand Down Expand Up @@ -98,6 +91,6 @@ class GPIOExpander {
virtual ~GPIOExpander() {};
};

} /* namespace exploringRPi */
} /* namespace exploringBB */

#endif /* GPIOEXPANDER_H_ */
Binary file modified chp10/gpioExpander/testI2C
Binary file not shown.
2 changes: 1 addition & 1 deletion chp10/gpioExpander/testI2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "gpioExpander.h"

using namespace std;
using namespace exploringRPi;
using namespace exploringBB;

int main(){
unsigned char value;
Expand Down
Binary file modified chp10/gpioExpander/testSPI
Binary file not shown.
2 changes: 1 addition & 1 deletion chp10/gpioExpander/testSPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "gpioExpander.h"

using namespace std;
using namespace exploringRPi;
using namespace exploringBB;

int main(){
unsigned char send[3], receive[3]; // used to store the send/rec data
Expand Down
16 changes: 16 additions & 0 deletions chp10/hello/hello.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
int ledPin = 13; // the Arduino on-board LED

void setup() {
Serial.begin(9600, SERIAL_8N1); // set up 8-bit with no parity and 1 stop bit
pinMode(ledPin, OUTPUT); // the ledPin is an output
Serial.write("Hello from the Arduino");
}

// this function loops as quickly as possible, forever
void loop() {
digitalWrite(ledPin, HIGH); // set the LED pin high
delay(500); // high and low every second (flash at 1Hz)
digitalWrite(ledPin, LOW); // set the LED pin low
delay(500);
}

23 changes: 23 additions & 0 deletions chp10/i2c/echo/echo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <Wire.h> // Uses the Two-Wire Interface (TWI)

const byte slaveAddr = 0x44; // the slave address of the Arduino
int registerAddr; // the shared register addr variable

void setup() { // the setup function -- called once
TWBR=100000L; // the i2c clk freq: 400000L = 400kHz
Wire.begin(slaveAddr); // set Arduino as an I2C slave device
Wire.onReceive(receiveRegister); // register receive listener below
Wire.onRequest(respondData); // register respond listener below
}

void loop() {
delay(1000); // loop each second -- reduce load
}

void receiveRegister(int x){ // handler called when data available
registerAddr = Wire.read(); // read in one-byte address from PB
}

void respondData(){ // handler that is called on response
Wire.write(registerAddr); // i.e., send the data back to the PB
}
36 changes: 36 additions & 0 deletions chp10/i2c/i2cTMP36/i2cTMP36.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <Wire.h> // uses the Two-Wire Interface (TWI)
const byte slaveAddr = 0x44; // the slave address of the Arduino
int registerAddr; // the shared register addr variable
const int analogInPin = A0; // analog input for the TMP36
int data[4]; // the data registers 00 to 0x03

void setup(){
TWBR=100000L; // set the i2c clk freq e.g. 100000L
Wire.begin(slaveAddr); // set up the Arduino as an I2C slave
Wire.onReceive(receiveRegister); // register receive listener below
Wire.onRequest(respondData); // register respond listener below
}

void loop(){ // update registers every five seconds
int adcValue = analogRead(analogInPin); // using a 10-bit ADC
float curVoltage = adcValue * (5.0f/1024.0f); // Vcc = 5.0V, 10-bit
float tempC = 25.0 + ((curVoltage-0.75f)/0.01f); // from datasheet
float tempF = 32.0 + ((tempC * 9)/5); // deg. C to F
data[0] = (int) tempC; // whole deg C (0x00)
data[1] = (int) ((tempC - data[0])*100); // fract C (0x01)
data[2] = (int) tempF; // whole deg F (0x02)
data[3] = (int) ((tempF - data[2])*100); // fract F (0x03)
delay(5000); // delay 5 seconds
}

void receiveRegister(int x){ // passes the number of bytes
registerAddr = Wire.read(); // read in the one-byte address
}

void respondData(){ // respond function
byte dataValue = 0x00; // default response value is 0x00
if ((registerAddr >= 0x00) && (registerAddr <0x04)){
dataValue = data[registerAddr];
}
Wire.write(dataValue); // send the data back to the PB
}
Loading

0 comments on commit 59f9cdc

Please sign in to comment.