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-

 

 

Page 124

All variables named temp1 should be temp.

So the bottom part  of the page should read:

 

All we now have to do do is to put the two bytes together as a 16-bit integer. As the micro:bit supports a 16-bit int we can do this very easily:

 

int16_t temp= (b2<<8 | b1) ;

 

Finally we need to convert this to a floating point value. As already discussed, the micro:bit doesn’t support this in hardware. As all we really want is two digits to give the tens and two digits to give the fractional part it is easier to work in integer arithmetic using temperature*100:

 

temp = temp * 100 / 16;

This gives us an integer value that represents the temperature in hundredths of a degree centigrade, e.g. 25.45C is represented as 2545. Notice that this only works because int16_t really is a 16-bit integer. If you were to use a 32‑bit int:

 

int temp= (b2<<8 | b1);

then temp1 would be correct for positive temperatures but it would give the wrong answer for negative values because the sign bit isn't propagated into the top 16 bits. So if using a 32-bit integer, propagate the sign bit manually:

 

int temp=(b2<<8 | b1); if(b2 & 0x80) temp=temp | 0xFFFF0000;

Assuming we have the temperature in 100ths in temp, we can now display it on the micro:bit's LED matrix. First we need to convert it into a string:

 

char buff[10];

 

 

Note: It is correct in the final listing.

 

Just jQuery: Events, Async & AJAX

Errata: None 

 

 

ISBN: 978-1871962529

 

 

Buy from: 

USA and World   Amazon.com
Canada          Amazon.ca
UK              Amazon.co.uk
France          Amazon.fr
Germany         Amazon.de
Spain           Amazon.es
Italy           Amazon.it

 

  • Paperback: 214 pages
  • Publisher: I/O Press; 1 edition (June 20, 2017)
  • Language: English
  • ISBN-10: 1871962528
  • ISBN-13: 978-1871962529

 

jQuery is the closest thing JavaScript has to a standard library, yet many programmers never venture beyond its most obvious facilities. As well as having easy to use and very powerful DOM manipulation features, described in the companion book Just jQuery: The Core UI, it also offers an improved JavaScript event system, help with writing asynchronous code in the form of Promises, and easy to use and powerful AJAX functions.

Written for JavaScript developers working with advanced web pages Just jQuery: Events, Async & AJAX covers the parts of jQuery not associated with the DOM. Specifically it is about how to make use of jQuery’s event functions, Deferred and Promise functions and its AJAX functions. While not every programmer will need these advanced features in the early stages of using JavaScript, they are unavoidable aspects of modern web programming and sooner or later you will find a need to master them all.

This book is about ideas. It does show you how to use jQuery, but mainly by explaining how jQuery approaches the task. Once you understand this there is little need to go over complicated examples where the problem is seeing the big ideas because the small detail is overwhelming. This is not a book of projects or case studies, it is about understanding jQuery.

 

Contents:

  1. Events, Async & AJAX

  2. Events, Async & Ajax

  3. Reinventing Events

  4. Working With Events

  5. Asynchronous Code

  6. Consuming Promises

  7. Using Promises

  8. WebWorkers

  9. Ajax the Basics - get

  10. Ajax the Basics -  post

  11. Ajax - Advanced Ajax To The Server

  12. Ajax - Advanced Ajax To The Client

  13. Ajax - Advanced Ajax Transports And JSONP

  14. Ajax - Advanced Ajax The jsXHR Object

  15. Ajax - Advanced Ajax Character Coding And Encoding 

 

About the Author 

Ian Elliot is a freelance consultant used to meeting challenges in a range of arenas and using all the tools and skills a programmer has in their armoury. A core member of the I Programmer team, he writes on C# and JavaScript as well as all other aspects of web development. 

Printing 0

12/14/2016

 

Page 47

Add 

#define _GNU_SOURCE

to the start of all programs that make use of the time.h header

i.e.page 47, page 271 and page 282

Program listings web page has been updated

Page 49 

The program seems to have aquired some nonsense at the start! The correct version is:

#include <bcm2835.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    if (!bcm2835_init())
        return 1;
    bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_07,
                            BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_11,
                            BCM2835_GPIO_FSEL_OUTP);
    volatile int i;
    for (;;) {
        bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_07, LOW);
        bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_11, HIGH);
        bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_07, HIGH);
        bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_11, LOW);
    }
    return (EXIT_SUCCESS);
}

Page 166

The printf and humidy calculation uses byte1 to byte5 before they have been defined. The order just needs to be changed:

Change

printf("Checksum %d %d \n\r", byte5,  
        (byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float) (byte1 << 8 | byte2) / 10.0;
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("Humidity= %f \n\r", humidity); 

 

To 


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\r", byte5,  
        (byte1 + byte2 + byte3 + byte4) & 0xFF);
float humidity = (float) (byte1 << 8 | byte2) / 10.0;
printf("Humidity= %f \n\r", humidity);

 Page 250

 The test:

If(maxbytes>=sizeof(buf)) return -1

is silly. It is an attempt to check that the buffer is big enough but sizeof will return the size of the pointer not the array.

The size of the array is implied in maxbytes so no test is needed -- delete the test and ensure that buf has at least maxbytes elements before calling the function.

Page 257

Missing include means you get warnings about undeclared functions. Add the final include as listed:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>

#include <unistd.h>

Page 263

Missing include means you get warnings about undeclared functions. Add the final include as listed:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

 You also need to change the inital part of the program to read:

 hints.ai_family =  AF_INET;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_flags = AI_PASSIVE;
 getaddrinfo(NULL, "80", &hints, &server);
int sockfd = socket(server->ai_family, server->ai_socktype| SOCK_NONBLOCK, server->ai_protocol);

The change is to take SOCK_NONB from the call to getaddrinfo and move it to socket.