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; }