If you are looking for downloadable project files - there aren't any. The reason is that Android Studio changes too often to keep the details up to date. What would happen is that any project file would generate messages about being out of date almost as soon as it was published. Instead here are source code listing that you can copy and paste into a new project. This is a much better way of trying out code in Android Studio. 

So start a new project and paste the code into the appropriate file.

 

Custom 

Pages 292-

 

 

 

Marketing, rights and trade orders: This email address is being protected from spambots. You need JavaScript enabled to view it.

Commissioning Editor: This email address is being protected from spambots. You need JavaScript enabled to view it.

Send any error reports or questions concerning content to:  This email address is being protected from spambots. You need JavaScript enabled to view it.

All other matters email: This email address is being protected from spambots. You need JavaScript enabled to view it.

 

Our aim is to bring you not just books written by experts on their subjects, but books written by experts at writing books. Our authors don't just repeat the documentation, they explore the subject matter. They go off the beaten track to find out how things works and make things happen. 

We also use programmers to proofread our books and we do our best to ensure the layout is sympathetic to programs. We try not to have a page break in the middle of a program so that you can read everything without having to move to a new page and forget what you have read. Of course, this isn't always possible and when a program has to be split we attempt to find a location that divides the listing at a point of meaning - e.g. try not to split a function or subroutine.

As layout is important, we prefer our paperback imprints - they are close to what we would like to read. Until recently we didn't  make ebook versions available because free flowing layout is not good for programming titles. Now that Kindle supports "Print Replica" format we do offer ebook versions. These aren't pure ebooks and they lack some features. In particular at the moment you can't read them on a Kindle only via Kindle Reader on mobiles, tablets, portables and desktop computers. The advantage is that you see pages formatted as in the paper book. We hope that Amazon improves the Print Replica format in the future to allow us to provide a fuller ebook support. 

Important Information:
All our Kindle edition are in Print Replica Format.
The reason for this is that we regard paging as essential for programming books however this means it only works on Fire Tablets and Kindle Reading Apps.
Check to see if your device is supported!

You can of course still read much of the content of our books on I Programmer and IoT-Programmer.

Please support our efforts by buying copies of our books and visiting the sites with ad-blocking off. We would like to encourage our authors to write more!

If you have any suggestions for titles contact us at: This email address is being protected from spambots. You need JavaScript enabled to view it.

 LATEST TITLES 

Raspberry Pi IoT in Python
 Using GPIO Zero
 2nd Edition 

 
GPIOZero2E180  

Raspberry Pi IoT in Python
With Linux Driver
2nd Edition 

Raspberry Pi IoT In C
Using Linux Drivers
2nd Edition

DriverPython2e180 DriverC2E180 

Extending & Embedding 
Python Using C

Raspberry Pi IoT In C,
3rd Edition
 

extendPython180 piciot3e180 

Master the Raspberry Pi Pico
in C:
WiFi with lwIP & mbedtls

Programming the ESP32
in MicroPython

picomaster180  esppython180 

Programming The
RaspberryPi Pico/W in C
Second Edition

Programming the
Raspberry Pi Pico/W
in MicroPython,
Second Edition 

 
picoC2E180 picopython2e180 

Programmer's Python:
Everything Is An Object,
Second Edition

Programmer’s Python:
Everything is Data

pythonObject2e180 pythondata180

The Trick Of The Mind

 

Programmer's Python:
Async

Trick180 pythonAsync180 

Programmer's Guide To Kotlin
Second Edition

Deep C# :
Dive Into Modern C#

Explore Intel Edison  Programs

We have decided not to make the programs available as a download because this is not the point of the book - the programs are not finished production code but something you should type in and study.

There is also the problem of what happens when NetBeans changes and the downloads no longer work. 

The best solution is to provide the source code of the programs in a form that can be copied and pasted into a NetBeans project. This means that you have some interaction with the code rather than just running it and it means that it is immune to changes in NetBeans.

The only downside is that you have to create a project to paste the code into.

To do this follow the instruction in chapter 2.

If anything you consider important is missing or if you have any requests or comments  contact me

This email address is being protected from spambots. You need JavaScript enabled to view it.

page 33

#include "mraa.h"
#include 
#include 

int main()
{
 mraa_gpio_context pin = mraa_gpio_init(13);
 mraa_gpio_dir(pin, MRAA_GPIO_OUT);

 for (;;) {
  mraa_gpio_write(pin, 0);
  sleep(1);
  mraa_gpio_write(pin, 1);
  sleep(1);
 }

 return MRAA_SUCCESS;
}

page 44

#include "mraa.h"
#include 
#include 
int main()
{
 mraa_init();
 mraa_gpio_context pin15 = mraa_gpio_init(15);
 mraa_gpio_dir(pin15, MRAA_GPIO_OUT_HIGH);
 mraa_gpio_context pin31 = mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_OUT_LOW);
 for (;;) {
  mraa_gpio_write(pin15, 0);
  mraa_gpio_write(pin31, 1);
  mraa_gpio_write(pin15, 1);
  mraa_gpio_write(pin31, 0);
 }
 return MRAA_SUCCESS;
}

page 46

#include "mraa.h"
#include 
#include 

int main()
{

 mraa_init();
 mraa_gpio_context pin31 = mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_IN);
 int in;
 for (;;) {
  in=mraa_gpio_read(pin31);
  printf("switch %d \n",in);
  usleep(1000*1000);
 }
 return MRAA_SUCCESS;
}

page 48

#include "mraa.h"
#include 
#include 

void switchChange(); 

int main(){ 
 mraa_init(); 
 mraa_gpio_context pin31 = mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_IN); 
 mraa_gpio_isr(pin31, MRAA_GPIO_EDGE_FALLING, &switchChange,NULL);
 for (;;) {};

 return MRAA_SUCCESS;
} 

void switchChange(){ 
 printf("switch \n");
}

page 49

#include "mraa.h" 
#include 
#include 

void switchChange(void* pin);
int main(){
 mraa_init();
 mraa_gpio_context pin31 = mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_IN); 
 mraa_gpio_isr(pin31, MRAA_GPIO_EDGE_BOTH, &switchChange,pin31);
 for (;;) {};
 return MRAA_SUCCESS;
} 

void switchChange(void* pin){
    int s=mraa_gpio_read((mraa_gpio_context) pin);
    printf("switch %d \n",s);
}

page 52

#include "mraa.h" 
#include 
#include  
#include 

page 60

#include "mraa.h"
#include 
#include 

int main()
{
 mraa_gpio_context pin31=mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_OUT);
 mraa_gpio_use_mmaped(pin31,1);
 for (;;) {
  mraa_gpio_write(pin31, 0);
  mraa_gpio_write(pin31, 1);
 }
 return MRAA_SUCCESS;
}

page 61

#include "mraa.h"
#include 
#include 

int main()
{
 mraa_gpio_context pin31=mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_OUT);
 mraa_gpio_use_mmaped(pin31,1);
 int i;
 for (;;) {
  mraa_gpio_write(pin31, 0);
  for(i=1;i<7500;i++){};
  mraa_gpio_write(pin31, 1);
  for(i=1;i<7500;i++){};
 }
 return MRAA_SUCCESS;
}

page 62

#include 
#include 
int main(){ 
 mraa_init(); 
 mraa_gpio_context pin15 = mraa_gpio_init(15);
 mraa_gpio_dir(pin15, MRAA_GPIO_OUT_HIGH);
 mraa_gpio_use_mmaped(pin15,1);
 mraa_gpio_context pin31 = mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_OUT_LOW);
 mraa_gpio_use_mmaped(pin31,1);
 for (;;) {
  mraa_gpio_write(pin15, 0);
  mraa_gpio_write(pin31, 1);
  mraa_gpio_write(pin15, 1);
  mraa_gpio_write(pin31, 0);
 }
 return MRAA_SUCCESS;
}

page 66

#include 
#include  

int main() {
 mraa_init();
 mraa_gpio_context pin31 = mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_IN);
 mraa_gpio_use_mmaped(pin31,1);
 int i;

 for(;;){
  if(mraa_gpio_read(pin31)==0)break;
 }

 for(i=0;i<10000;i++){
  if(mraa_gpio_read(pin31)==1)break;
 }

 for(i=1;i<10000;i++){
  if(mraa_gpio_read(pin31)==0)break;
 }

 printf("%d \n",i);
 return MRAA_SUCCESS;
}

page 68

#include 
#include 
#include <sys/mman.h>
#include <sys/stat.h>

uint8_t* load_memmap();

int main()
{
 mraa_init();
 mraa_gpio_context pin31 = mraa_gpio_init(31);
 mraa_gpio_dir(pin31, MRAA_GPIO_OUT);
 
 load_memmap();
 int pin= 44;

 uint32_t mask=(uint32_t)(1 << (pin % 32));
 uint8_t valoffsetOn = 0x34;
 uint8_t valoffsetOff = 0x4c;
 
 uint8_t* loc=(pin / 32) * sizeof(uint32_t)+ mmap_reg;

 for (;;) {
  *(volatile uint32_t*) (loc + valoffsetOn) = mask;
  *(volatile uint32_t*) (loc + valoffsetOff) = mask;
 }
 return MRAA_SUCCESS;
}

uint8_t* load_memmap(){
 int mmap_fd;
 struct stat fd_stat;
 uint8_t* mmap_reg;
 mmap_fd = open(  "/sys/devices/pci0000:00/0000:00:0c.0/resource0",  O_RDWR);
 fstat(mmap_fd, &fd_stat);
 mmap_reg =(uint8_t*) mmap(NULL, fd_stat.st_size,
   PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
   mmap_fd, 0);
 return mmap_reg;
}

page 76

#include 
#include 
#include "mraa.h"
#include 
int main() {
 mraa_gpio_context pin = mraa_gpio_init(13);
 mraa_gpio_dir(pin, MRAA_GPIO_OUT);
 for (;;) {
  mraa_gpio_write(pin, 0);
  mraa_gpio_write(pin, 1);
 }
 return MRAA_SUCCESS;
}

page 78

#include 
#include 
#include "mraa.h"
#include 
int main() {
 const struct sched_param priority={1};
 sched_setscheduler(0,SCHED_FIFO,&priority);
 mraa_gpio_context pin = mraa_gpio_init(13);
 mraa_gpio_dir(pin, MRAA_GPIO_OUT);
 mraa_gpio_use_mmaped(pin,1);
 for (;;) {
  mraa_gpio_write(pin, 0);
  mraa_gpio_write(pin, 1);
 }
 return MRAA_SUCCESS;
}

page 85

#include "mraa.h"
#include 
#include int main()
{
 mraa_pwm_context pwm0 = mraa_pwm_init(20);
 mraa_pwm_context pwm1 = mraa_pwm_init(14);
 mraa_pwm_context pwm2 = mraa_pwm_init(0);
 mraa_pwm_context pwm3 = mraa_pwm_init(21); 
 mraa_pwm_period_us(pwm0, 10);
 mraa_pwm_period_us(pwm1, 20);
 mraa_pwm_period_us(pwm2, 30);
 mraa_pwm_period_us(pwm3, 40);
 mraa_pwm_write(pwm0, 0.5f);
 mraa_pwm_write(pwm1, 0.4f);
 mraa_pwm_write(pwm2, 0.3f);
 mraa_pwm_write(pwm3, 0.2f);
 mraa_pwm_enable(pwm0,1 );
 mraa_pwm_enable(pwm1,1 );
 mraa_pwm_enable(pwm2,1 );
 mraa_pwm_enable(pwm3,1 );
 return MRAA_SUCCESS;
}
 

page 89

#include "mraa.h"
#include 
#include 
int main()
{
 mraa_pwm_context pwm0 = mraa_pwm_init(20);
 mraa_pwm_enable(pwm0,0 );
 mraa_pwm_period_ms(pwm0,1);
 mraa_pwm_enable(pwm0,1 );
 int w=1;
 int inc=1;
 for (;;) {
  mraa_pwm_pulsewidth_us (pwm0, w);
  w=w+inc;
  if(w>100 || w<2)inc=(-1)*inc;
  usleep(1000);
 }
 return MRAA_SUCCESS;
}
 

page 90

 
#include "mraa.h"
#include 
#include 
int main()
{
 mraa_pwm_context pwm0 = mraa_pwm_init(20);
 mraa_pwm_enable(pwm0,0 );
 mraa_pwm_period_ms(pwm0,1);
 mraa_pwm_enable(pwm0,1 );

 int w=0;
 int b=0;
 int inc=1;
 for (;;) {
  b+=inc;
  w=b*b*b;
  mraa_pwm_pulsewidth_us (pwm0, w);
  if(w>=1000 || w<=0)inc=(-1)*inc;
  usleep(40000);
 }
 return MRAA_SUCCESS;
}

page 93

#include "mraa.h"
#include 
#include 

int main()
{
 mraa_pwm_context pwm3 = mraa_pwm_init(21);
 mraa_pwm_enable(pwm3,0 );
 mraa_pwm_period_ms(pwm3,20);
 mraa_pwm_enable(pwm3,1 );
 for (;;) {
  mraa_pwm_pulsewidth_us (pwm3,20000-1000);
  sleep(4);
  mraa_pwm_pulsewidth_us (pwm3,20000-1500);
  sleep(4);
  mraa_pwm_pulsewidth_us (pwm3,20000-2000);
  sleep(4);
 }
 return MRAA_SUCCESS;
}

page 113

 
#include "mraa.h"
#include 
#include 
uint8_t crcCheck(uint8_t, uint8_t, uint8_t);
int main()
{ 
 mraa_i2c_context i2c;
 i2c = mraa_i2c_init(1);
 mraa_i2c_address(i2c, 0x40);
 uint8_t data =mraa_i2c_read_byte_data (i2c, 0xE7);
 printf("Register= %d \n", data);
 uint8_t buf[3];
 mraa_i2c_read_bytes_data(i2c,0xE3,buf,3);
 uint8_t msb= buf[0];
 uint8_t lsb= buf[1];
 uint8_t check= buf[2];
 printf(" msb %d \n lsb %d \n checksum %d \n",msb,lsb,check);
 printf("crc %d \n ", crcCheck(msb,lsb,check));
 unsigned int data16=((unsigned int) msb << 8) |
                                (unsigned int) (lsb & 0xFC);
 float temp = (float)(-46.85 + (175.72 * data16 / (float)65536));
 printf("Temperature %f C \n",temp);
 mraa_i2c_read_bytes_data(i2c,0xE5,buf,3);
 msb= buf[0];
 lsb= buf[1];
 check= buf[2];
 printf(" msb %d \n lsb %d \n checksum %d \n", msb,lsb,check);
 printf("crc %d \n ", crcCheck(msb,lsb,check));
 data16=((unsigned int) msb << 8) | (unsigned int) (lsb & 0xFC);
 float hum = (float)(-6 + (125.0 * data16 / (float)65536));
 printf("Humidity %f %% \n",hum);return MRAA_SUCCESS;
}

uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check){
 uint32_t data32 = ((uint32_t)msb << 16)|((uint32_t) lsb <<8) | (uint32_t) check;
 uint32_t divisor = 0x988000;
 for (int i = 0 ; i < 16 ; i++) 
 {
  if( data32 & (uint32_t)1<<(23 - i) )data32 ^= divisor;
  divisor>>= 1;
 };
 return (uint8_t) data32;
}

page 135

 
#include "mraa.h"
#include 
#include 

uint getByte(int,int[]);

int main()
{
 const struct sched_param priority={1};
 sched_setscheduler(0,SCHED_FIFO,&priority);

 mraa_gpio_context pin = mraa_gpio_init(31);
 mraa_gpio_use_mmaped(pin,1);
 mraa_gpio_dir(pin, MRAA_GPIO_OUT_HIGH);
 
 mraa_gpio_write(pin, 0);
 usleep(1000);
 mraa_gpio_dir(pin, MRAA_GPIO_IN);int buf[40];

 int i, j;
 for(j=0;j<=40;j++){
  for(i=1;i<200;i++){
   if(mraa_gpio_read(pin)==1)break;
  };
  for(i=1;i<200;i++){
   if(mraa_gpio_read(pin)==0)break;
  }
  buf[j]=0;
  if(i>75)buf[j]=1;
 }

 for(j=0;j<=40;j++){
  printf("%d %d \n",j,buf[j]);
 }

 int byte1=getByte(1,buf);
 int byte2=getByte(2,buf);
 int byte3=getByte(3,buf);
 int byte4=getByte(4,buf);
 int byte5=getByte(5,buf);

 printf("Checksum %d %d \n",byte5,
     (byte1+byte2+byte3+byte4) & 0xFF);
 
 float humidity= (float) (byte1<0)temperature=-temperature;
 printf("Temperature= %f \n",temperature);

 return MRAA_SUCCESS;
}

uint getByte(int b,int buf[]){
 int i;
 uint result=0;
 b=(b-1)*8+1;
 for(i=b;i<=b+7;i++){
  result<<=1;
  result |= buf[i];
 }
 return result;
}

page 150

 
#include "mraa.h"
#include 
#include int init();

int readBit();
int readByte();
void writeBit(int);
void writeByte(int);
int convert();
void putSetting(int,char[],char[]);

mraa_gpio_context pinIn;
mraa_gpio_context pinOut;

int main() {
 const struct sched_param priority = { 1 };
 sched_setscheduler(0, SCHED_FIFO, &priority);

 pinIn = mraa_gpio_init(32);
 mraa_gpio_use_mmaped(pinIn, 1);
 mraa_gpio_dir(pinIn, MRAA_GPIO_IN);pinOut = 

 mraa_gpio_init(31);
 mraa_gpio_use_mmaped(pinOut, 1);
 mraa_gpio_dir(pinOut, MRAA_GPIO_OUT_HIGH);
putSetting(44,"pullmode","nopull");
putSetting(44,"opendrain","enable");

 if (init() == 1) {
  printf("No device \n");
  return MRAA_SUCCESS;
 }
 writeByte(0xCC);
 convert();
 
 if (init() == 1) {
  printf("No device \n");
  return MRAA_SUCCESS;
 }
 writeByte(0xCC);
 writeByte(0xBE);

 int b1 = readByte();
 printf("%d \n", b1);
 int b2 = readByte();

 printf("%d \n", b2);
 int16_t temp1 = (b2 << 8 | b1);
 float temp = (float) temp1 / 16;
 printf("temperature = %f C \n", temp);
 return MRAA_SUCCESS;
}

void writeBit(int b) {
 int i;
 int delay1, delay2;
 if (b == 1) {
  delay1 = 60;
  delay2 = 4000;
 } else {
  delay1 = 4000;
  delay2 = 60;
 }
 mraa_gpio_write(pinOut, 0);
 for (i = 1; i < delay1; i++) {
 };
 mraa_gpio_write(pinOut, 1);
 for (i = 1; i < delay2; i++) {
 };
 }
 
int readBit() {
 int i;
 mraa_gpio_write(pinOut, 0);
 for (i = 1; i < 60; i++) {
 };
 mraa_gpio_write(pinOut, 1);
 for (i = 1; i < 450; i++) {
 };
 int b = mraa_gpio_read(pinIn);
 for (i = 1; i < 3500; i++) {
 };
 return b;
}

void writeByte(int byte) {
 int i;
 for (i = 0; i < 8; i++) {
  if (byte & 1) {
   writeBit(1);
  } else {
   writeBit(0); 
  }
  byte = byte >> 1;
 }
}

int readByte() {
 int byte = 0;
 int i;
 for (i = 0; i < 8; i++) {
  byte = byte | readBit() << i;
 };
 return byte;
}

int convert() {
 int i;
 writeByte(0x44);
 for (i = 1; i < 1000; i++) {
  usleep(10000);
  if (readBit() == 1)
   break;
  };
 return (i == 1000);
}

int init() {
 mraa_gpio_write(pinOut, 0);
 usleep(500);
 mraa_gpio_write(pinOut, 1);
 usleep(60);
 int b = mraa_gpio_read(pinIn);
 usleep(500);
 return b;
}
void putSetting(int pin,char prop[],char value[]){
 char buf[200];
 sprintf(buf, "/sys/kernel/debug/gpio_debug/gpio%d/current_%s",pin,prop);
 int fd = open(buf, O_WRONLY);
 write(fd, value, strlen(value));
 close(fd);
}

page 161

 
#include "mraa.h"
#include 
#include int main() {
 mraa_spi_context spi = mraa_spi_init(0);
 mraa_spi_mode(spi, MRAA_SPI_MODE0);
 mraa_spi_frequency(spi, 400000);
 mraa_spi_lsbmode(spi, 0);
 mraa_spi_bit_per_word(spi, 8);
 uint16_t read_data = mraa_spi_write(spi, 0xAA);
 if (read_data == 0xAA) printf("data received correctly");
 mraa_spi_stop(spi);
 return MRAA_SUCCESS;
}

page 168

#include "mraa.h"
#include 
#include 
uint8_t sendbyte(uint8_t byte, int delay);
mraa_gpio_context CS1;
mraa_gpio_context SCK;
mraa_gpio_context MOSI;
mraa_gpio_context MISO;

int main() {
 const struct sched_param priority = { 1 };
 sched_setscheduler(0, SCHED_FIFO, &priority);
 CS1 = mraa_gpio_init(9);
 mraa_gpio_use_mmaped(CS1, 1);
 mraa_gpio_dir(CS1, MRAA_GPIO_OUT);
 mraa_gpio_write(CS1, 1);
 SCK = mraa_gpio_init(10);
 mraa_gpio_use_mmaped(SCK, 1);
 mraa_gpio_dir(SCK, MRAA_GPIO_OUT);
 mraa_gpio_write(SCK, 0);
 MOSI = mraa_gpio_init(11);
 mraa_gpio_use_mmaped(MOSI, 1);
 mraa_gpio_dir(MOSI, MRAA_GPIO_OUT);
 mraa_gpio_write(MOSI, 0);
 MISO = mraa_gpio_init(24);
 mraa_gpio_use_mmaped(MISO, 1);
 mraa_gpio_dir(MISO, MRAA_GPIO_IN);
 mraa_gpio_read(MISO);
 int delay = 1000;
 uint8_t read;
 for (;;) {
  read = sendbyte(0xAA, delay);
  if (read != 0xAA)
  printf("Error \n");
 }
 return MRAA_SUCCESS;
}
uint8_t sendbyte(uint8_t byte, int delay) {
 int i, j;
 int read = 0;
 mraa_gpio_write(CS1, 0);
 for (j = 1; j < delay / 8 + 100; j++) {
 };
 for (i = 0; i < 8; i++){
  mraa_gpio_write(MOSI, byte & 0x80);
  byte = byte << 1;
  for (j = 1; j < delay; j++) {
  };
  mraa_gpio_write(SCK, 1);
  read = read << 1;
  read = read | (mraa_gpio_read(MISO));
  for (j = 1; j < delay; j++) {
  };
  mraa_gpio_write(SCK, 0);
 }
 mraa_gpio_write(CS1, 1);
 for (j = 1; j < delay / 8 + 20; j++) { };
 return (uint8_t) read;
}

page 182

 
#include 
#include 
#include "mraa.h"
#include 

int readADC(mraa_spi_context spi, uint8_t chan);
mraa_spi_context initADC(int freq);
int readADCsoft(uint8_t chan, int delay);
void initSPIsoft();

mraa_gpio_context CS1;
mraa_gpio_context SCK;
mraa_gpio_context MOSI;
mraa_gpio_context MISO;

int main() {
 const struct sched_param priority = { 1 };
 sched_setscheduler(0, SCHED_FIFO, &priority);
 // mraa_spi_context spi=initADC(1000000);
 int data;
 initSPIsoft();
 for (;;) {
  // data =readADC(spi,0);
  data = readADCsoft(0, 0);
 }
 printf("Data %d \n", data);
 float volts = ((float) data) * 3.3f / 1023.0f;
 printf("volts= %f \n", volts);
 // mraa_spi_stop(spi);
 return 0;
}

int readADC(mraa_spi_context spi, uint8_t chan) {
 uint8_t buf[] = {0x01,(0x08 | chan) << 4, 0x00};
 uint8_t readBuf[3];
 mraa_spi_transfer_buf(spi, buf, readBuf, 3);
 return ((int) readBuf[1] & 0x03) << 8 | (int) readBuf[2];
} 

mraa_spi_context initADC(int freq) {
 mraa_spi_context spi = 
 mraa_spi_init(0);mraa_spi_mode(spi, MRAA_SPI_MODE0);
 mraa_spi_frequency(spi, freq);
 mraa_spi_lsbmode(spi, 0);
 mraa_spi_bit_per_word(spi, 8);
 return spi;
} 
int readADCsoft(uint8_t chan, int delay) {
 int i, j;
 int read;
 int result;
 uint8_t byte;
 read = 0;
 mraa_gpio_write(CS1, 0);
 for (j = 1; j < 100; j++) {
 };
 byte = 0x01;
 for (i = 0; i < 8; i++) {
  mraa_gpio_write(MOSI, byte & 0x80);
  byte = byte << 1;
  for (j = 1; j < delay; j++) {
  };
  mraa_gpio_write(SCK, 1);
  for (j = 1; j < delay / 2; j++) {
  };
  read = read << 1;
  read = read | (mraa_gpio_read(MISO));
  for (j = 1; j < delay / 2; j++) {
  };
  mraa_gpio_write(SCK, 0);
 }
 byte = (0x08 | chan) << 4;
 for (i = 0; i < 8; i++) {
  mraa_gpio_write(MOSI, byte & 0x80);
  byte = byte << 1;
  for (j = 1; j < delay; j++) {
  };
  mraa_gpio_write(SCK, 1);
  for (j = 1; j < delay / 2; j++) {
  };
  read = read << 1;
  read = read | (mraa_gpio_read(MISO));
  for (j = 1; j < delay / 2; j++) {
  };
  mraa_gpio_write(SCK, 0);
 }
 result = (int) read & 0x03 << 8;
 byte = 0;
 for (i = 0; i < 8; i++) {
  mraa_gpio_write(MOSI, byte & 0x80);
  byte = byte << 1;
  for (j = 1; j < delay; j++) {
  };
  mraa_gpio_write(SCK, 1);
  for (j = 1; j < delay / 2; j++) {
  };
  read = read << 1;
  read = read | (mraa_gpio_read(MISO));
  for (j = 1; j < delay / 2; j++) {
  };
  mraa_gpio_write(SCK, 0);
 }
 mraa_gpio_write(CS0, 1);
 for (j = 1; j < 10; j++) {
 };
 return result | read;
}
 
void initSPIsoft() {
 CS1 = mraa_gpio_init(9);
 mraa_gpio_dir(CS1, MRAA_GPIO_OUT);
 mraa_gpio_use_mmaped(CS1, 1);
 mraa_gpio_write(CS1, 1);
 
 SCK = mraa_gpio_init(10);
 mraa_gpio_dir(SCK, MRAA_GPIO_OUT);
 mraa_gpio_use_mmaped(SCK, 1);
 mraa_gpio_write(SCK, 0);
 
 MOSI = mraa_gpio_init(11);
 mraa_gpio_dir(MOSI, MRAA_GPIO_OUT);
 mraa_gpio_use_mmaped(MOSI, 1);
 mraa_gpio_write(MOSI, 0);
 
 MISO = mraa_gpio_init(24);
 mraa_gpio_use_mmaped(MISO, 1);
 mraa_gpio_dir(MISO, MRAA_GPIO_IN);
}


page 189

 
#include 
#include 
#include "mraa.h"
int main() {
 int fd;
 char buf[100];
 int gpio = 44;
 fd = open("/sys/class/gpio/export", O_WRONLY);
 sprintf(buf, "%d", gpio);
 write(fd, buf, strlen(buf));
 close(fd);
 sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);
 fd = open(buf, O_WRONLY);
 write(fd, "out", 3);
 close(fd);
 sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
 fd = open(buf, O_WRONLY);
 for(;;){
  write(fd, "1", 1);
  write(fd, "0", 1);
 }
 close(fd);
 fd = open("/sys/class/gpio/unexport", O_WRONLY);
 sprintf(buf, "%d", gpio);
 write(fd, buf, strlen(buf));
 close(fd); 
 
 return 0;
}

 Page 24

 

Create a folder on the local machine to act as the top level project folder – all of your C projects will be stored in sub-folders of this one. Within your top-level folder, create a folder called .json and create a custom settings.json file. This contains the details of the remote machine and the folders you want to use on the remote:

change .json to:.vscode

Create a folder on the local machine to act as the top level project folder – all of your C projects will be stored in sub-folders of this one. Within your top-level folder, create a folder called .vscode if you need to, and create a custom settings.json file. This contains the details of the remote machine and the folders you want to use on the remote:

Page 132

 

The number of different duty cycles you can achieve depends on the number of clock pulses in the total period. At 10MHz there are just two clock pulses and so just four duty cycles 0%, 50% and 100%.

change four to three

The number of different duty cycles you can achieve depends on the number of clock pulses in the total period. At 10MHz there are just two clock pulses and so just three duty cycles 0%, 50% and 100%.

The table on this page should read:

PWM Frequency

Number of clock pulses

Number of different duty cycles

Resolution in bits

10MHz

2

3

1.6

5MHz

4

5

2.3

2.5MHz

8

9

3.2

1.25MHz

16

17

4.1

625kHz

32

33

5.0

312.5kHz

64

65

6.0

156.25kHz

128

129

7.0

nkHz

20000/n

20000/n - 1

Log2(20000/n -1)

In many applications 8-bit resolution for the duty cycle is considered the minimum acceptable and this sets the highest frequency to about 75kHz, which is high enough for most things.

For example, if you want to control a servo motor, see later, then you need a PWM signal with a frequency of 50Hz and at this frequency you can specify the duty cycle down to about 25 bits or around 40 million increments – more than enough for any real servo motor.

 

 

Page 148

The number of SPI buses a Raspberry Pi has depends on its model. The PiZero and Pi 3 have three SPI devices, but only SP0 and SP1 are available on the GPIO connector:

change SP0 and SP1 to SPI0 and SPI1 

The number of SPI buses a Raspberry Pi has depends on its model. The PiZero and Pi 3 have three SPI devices, but only SPI0 and SPI1 are available on the GPIO connector:

Page 153

First connect pin 19 to pin 20 using a jumper wire and start a new project. The program is very simple. First we check that the SPI bus is loaded:

change 20 to 21

First connect pin 19 to pin 21 using a jumper wire and start a new project. The program is very simple. First we check that the SPI bus is loaded:

Page 242

Change

struct sockaddr_nl sa;
sa.nl_family = AF_NETLINK;
sa.nl_pid = getpid();
sa.nl_groups = 0;

change sa.nl_pid = getpid();  to sa.nl_pid = 0;

struct sockaddr_nl sa;
sa.nl_family = AF_NETLINK;
sa.nl_pid = 0;
sa.nl_groups = 0;

WIthout this change the program doesn't work but returns no error

Page 248 (Same error as page 242)

Change

struct sockaddr_nl sa;
sa.nl_family = AF_NETLINK;
sa.nl_pid = getpid();
sa.nl_groups = 0;

change sa.nl_pid = getpid();  to sa.nl_pid = 0;

struct sockaddr_nl sa;
sa.nl_family = AF_NETLINK;
sa.nl_pid = 0;
sa.nl_groups = 0;