The MagPi Issue 11 En

Embed Size (px)

Citation preview

  • 7/30/2019 The MagPi Issue 11 En

    1/36

    IISSSSUUEE 1111 -- AAPPRR 22001133

    AA

    MM

    aaggaazziinneeffoorrRRaassppbbeerrrryyPPiiUUsseerrss

    hh tt tt pp ::////ww ww ww .. tt hh ee mm aa gg pp ii .. cc oo mmRaspberry Pi is a trademark of The Raspberry Pi Foundation.This magazine was created using a Raspberry Pi computer.

    GGeett pprriinntteedd ccooppiieess aatt

    tthheemmaaggppii..ccoomm

    W i n a

    l i m i t e d e d i t i o n

    B L U E

    R a s p b e r r y P i !

    TThhiiss IIssssuuee......

    CCoonnttrrooll YYoouurr HHeeaattiinngg SSyysstteemm

    MMaakkee AA WWiirreelleessss PPii--PPooiinntt

    PPrriinntt FFrroomm YYoouurr PPii

    CCrreeaattee AAnn IInnttrraanneett.. ....aanndd mmuucchh mmoorree

    HHooww ttoo IInnssttaall ll ,, PPllaayy aanndd PPrrooggrraamm

  • 7/30/2019 The MagPi Issue 11 En

    2/36

  • 7/30/2019 The MagPi Issue 11 En

    3/363

    4

    H O M E H E A T I N G S Y S T E M

    C o n t r o l a n d m o n i t o r y o u r h o m e h e a t i n g s y s t e m w i t h a R a s p b e r r y P i a n d a S m a r t p h o n e

    6

    A c o n s t r u c t i o n a l p r o j e c t f o r t h e h o b b y i s t w h o i s c o n f i d e n t w i t h a s o l d e r i n g i r o n

    9

    T H I S M O N T H ' S P C S U P P L I E S C O M P E T I T I O N

    W i n a 5 1 2 m b R a s p b e r r y P i m o d e l B , f r o m P C S u p p l i e s i n t h e U K

    1 0

    W I F I A C C E S S P O I N T

    T u r n y o u r R a s p b e r r y P i i n t o a W i r e l e s s P i - P o i n t

    1 5

    T H I S M O N T H ' S E V E N T S G U I D E

    Y o r k s h i r e , B e r m u d a , G e n e v a , B r i s t o l - w h a t ' s h a p p e n i n g i n y o u r a r e a

    1 6

    M I N E C R A F T : P I E D I T I O N

    I n s t a l l i n g a n d m o d i f y i n g M i n e c r a f t o n t h e R a s p b e r r y P i w i t h P y t h o n

    2 0

    C O N F I G U R I N G P R I N T E R S

    A n i n t r o d u c t o r y g u i d e t o s e t t i n g u p a p r i n t e r w i t h C U P S

    2 1

    Q U I C K 2 W I R E C O M P E T I T I O N

    W i n a f u l l s e t o f Q u i c k 2 W i r e k i t s

    2 3

    S I M P L E I N T R A N E T

    L e a r n h o w t o c o n f i g u r e y o u r o w n s i m p l e i n t r a n e t

    2 5

    A S S E M B L Y P R O G R A M M I N G W I T H R I S C O S

    L e a r n h o w t o p r o g r a m t h e R a s p b e r r y P i b y u s i n g A s s e m b l y L a n g u a g e

    2 8

    A N I N T R O D U C T I O N T O C H A R M - P A R T 2

    A n i n t r o d u c t i o n t o C h a r m D a t a T y p e s

    3 2

    C O N S O L E C O L O U R S

    C o n t r o l c o n s o l e c o l o u r s u s i n g e s c a p e s e q u e n c e s

    3 3

    W I N Y O U R O W N B L U E P I

    A c o m p e t i t i o n w i t h R S C o m p o n e n t s

    3 4

    S C R A T C H P A T C H - H E A P S O R T

    S o r t a h e a p o f n u m b e r s u s i n g S c r a t c h

    P O W E R A N D I / O E X P A N S I O N B O A R D

    h t t p : / / w w w . t h e m a g p i . c o m

    C o n t e n t s C o n t e n t s C o n t e n t s

  • 7/30/2019 The MagPi Issue 11 En

    4/36

    D I F F I C U L T Y : A D V A N C E D

    N i k o l a o s T s i p a s

    G u e s t W r i t e r

    C o n t r o l a n d M o n i t o r Y o u r H o m e

    H e a t i n g S y s t e m w i t h a

    R a s p b e r r y P i a n d a S m a r t p h o n e

    I n t r o d u c t i o n

    I n t h i s a r t i c l e , w e e x p l o r e t h e p o s s i b i l i t i e s o f u s i n g

    a R a s p b e r r y P i f o r t h e r e m o t e c o n t r o l a n d

    m o n i t o r i n g o f a h o m e h e a t i n g s y s t e m u s i n g :

    s e r v o c o n t r o l , a n a l o g t o d i g i t a l c o n v e r s i o n ,

    C p r o g r a m m i n g a n d w e b d e v e l o p m e n t .

    S e r v o C o n t r o l

    W e d e c i d e d t o u s e a s e r v o d e v i c e t o c o n t r o l t h e

    o n / o f f s w i t c h o f t h e b o i l e r . W e c o u l d h a v e u s e d

    r e l a y s w i t c h e s f o r t h e s a m e p u r p o s e , b u t t r i e d t o

    s t i c k t o t h e p r i n c i p l e o f m a k i n g n o m o d i f i c a t i o n s

    t o t h e b o i l e r ( a s i t s o w n e d b y t h e l a n d l o r d : D ) .

    T h e f o l l o w i n g i m a g e s h o w s h o w t h e s e r v o i s

    m o u n t e d a n d t h e v a r i o u s s t a t e s o f c o n t r o l .

    O u r m i n i s e r v o h a s r e d u c e d p o w e r r e q u i r e m e n t s ,

    s o w e c o u l d p o w e r i t d i r e c t l y f r o m t h e 5 V p i n o f

    t h e R a s p b e r r y P i . T o c r e a t e t h e n e e d e d p u l s e f o r

    s e r v o c o n t r o l , w e u t i l i z e d w i r i n g P i l i b r a r y ' s

    s o f t P W M i m p l e m e n t a t i o n . S m a l l g l i t c h e s e x i s t ,

    b u t t h e a c c u r a c y i s s u f f i c i e n t f o r t h i s k i n d o f

    c o n t r o l . T h e c o d e b e l o w u s e s G P I O p i n 0 a s a

    P W M s i g n a l o u t p u t t o c o n t r o l t h e s e r v o . W e

    a s s u m e t h a t w i r i n g P i l i b r a r y i s a l r e a d y i n s t a l l e d .

    T h e " c o n t r o l " v a r i a b l e a c c e p t s i n t e g e r v a l u e s i n

    t h e r a n g e o f 1 8 0 t o 1 9 4 . T h e i n p u t t o t h a t

    v a r i a b l e w i l l c o n t r o l t h e t h r e e p o s i t i o n s o f t h e

    s w i t c h . T h i s w i l l e v e n t u a l l y b e c o n t r o l l e d f r o m a

    w e b b a s e d u s e r i n t e r f a c e .

    S e n s o r s a n d A D C

    O u r s e r v o d e v i c e c a n n o w c o n t r o l t h e s t a t e o f t h e

    b o i l e r f r o m o u r R a s p b e r r y P i . H o w e v e r , w h e n w e

    r e m o t e l y c o n t r o l t h e s y s t e m , w e c a n n o t b e s u r e

    t h a t t h e s e r v o w o r k e d a s i t s h o u l d a n d t h a t t h e

    b o i l e r i s i n t h e s t a t e w e w a n t . F o r t h i s r e a s o n , w e

    n e e d a w a y t o g e t f e e d b a c k f r o m t h e R a s p b e r r y

    P i r e g a r d i n g t h e b o i l e r s s t a t e . T h e m o s t s t r a i g h t -

    f o r w a r d w a y t o a c h i e v e t h i s i s t o c h e c k t h e s t a t u s

    o f t h e L E D i n d i c a t o r o n t h e b o i l e r . W h e n t h e L E D

    i s o n , t h e b o i l e r i s t u r n e d o n , a n d t h e L E D i s o f f ,

    w h e n t h e b o i l e r i s o f f . A s i m p l e L D R ( L i g h t

    D e p e n d e n t R e s i s t o r ) l i g h t s e n s o r i n a d a r k b o x ,

    m o u n t e d o n t h e b o i l e r s t w o 7 s e g m e n t L E D

    d i s p l a y s s h o u l d d o t h e j o b . A L D R s e n s o r i s a

    v a r i a b l e r e s i s t o r w h o s e r e s i s t a n c e d e p e n d s o n

    t h e a m o u n t o f l i g h t a r r i v i n g o n i t .

    pinMode(0, OUTPUT);

    digitalWrite(0, LOW);softPwmCreate(0,0,200);

    softPwmWrite(0,control);

  • 7/30/2019 The MagPi Issue 11 En

    5/365

    W e a l s o u s e t w o L M 3 5 t e m p e r a t u r e s e n s o r s w i t h

    a n a l o g u e o u t p u t ; o n e f o r a i r t e m p e r a t u r e ,

    m o u n t e d o n t h e b r e a d b o a r d a n d o n e f o r w a t e r

    t e m p e r a t u r e , m o u n t e d o n a r a d i a t o r .

    Y o u ' l l n e e d a n a n a l o g u e - t o - d i g i t a l c o n v e r t e r

    ( A D C ) w h e n u s i n g a n a l o g u e s e n s o r s . W e c h o s e

    a B V 4 2 0 5 , a 1 0 c h a n n e l I C e n a b l e d i c . T h e

    a n a l o g u e o u t p u t f r o m t h e t h r e e s e n s o r s g e t s

    c o n v e r t e d t o d i g i t a l a n d i s f e d t o t h e R a s p b e r r y

    P i t h r o u g h t h e I C b u s . H o w e v e r , d u e t o k n o w n

    i s s u e s w i t h t h e I C b u s o n t h e R a s p b e r r y P i , a

    " p a t c h " t o s l o w d o w n t h e c l o c k o f t h e I C b u s h a d

    t o b e a p p l i e d b e f o r e u s i n g t h e B V 4 2 0 5 .

    A l t e r n a t i v e l y , a m i c r o c o n t r o l l e r ( e . g . a t m e g a )

    c o u l d b e u s e d f o r t h e p u r p o s e o f a n A D C .

    B e l o w , w e u s e t h e m o s t b a s i c p a r t s o f t h e c o d e

    n e e d e d f o r c o m m u n i c a t i o n s o v e r t h e I C b u s t o

    r e a d t h e a n a l o g u e i n p u t o f a s e n s o r c o n n e c t e d t o

    c h a n n e l 7 . S i m i l a r c o d e c a n b e u s e d f o r

    i n t e r a c t i o n w i t h a n y I C c o m p a t i b l e d e v i c e .

    U s e r I n t e r f a c e

    T h e f o l l o w i n g s c h e m a t i c s h o w s t h e s e t u p a n d

    h o w t h e d e v i c e s c o n n e c t a n d i n t e r a c t . A s i m p l e

    w e b i n t e r f a c e p r o v i d e s e a s y a c c e s s f r o m w e b

    e n a b l e d d e v i c e s . T h e w e b i n t e r f a c e i s m a i n l y

    b a s e d o n j Q u e r y , P H P a n d M y S Q L .

    U s i n g P H P s e x e c c o m m a n d , w e g e t a c c e s s t o

    o u r t w o m a i n e x e c u t a b l e s t h a t c o n t r o l t h e s e r v o ' s

    p o s i t i o n a n d r e a d s e n s o r o u t p u t . A j a x h a s b e e n

    u s e d e x t e n s i v e l y i n o r d e r t o a c h i e v e r e a l - t i m e

    m o n i t o r i n g o f t h e b o i l e r s s t a t e a n d s e n s o r

    r e a d i n g s . F u r t h e r m o r e , w e c a l c u l a t e h o w l o n g

    t h e s y s t e m w a s o n e a c h d a y ( i n m i n u t e s ) b y

    a n a l y s i n g t h e l i g h t s e n s o r s o u t p u t . T h e u s e r i s

    a l s o p r e s e n t e d w i t h a n s v g ( d 3 . j s l i b r a r y ) g r a p h

    o f t h e a i r t e m p e r a t u r e f o r t h e l a s t 2 4 h o u r s w i t h

    t i m e r e s o l u t i o n o f 1 m i n u t e .

    B y e x p l o i t i n g d a t a f r o m t h e s e n s o r s , w e c o u l d

    f u r t h e r e x t e n d t h e s y s t e m a n d m a k e i t m o r e

    i n t e l l i g e n t b y f u l l y a u t o m a t i n g o n a n d o f f e v e n t s

    b a s e d o n s e n s o r o u t p u t a n d s p e c i f i c u s a g e

    p r o f i l e s . F o r m o r e i n f o r m a t i o n ( c o d e ,

    s c h e m a t i c s , p i c t u r e s ) a b o u t t h e p r o j e c t v i s i t :

    h t t p : / / p r o j e c t e d n e u r a l a c t i v i t y . b l o g s p o t . c o . u k /

    //Initialize I2C bus

    int fd;

    char *fileName = "/dev/i2c-1";

    //Note: When using a Rev 1 Pi, use

    "/dev/i2c-0"

    int address = 0x31;

    unsigned char buf[10];

    fd = open(fileName, O_RDWR);

    ioctl(fd, I2C_SLAVE, address);

    //Select ADC channel 7

    buf[0] = 1;

    buf[1] = 7;

    write(fd, buf, 2);

    //Start conversion

    buf[0] = 2;

    write(fd, buf, 1);

    //Fetch result of conversion

    buf[0] = 4;

    write(fd, buf, 1);

    read(fd, buf, 2);

    unsigned short packedword;

    packedword = (buf[0]

  • 7/30/2019 The MagPi Issue 11 En

    6/366

    D I F F I C U L T Y : A D V A N C E D

    L l o y d S e a t o n

    G u e s t W r i t e r

    P O W E R A N D I / O

    E X P A N S I O N B O A R D

    T h i s i s a c o n s t r u c t i o n a l p r o j e c t f o r t h e h o b b y i s t w h o i s

    c o n f i d e n t w i t h a s o l d e r i n g i r o n , l i k e s t o h a v e o p t i o n s

    a n d i s p r e p a r e d t o p u r c h a s e t h e i r o w n c o m p o n e n t s .

    F u n c t i o n a l i t y

    I n s p i t e o f i t s s m a l l s i z e , P o w e r I / O e n d o w s t h e

    R a s p b e r r y P i w i t h s u b s t a n t i a l G P I O i n t e r f a c i n g

    c a p a b i l i t i e s a n d f l e x i b i l e p o w e r s u p p l y

    a r r a n g e m e n t s :

    - 7 d i g i t a l o u t p u t s w i t h h i g h c u r r e n t d r i v e r s

    - 7 i n d i c a t o r L E D s a s s o c i a t e d w i t h t h e a b o v e

    - 5 d i g i t a l i n p u t s w i t h 1 6 V t o l e r a n c e

    - U A R T a n d I 2 C b u s p r o v i s i o n s

    - J u m p e r o p t i o n s f o r f l e x i b i l i t y

    - H e x a d e c i m a l r o t a r y s w i t c h

    - S w i t c h i n g m o d e p o w e r s u p p l y ( 7 ~ 2 0 V D C )

    F o r m F a c t o r

    A s c a n b e s e e n a b o v e , P o w e r I / O s i t s d i r e c t l y

    a b o v e t h e R a s p b e r r y P i a n d i n t e g r a t e s n e a t l y , s o

    t h a t t h e t w o u n i t s t o g e t h e r h a v e a m i n i m a l

    f o o t p r i n t a n d a t i d y a p p e a r a n c e .

    P r o v i s i o n i s m a d e f o r t h e p r i n t e d c i r c u i t b o a r d

    ( P C B ) t o h a v e a c u t - o u t t o c l e a r t h e R a s p b e r r y

    P i ' s v i d e o c o n n e c t o r i f t h e c o n s t r u c t o r c h o o s e s t o

    u s e a l o w p r o f i l e r e c e p t a c l e ( s o c k e t ) t o c o n n e c t

    w i t h t h e R a s p b e r r y P i . T h i s u n i t h a s i n s t e a d

    b e e n c o n s t r u c t e d u s i n g t h e p r e f e r r e d 3 M h i g h

    p r o f i l e s o c k e t s o t h a t n o c u t - o u t w a s r e q u i r e d

    a n d t h e r e i s p l e n t y o f c l e a r a n c e b e t w e e n t h e t w o

    p r i n t e d c i r c u i t a s s e m b l i e s ( P C A s ) t o e l i m i n a t e

    a n y r i s k o f a n e l e c t r i c a l s h o r t o r r e s t r i c t i o n o f a i r

    c i r c u l a t i o n a r o u n d t h e B r o a d c o m S o C o r e t h e r n e t

    c h i p .

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    " I n c l u d e , o m i t o r v a r y c o m p o n e n t s t o

    m a t c h y o u r i n d i v i d u a l p r e f e r e n c e s . "

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    P C B M a n u f a c t u r i n g

    T h e f i r s t t h i n g y o u m u s t d e c i d e i s h o w t o p r o c u r e

    y o u r p r i n t e d c i r c u i t b o a r d s ( P C B s ) a n d t h e r e a r e

    s e v e r a l w a y s , d e p e n d i n g o n y o u r a f f i l i a t i o n s a n d

    t h e i n t e r e s t s o f y o u a n d y o u r a f f i l i a t e s . T h i s

    p r o j e c t ' s P C B i s r e p r e s e n t e d i n 4 d i f f e r e n t

    c o c k t a i l f i l e s f r o m w h i c h y o u c a n c h o o s e .

    T h e f i l e , 2 x P i P o w e r + I O . p c b , s i m p l y h a s t w i n

    c o p i e s o f t h i s p r o j e c t s o t h a t , i f y o u u s e t h i s f i l e t o

    o r d e r y o u r P C B s t h r o u g h t h e E x p r e s s P C B . c o m

    M i n i B o a r d P r o s e r v i c e , e a c h o f t h e 3

  • 7/30/2019 The MagPi Issue 11 En

    7/367

    m a n u f a c t u r e d P C B s w i l l y i e l d 2 P C B s f o r t h i s

    p r o j e c t , t o b e s e p a r a t e d b y h a c k s a w . T h e r e f o r e ,

    t h e y i e l d f r o m a n o r d e r w i l l b e 6 P C B s f o r t h i s

    p r o j e c t .

    T h e f i l e , M e g a P o w e r I O . p c b , h a s 1 c o p y o f t h i s

    p r o j e c t ' s P C B t o g e t h e r w i t h 1 c o p y o f a P C B f o r

    t h e M e g a P o w e r p r o j e c t . T h e r e f o r e , a M i n i B o a r d

    P r o o r d e r t h r o u g h E x p r e s s P C B . c o m w i l l y i e l d 3

    P C B s f o r t h i s p r o j e c t a n d 3 P C B s f o r t h e

    M e g a P o w e r p r o j e c t , a f t e r s e p a r a t i o n .

    T h e f i l e , P o w e r Q u a r t e t . p c b , h a s 1 c o p y e a c h o f

    P C B s f o r : t h i s p r o j e c t , B a t t e r y L o a d M a n a g e r 8 5 ,

    B a t t e r y L o a d M a n a g e r + a n d P i B r i d g e I C S P

    I n t e r c o n n e c t . T h e r e f o r e , a M i n i B o a r d P r o o r d e r

    t h r o u g h E x p r e s s P C B . c o m w i l l y i e l d 3 P C B s f o r

    e a c h o f t h e s e p r o j e c t s , a f t e r s e p a r a t i o n .

    T h e f i l e , T i n y P o w e r I O . p c b , h a s 1 c o p y o f t h i s

    p r o j e c t ' s P C B a n d a c o p y o f a P C B f o r t h e T i n y

    I / O p r o j e c t . T h e r e f o r e , a M i n i B o a r d P r o o r d e r

    t h r o u g h E x p r e s s P C B . c o m w i l l y i e l d 3 P C B s f o r

    t h i s p r o j e c t a n d 3 P C B s f o r t h e T i n y I / O p r o j e c t ,

    a f t e r s e p a r a t i o n .

    A l l o f t h e p r o j e c t s i n t h e a b o v e c o c k t a i l f i l e s w e r e

    o u t l i n e d i n t h e I s s u e 1 0 a r t i c l e , " T r y a C o c k t a i l o f

    P r o j e c t s " . E x t e n s i v e d o c u m e n t a t i o n , p h o t o s a n d

    t e s t p r o g r a m s f o r a l l o f t h e s e p r o j e c t s a r e

    a v a i l a b l e o n t h e I n t e r n e t v i a t h e i n f o r m a t i o n b l o g

    a t h t t p : / / p i c o c k t a i l s . b l o g s p o t . c o m .

    O p t i o n s

    A n i m p o r t a n t a d v a n t a g e o f c o n s t r u c t i n g y o u r o w n

    e x p a n s i o n b o a r d i s t h a t y o u c a n o p t i o n a l l y

    i n c l u d e , o m i t o r v a r y c o m p o n e n t s t o m a t c h y o u r

    i n d i v i d u a l p r e f e r e n c e s .

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    " T h i s p r o j e c t c a l l s f o r r e a s o n a b l e

    p r o f i c i e n c y a t s o l d e r i n g "

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    T h e u n i t p i c t u r e d t o t h e r i g h t h a s b e e n

    d e l i b e r a t e l y a s s e m b l e d w i t h o u t t h e s w i t c h i n g

    p o w e r s u p p l y , b u t , i f c i r c u m s t a n c e s c h a n g e , t h e

    p o w e r s u p p l y c o m p o n e n t s c a n b e a d d e d l a t e r .

    T h i s u n i t h a s a c u t - o u t s e c t i o n a t t h e l o w e r l e f t ,

    n e c e s s i t a t e d b y t h e u s e o f a l o w - p r o f i l e s o c k e t

    i n s t e a d o f t h e p r e f e r r e d h i g h - p r o f i l e 3 M s o c k e t .

    A h e x a d e c i m a l l y e n c o d e d s w i t c h ( S W 1 ) c a n b e

    i n c l u d e d i f t h e i n t e n d e d a p p l i c a t i o n a p p r e c i a t e s

    i t s f u n c t i o n a l o r e d u c a t i o n a l v a l u e b u t i t h a s b e e n

    o m i t t e d f r o m t h e u n i t p i c t u r e d a b o v e f o r

    b u d g e t a r y o r f u n c t i o n a l r e a s o n s , r e p l a c e d b y a

    p a i r o f 3 - p i n h e a d e r s t h a t c a n b e j u m p e r e d t o

    m a t c h u s e r r e q u i r e m e n t s .

    C o n s t r u c t i o n

    T h i s p r o j e c t c a l l s f o r r e a s o n a b l e p r o f i c i e n c y a t

    s o l d e r i n g a s t h e c o m p o n e n t d e n s i t y i s r e l a t i v e l y

    h i g h , b u t a n e l a b o r a t e t o o l k i t i s n o t r e q u i r e d .

    Y o u r g r e a t e s t c o n s t r u c t i o n a s s e t s w i l l b e

    p l a n n i n g , c a r e , a n d p a t i e n c e .

    A t h t t p : / / p i c o c k t a i l s . b l o g s p o t . c o m , t h e r e i s a

    d e d i c a t e d p a g e ( I s s u e 1 1 ) w i t h l i n k s t o a l l o f t h e

    r e s o u r c e s r e l a t e d t o t h i s p r o j e c t . T h e r e i s a

    d e s i g n d e s c r i p t i o n d o c u m e n t w i t h s c h e m a t i c

    d i a g r a m , p i n - o u t t a b l e s , s u g g e s t e d c o m p o n e n t

    s u p p l i e r s , c o n s t r u c t i o n a n d o p e r a t i n g h i n t s .

    T h e r e a r e a l s o l i n k s t o t h e c o c k t a i l f i l e s , p h o t o

    a l b u m s , a n d a P y t h o n t e s t p r o g r a m . T h e b l o g

    p a g e a l s o p r o v i d e s a m e c h a n i s m f o r p u b l i c a t i o n

    o f a n y o t h e r i n f o r m a t i o n t h a t m a y b e r e q u i r e d f o r

    t h e p r o j e c t ' s o n - g o i n g s u p p o r t .

  • 7/30/2019 The MagPi Issue 11 En

    8/36

    http://shop.pimoroni.com/
  • 7/30/2019 The MagPi Issue 11 En

    9/36

    T o s e e t h e l a r g e r a n g e o f P C S L b r a n d R a s p b e r r y P i a c c e s s o r i e s v i s i t

    h t t p : / / w w w . p c s l s h o p . c o m

    March's Winners!T h e w i n n e r o f t h e n e w 5 1 2 M B R a s p b e r r y P i M o d e l B i s J o n a t h a n W a t t e r s ( E n f i e l d , U K ) .

    T h e 2 n d a n d 3 r d p r i z e w i n n e r s o f t h e P C S L L C D V E S A m o u n t c a s e a r e D e n n i s G r a y

    ( L o g a n , U t a h , U S A ) a n d R i c h a r d S t e v e n s o n ( P o r t s m o u t h , U K ) .

    C o n g r a t u l a t i o n s . W e w i l l b e e m a i l i n g y o u s o o n w i t h d e t a i l s o f h o w t o c l a i m y o u r p r i z e s !

    T h i s m o n t h t h e r e a r e t h r e e p r i z e s !

    T h e f i r s t p r i z e w i n n e r w i l l r e c e i v e a n e w

    5 1 2 M B R a s p b e r r y P i M o d e l B p l u s a

    P C S L R a s p b e r r y P i c a s e !

    T h e s e c o n d a n d t h i r d p r i z e w i n n e r s w i l l

    r e c e i v e a P C S L R a s p b e r r y P i c a s e .

    F o r a c h a n c e t o t a k e p a r t i n t h i s m o n t h ' s

    c o m p e t i t i o n v i s i t :

    h t t p : / / w w w . p c s l s h o p . c o m / i n f o / m a g p i

    C l o s i n g d a t e i s 2 0 t h A p r i l 2 0 1 3 .

    W i n n e r s w i l l b e n o t i f i e d i n n e x t m o n t h ' s

    m a g a z i n e a n d b y e m a i l . G o o d l u c k !

    O n c e a g a i n T h e M a g P i a n d P C S u p p l i e s L i m i t e d a r e p r o u d t o a n n o u n c e y e t

    a n o t h e r c h a n c e t o w i n s o m e f a n t a s t i c R a s p b e r r y P i g o o d i e s !

    APRIL COMPETITION

    9

    http://www.pcslshop.com/http://www.pcslshop.com/info/magpi
  • 7/30/2019 The MagPi Issue 11 En

    10/360

    D I F F I C U L T Y : A D V A N C E D

    G u y E a s t w o o d

    G u e s t W r i t e r

    W i F i A c c e s s P o i n t

    T u r n y o u r R a s p b e r r y P i i n t o a

    W i r e l e s s P i - P o i n t

    H o w t o c o n v e r t y o u r R a s p b e r r y P i i n t o a

    W i r e l e s s A c c e s s P o i n t u s i n g a s i m p l e

    W i F i U S B d o n g l e .

    Y o u w i l l n e e d

    - A R a s p b e r r y P i m o d e l B ( o f c o u r s e ! )

    - A n S D C a r d f o r y o u r P i I u s e 4 G B f o r t h i s

    b u t w h a t e v e r y o u h a v e s h o u l d b e g o o d

    - A U S B W i F i D o n g l e I u s e d a Z y X E L

    C o m m u n i c a t i o n s C o r p . Z y A I R G - 2 0 2 8 0 2 . 1 1 b g

    S e t t i n g u p y o u r S D C a r d

    F i r s t i n s t a l l R a s p b i a n f r o m t h e R a s p b e r r y P i s i t e

    a t t h e U R L b e l o w . A t t h e t i m e o f w r i t i n g t h e

    c u r r e n t v e r s i o n i s ' W h e e z y ' :

    I n s t a l l t h e i m a g e t o y o u r S D c a r d a s e x p l a i n e d

    h e r e :

    L o g g i n g i n t o y o u r P i

    L o g i n t o y o u r P i I s e t u p m i n e v i a S S H b u t n o

    r e a s o n w h y y o u c a n ' t d o i t v i a a k e y b o a r d a n d

    s c r e e n i f y o u h a v e i t c o n n e c t e d t h a t w a y . T h e

    d e f a u l t l o g i n i s u s e r n a m e ' p i ' w i t h p a s s w o r d

    ' r a s p b e r r y ' . Y o u ' l l b e w a n t i n g t o c h a n g e t h e s e

    l a t e r f o r s e c u r i t y . I f n o t l o g g i n g i n v i a S S H m a k e

    s u r e y o u h a v e a n e t w o r k c o n n e c t i o n w i t h i n t e r n e t

    a c c e s s .

    I f y o u ' r e u s i n g S S H t h e n y o u ' l l n e e d t o l o c a t e

    y o u r P i ' s I P a d d r e s s o n y o u r L A N u s i n g ( o n L i n u x

    s y s t e m s , a t l e a s t ) :

    W h i c h w i l l l i s t a l l I P ' s o n y o u r n e t w o r k ( i f y o u r

    n e t w o r k i s 1 9 2 . 1 6 8 . 1 . x t h e n c h a n g e t h e n e t w o r k

    r a n g e i n t h e c o m m a n d t o 1 9 2 . 1 6 8 . 1 . 0 / 2 4 a n d s o

    o n ) . Y o u ' l l g e t a l i s t a l o n g t h e l i n e s o f . . .

    Nmap scan report for UNKNOWN

    (192.168.0.54)Host is up (0.65s latency).

    MAC Address: 00:24:2C:12:62:55 (Hon Hai

    Precision Ind. Co.)

    Nmap scan report for UNKNOWN

    (192.168.0.55)

    Host is up (0.23s latency).

    MAC Address: B8:27:EB:9E:CA:4D

    (Raspberry Pi Foundation)

    Nmap scan report for UNKNOWN

    (192.168.0.57)Host is up (0.0036s latency).

    MAC Address: 00:A0:DE:86:D3:6B (Yamaha)

    http://www.raspberrypi.org/downloads

    http://elinux.org/RPi_Easy_SD_Card_Setup

    $ sudo nmap -sP 192.168.0.0/24

  • 7/30/2019 The MagPi Issue 11 En

    11/361 1

    I n t h i s c a s e t h e P i i s a t 1 9 2 . 1 6 8 . 0 . 5 5 s o I ' d l o g i n

    w i t h s s h p i @ 1 9 2 . 1 6 8 . 0 . 5 5 .

    C o n f i g u r i n g y o u r P i

    F i r s t l y r u n :

    i f y o u h a v e n ' t y e t , t o s e t u p y o u r P i ( y o u ' l l l i k e l y

    w a n t t o a t l e a s t c h a n g e y o u r m e m o r y s p l i t t o 1 6

    a n d t u r n o f f ' S t a r t d e s k t o p o n b o o t ? ' w h i c h y o u ' l l

    n o t b e n e e d i n g ) t h e n r e b o o t a n d s e t a r o o t

    p a s s w o r d u s i n g sudo passwd a n d e n t e r a d e c e n t

    ( i . e . n o t ' p a s s w o r d ' ! ) p a s s w o r d f o r t h e r o o t u s e r .

    F r o m t h i s p o i n t o n I ' l l a s s u m e y o u ' r e l o g g e d i n a s

    r o o t . I ' m a l s o a s s u m i n g y o u k n o w h o w t o u s e v i

    o r v i m c o n s o l e t e x t e d i t o r s ( o r m a y b e a n

    a l t e r n a t i v e l i k e n a n o ) .

    I n s t a l l i n g t h e n e c e s s a r y s o f t w a r e

    p a c k a g e s

    I p r e f e r u s i n g a p t i t u d e f o r p a c k a g e i n s t a l l s a n d

    w i l l b e u s i n g i t t h r o u g h o u t t h i s a r t i c l e i f y o u ' r e

    h a p p i e r u s i n g a p t - g e t t h e n y o u ' l l b e w a n t i n g t o

    a l t e r s o m e o f t h e i n s t a l l a t i o n c o m m a n d s t o s u i t .

    I n s t a l l a p t i t u d e w i t h :

    B r i n g y o u r P i u p t o d a t e u s i n g :

    T h i s m a y t a k e a l i t t l e w h i l e a n d a l s o d e p e n d o n

    y o u r i n t e r n e t c o n n e c t i o n s p e e d .

    Y o u ' l l n e e d t o i n s t a l l t h e s e p a c k a g e s :

    T h e s e a r e :

    r f k i l l - - W i r e l e s s u t i l i t y

    z d 1 2 1 1 - f i r m w a r e - - S o f t w a r e f o r d e a l i n g w i t h

    z d 1 2 1 1 b a s e d W i r e l e s s h a r d w a r e

    h o s t a p d - - T h e h o s t a p w i r e l e s s a c c e s s p o i n t

    d a e m o n

    h o s t a p - u t i l s T o o l s t h a t g o w i t h h o s t a p

    i w W i r e l e s s c o n f i g u t i l i t y

    d n s m a s q a D H C P a n d D N S u t i l i t y

    Y o u m a y a l s o w a n t t o a d d ' v i m ' t o t h a t l i s t w h i c h

    i s a n i c e r c o n s o l e e d i t o r t h a n t h e d e f a u l t ' v i ' .

    T h e z d 1 2 1 1 - f i r m w a r e p a c k a g e i s h a r d w a r e -

    s p e c i f i c b u t i s a v e r y c o m m o n i m p l e m e n t a t i o n f o r

    w i f i d o n g l e s . Y o u r d o n g l e w i l l a l s o n e e d t o

    s u p p o r t ' A P ' m o d e w h i c h y o u c a n v e r i f y b y

    t y p i n g :

    a n d u n d e r t h e ' S u p p o r t e d i n t e r f a c e m o d e s ' y o u

    w i l l h o p e f u l l y s e e ' A P ' l i s t e d a l o n g w i t h o t h e r s l i k e

    ' m a n a g e d ' a n d ' m o n i t o r ' . I f n o t y o u ' r e o u t o f l u c k

    w i t h y o u r p a r t i c u l a r d o n g l e , s o i f y o u h a v e

    a n o t h e r L i n u x m a c h i n e h a n d y y o u m a y w a n t t o

    p l u g t h e d o n g l e i n t o t h a t a n d c h e c k i t w i t h i w l i s t

    b e f o r e y o u s t a r t !

    C o n f i g u r i n g t h e w i r e l e s s i n t e r f a c e

    Y o u r w i r e l e s s i n t e r f a c e m u s t b e s e t s t a t i c a l l y f o r

    h o s t a p , e d i t y o u r / e t c / n e t w o r k / i n t e r f a c e s f i l e t o

    l o o k l i k e t h i s :

    $ sudo raspi-config

    $ aptitude update; aptitude safe-upgrade

    auto lo

    iface lo inet loopback

    iface eth0 inet dhcp

    iface wlan0 inet static

    address 192.168.1.1

    netmask 255.255.255.0

    $ aptitude install rfkill zd1211-

    firmware hostapd hostap-utils iw dnsmasq

    $ apt-get install aptitude

    $ iw list

  • 7/30/2019 The MagPi Issue 11 En

    12/362

    T h e n r e s t a r t y o u r w i r e l e s s i n t e r f a c e u s i n g :

    w h i c h s h o u l d h o p e f u l l y g o s m o o t h l y w i t h o u t

    e r r o r s . T h e a d d r e s s o f 1 9 2 . 1 6 8 . 1 . 1 s h o u l d n o t b e

    t h e s a m e a s t h e n e t w o r k c o n n e c t e d t o e t h 0 , m y

    m a i n L A N i s 1 9 2 . 1 6 8 . 0 . 0 / 2 4 a n d t h e w i r e l e s s

    i n t e r f a c e s h o u l d b e a d i f f e r e n t n e t w o r k . T h i s

    a d d s a s i m p l e l a y e r o f s e c u r i t y i n c a s e y o u ' r e

    c o n f i g u r i n g a n o p e n a c c e s s p o i n t a s I w a s a n d

    a l l o w s y o u t o f i r e w a l l o n e n e t w o r k f r o m t h e o t h e r

    f a r m o r e e a s i l y .

    C o n f i g u r i n g t h e A c c e s s P o i n t

    N o w t o c o n f i g u r e h o s t a p . E d i t

    / e t c / h o s t a p d / h o s t a p . c o n f ( i t m a y n o t a l r e a d y e x i s t

    b u t t h i s w i l l c r e a t e i t , a n y w a y ) t o l o o k l i k e t h i s :

    T h e s e t t i n g s a r e p r e t t y o b v i o u s , ' d r i v e r ' b e i n g t h e

    o n l y e x c e p t i o n , j u s t l e a v e i t a s i t i s b u t c h a n g e

    t h e o t h e r v a l u e s a s y o u s e e f i t , t h o u g h t h e s e

    s h o u l d d o f o r a n i n i t i a l t e s t . O n e t h i n g t o b e a r i n

    m i n d i s t h a t h o s t a p s e e m s t o b e v e r y l i t e r a l a b o u t

    r e a d i n g i t s c o n f i g u r a t i o n f i l e s o m a k e s u r e y o u

    h a v e n o t r a i l i n g s p a c e s o n t h e e n d o f a n y l i n e s !

    R e s t a r t t h e h o s t a p d s e r v i c e w i t h :

    A l l b e i n g w e l l y o u s h o u l d n o w s e e ' t e s t ' a s a

    w i r e l e s s n e t w o r k , t h o u g h a l i t t l e m o r e w o r k n e e d s

    t o b e d o n e t o c o n n e c t t o i t y e t .

    C o n f i g u r i n g t h e D H C P s e r v e r

    T h e f i n a l s t e p i s t o c o n f i g u r e d n s m a s q s o y o u

    c a n o b t a i n a n I P a d d r e s s f r o m y o u r n e w P i - P o i n t .

    E d i t y o u r / e t c / d n s m a s q . c o n f f i l e t o l o o k l i k e t h i s :

    R e m e m b e r t o c h a n g e t h e d h c p - r a n g e o p t i o n f o r

    t h e n e t w o r k I P r a n g e y o u ' r e u s i n g . I f y o u ' r e

    h a v i n g p r o b l e m s w i t h W i n d o w s 7 m a c h i n e s t h e n

    t r y u n c o m m e n t i n g t h e l a s t l i n e . R e s t a r t d n s m a s q

    b y t y p i n g :

    f o r t h e c o n f i g u r a t i o n t o t a k e e f f e c t . N o w y o u

    s h o u l d b e a b l e t o c o n n e c t t o y o u r n e w P i - P o i n t

    a n d g e t a p r o p e r I P a d d r e s s f r o m i t .

    N o w y o u r P i - P o i n t i s u p a n d r u n n i n g t h e r e ' s a f e w

    t h i n g s t o t a k e c a r e o f . Y o u ' l l p r o b a b l y h a v e

    n o t i c e d t h a t y o u c a n ' t l o a d G o o g l e o r a n y t h i n g

    e l s e . T h a t ' s b e c a u s e a l t h o u g h y o u c a n c o n n e c t

    t o t h e P i - P o i n t a n d g e t a n I P a d d r e s s f r o m i t , i t ' s

    n o t y e t r o u t i n g y o u r r e q u e s t s a c r o s s y o u r

    n e t w o r k a n d o u t o n t o t h e i n t e r n e t . W e ' l l a d d r e s s

    t h i s n e x t .

    C o n f i g u r e t h e n e t w o r k r o u t i n g

    T h e f i r s t t h i n g y o u n e e d t o d o i s t o a l l o w

    f o r w a r d i n g i n t h e P i - P o i n t , t h i s i s a c h i e v e d b y

    i s s u i n g t h e c o m m a n d :

    t o t u r n f o r w a r d i n g o n ( N . B . y o u c a n a l s o

    p e r m a n e n t l y s e t t h e i p _ f o r w a r d v a l u e a s a o n e -

    o f f i n / e t c / s y s c t l . c o n f b y u n c o m m e n t i n g t h e

    a p p r o p r i a t e l i n e ) . N o w y o u r P i - P o i n t i s f o r w a r d i n g

    $ ifdown wlan0; ifup wlan0

    interface=wlan0

    driver=nl80211

    ssid=test

    channel=1

    # Only listen for DHCP on wlan0

    interface=wlan0

    # create a domain if you want, comment

    #it out otherwise

    #domain=Pi-Point.co.uk

    # Create a dhcp range on your /24 wlan0

    #network with 12 hour lease time

    dhcp-range=

    192.168.1.5,192.168.1.254,255.255.255.0,

    12h

    # Send an empty WPAD option. This may be

    #REQUIRED to get windows 7 to behave.

    #dhcp-option=252,"\n"

    $ echo 1 > /proc/sys/net/ipv4/ip_forward

    $ service dnsmasq restart

    $ service hostapd restart

    # Never forward plain names (without a

    #dot or domain part)domain-needed

  • 7/30/2019 The MagPi Issue 11 En

    13/361 3

    $ iptables -t nat -A POSTROUTING -j

    MASQUERADE

    p a c k e t s y o u n e e d i t t o p r o v i d e N A T b e t w e e n

    y o u r w i f i n e t w o r k a n d y o u r m a i n n e t w o r k w h i c h i s

    a t t a c h e d t o t h e i n t e r n e t :

    Y o u s h o u l d n o w h a v e a w o r k i n g P i - P o i n t w h i c h

    w i l l a l l o w u s e r s t o c o n n e c t w i r e l e s s l y a n d u s e t h e

    i n t e r n e t v i a y o u r i n t e r n e t c o n n e c t i o n !

    M a k i n g s u r e i t r e b o o t s a s a w o r k i n g

    A c c e s s P o i n t

    T o e n s u r e y o u r P i - P o i n t w o r k s f r o m a r e b o o t

    r e m e m b e r y o u ' v e d o n e a l l t h i s i n s i d e y o u r c u r r e n t

    l o g i n s e s s i o n w i t h c o n s o l e c o m m a n d s - y o u ' l l

    n e e d t o c r e a t e a r u n f i l e t o t u r n o n f o r w a r d i n g ,

    N A T a n d r u n h o s t a p a t b o o t t i m e . C r e a t e a f i l e

    / e t c / i n i t . d / p i p o i n t w i t h t h e s e c o n t e n t s , k e e p a l l t h e

    c o m m e n t e d l i n e s , t o o , a s t h e s e a r e u s e d b y t h e

    s t a r t u p p r o c e s s e s :

    N e x t m a k e t h e s c r i p t e x e c u t a b l e w i t h :

    N e x t a d d t h e s c r i p t t o t h e s t a r t u p s e q u e n c e

    u s i n g :

    T h i s s h o u l d e n s u r e y o u r P i - P o i n t w i l l r e b o o t a s a

    f u n c t i o n i n g w i f i a c c e s s p o i n t .

    W h e r e n e x t ?

    Y o u c o u l d t r y i n s t a l l i n g S Q U I D p r o x y t o r e d u c e

    b a n d w i d t h f r o m w i f i u s e r s a c r o s s y o u r n e t w o r k o r

    e v e n u s e a P i - P o i n t i n p l a c e o f b u y i n g a w i f i

    r o u t e r t o c r e a t e a p r i v a t e g a m i n g n e t w o r k

    u n c o n n e c t e d t o y o u r m a i n L A N w h i c h g a m e

    p l a y e r s ( e . g . M i n e c r a f t ) c o u l d c o n n e c t t o a s a n

    i s o l a t e d w i r e l e s s n e t w o r k .

    A b o u t t h e A u t h o r

    G u y E a s t w o o d i s a w e b d e v e l o p m e n t

    p r o f e s s i o n a l ( m o s t o f t h e t i m e ) t h e s e d a y s a n d

    O p e n S o u r c e a d v o c a t e , h a v i n g c u t h i s t e e t h o n

    p r e t t y m u c h e v e r y t h i n g f r o m Z X 8 1 B A S I C t o

    S h a r c A s s e m b l e r D S P a u d i o p l u g i n s f o r s o u n d

    c a r d s . H e f i l l s s p a r e t i m e m u c k i n g a b o u t w i t h

    A n d r o i d s t u f f , R a s p b e r r y P i c o n f i g u r a t i o n s ,

    P l a y s t a t i o n 3 a n d d o i n g a s h e s t o l d ( p r o m p t l y ) b y

    h i s c u l i n a r y g e n i u s w i f e .

    F o r f u r t h e r d e t a i l s v i s i t h t t p : / / w w w . p i - p o i n t . c o . u k

    #!/bin/sh

    # Configure Wifi Access Point.

    #

    ### BEGIN INIT INFO# Provides: WifiAP

    # Required-Start: $remote_fs $syslog

    $time

    # Required-Stop: $remote_fs $syslog

    $time

    # Should-Start: $network $named

    slapd autofs ypbind nscd nslcd

    # Should-Stop: $network $named

    slapd autofs ypbind nscd nslcd

    # Default-Start: 2# Default-Stop:

    # Short-Description: Wifi Access Point

    configuration

    # Description: Sets forwarding,

    starts hostap, enables NAT in iptables

    ### END INIT INFO

    # turn on forwarding

    echo 1 > /proc/sys/net/ipv4/ip_forward

    # enable NAT

    iptables -t nat -A POSTROUTING -j

    MASQUERADE

    # start the access point

    hostapd -B /etc/hostapd/hostapd.conf

    $ update-rc.d pipoint start 99 2

    $ chmod +x /etc/init.d/pipoint

    http://www.pi-point.co.uk/
  • 7/30/2019 The MagPi Issue 11 En

    14/364

    http://learn.adafruit.com/category/raspberry-pihttp://learn.adafruit.com/category/raspberry-pi
  • 7/30/2019 The MagPi Issue 11 En

    15/36

    W a n t t o k e e p u p t o d a t e w i t h a l l t h i n g s R a s p b e r r y P i i n y o u r a r e a ?

    T h e n t h i s s e c t i o n o f t h e M a g P i i s f o r y o u ! W e a i m t o l i s t R a s p b e r r y J a m e v e n t s i n y o u r a r e a ,

    p r o v i d i n g y o u w i t h a R a s p b e r r y P i c a l e n d a r f o r t h e m o n t h a h e a d .

    A r e y o u i n c h a r g e o f r u n n i n g a R a s p b e r r y P i e v e n t ? W a n t t o p u b l i c i s e i t ?

    E m a i l u s a t : e d i t o r @ t h e m a g p i . c o m

    Y o r k s h i r e R I S C O S S h o w

    W h e n : S a t u r d a y 2 0 t h A p r i l 2 0 1 3 @ 1 0 : 3 0 a m

    W h e r e : T h e C e d a r C o u r t H o t e l , W a k e f i e l d , W e s t Y o r k s h i r e , W F 4 3 Q Z

    T h i s s h o w w i l l r u n f r o m 1 0 : 3 0 a m u n t i l 4 : 3 0 p m . F u r t h e r i n f o r m a t i o n i s a v a i l a b l e a t

    h t t p : / / w w w . w a k e f i e l d s h o w . o r g . u k / i n d e x . p h p

    B e r m u d a R a s p b e r r y J a m

    W h e n : F i r s t T u e s d a y o f e a c h m o n t h @ 6 : 0 0 p m

    W h e r e : A d m i r a l t y H o u s e , P e m b r o k e , B e r m u d a

    T h e m e e t i n g w i l l r u n f r o m 6 : 0 0 p m u n t i l 8 : 0 0 p m . F u r t h e r i n f o r m a t i o n i s a v a i l a b l e a t

    h t t p : / / b e r m u d a r a s p b e r r y u s e r g r o u p . t e a m s n a p . c o m o r e m a i l r p i @ s a i n i b . c o m

    G e n e v a R a s p b e r r y P i a t F t o n s L I N U X

    W h e n : S a t u r d a y 2 7 t h A p r i l 2 0 1 3 @ 1 : 0 0 p m

    W h e r e : H E P I A , r u e d e l a P r a i r i e 4 , G e n v e , S u i s s e

    T h e m e e t i n g w i l l r u n f r o m 1 : 0 0 p m t i l l 7 p m .

    F u r t h e r i n f o r m a t i o n i s a v a i l a b l e a t h t t p : / / w w w . f e t o n s - l i n u x . c h /

    B r i s t o l R a s p b e r r y P i @ B r i s t o l B o o t C a m p

    W h e n : S a t u r d a y 2 0 t h A p r i l 2 0 1 3 @ 1 0 : 3 0 a m

    W h e r e : @ B r i s t o l , A n c h o r R o a d , B r i s t o l , B S 1 5 D B

    T h e e v e n t w i l l r u n f r o m 1 0 : 3 0 a m t i l l 4 : 3 0 p m . P r e - r e g i s t r a t i o n r e q u i r e d .

    F u r t h e r i n f o r m a t i o n i s a v a i l a b l e a t h t t p s : / / b c s b r i s t o l b o o t c a m p . e v e n t b r i t e . c o m /

    1 5

    http://bermudaraspberryusergroup.teamsnap.com/http://wakefieldshow.org.uk/index.phphttp://fetons-linux.ch/https://bcsbristolbootcamp.eventbrite.com/
  • 7/30/2019 The MagPi Issue 11 En

    16/366

    D I F F I C U L T Y : I N T E R M E D I A T E

    M a r t i n O ' H a n l o n

    G u e s t W r i t e r

    M i n e c r a f t : P i E d i t i o n

    T h e G a m e

    M i n e c r a f t i s a g a m e w h i c h h a s a c h i e v e d m o n u m e n t a l

    s u c c e s s - a l m o s t 3 0 m i l l i o n c o p i e s , a c r o s s a l l i t s v e r s i o n s ,

    h a v e b e e n s o l d ; n o t b a d f o r a g a m e w h i c h d o e s n t r e a l l y

    h a v e a p o i n t ! I t i s c l a s s i f i e d a s a n I n d i e S a n d b o x g a m e , b u t

    i f i t d o e s h a v e a p o i n t , i t i s t o m a k e s t u f f ( a n d p e o p l e h a v e

    r e a l l y m a d e s t u f f ) , f r o m f u l l y f u n c t i o n i n g c o m p u t e r s t o s c a l e

    m o d e l s o f t h e s t a r s h i p E n t e r p r i s e .

    N o w M i n e c r a f t h a s c o m e t o t h e P i a n d t h e t w o b e s t t h i n g s

    a b o u t M i n e c r a f t : P i E d i t i o n a r e i t i s f r e e a n d c o m e s w i t h a n

    A P I - t w o t h i n g s y o u d o n o t g e t w i t h a n y o t h e r v e r s i o n o f

    M i n e c r a f t ( c e r t a i n l y n o t y e t a n y w a y ) .

    I n s t a l l i n g

    I f y o u h a v e n t i n s t a l l e d M i n e c r a f t a l r e a d y h e a d o v e r t o

    p i . m i n e c r a f t . n e t t o d o w n l o a d a n d v i e w t h e l a t e s t i n s t a l l a t i o n

    i n s t r u c t i o n s A t t h e t i m e o f w r i t i n g t h i s w a s t h e c u r r e n t

    m e t h o d :

    1 ) D o w n l o a d & E x t r a c t

    cd ~wget

    https://s3.amazonaws.com/assets.minecraft.net/pi/minecraft-pi-0.1.1.tar.gz

    tar -zxvf minecraft-pi-0.1.1.tar.gz

    2 ) R u n

    cd mcpi./minecraft-pi

    P l a y i n g

    T h e P i e d i t i o n o f M i n e c r a f t i s a c u t d o w n v e r s i o n o f t h e

    p o c k e t e d i t i o n a n d c u r r e n t l y o n l y o f f e r s o n e p l a y i n g m o d e ,

    C l a s s i c , w h i c h i s a l l a b o u t e x p l o r i n g a n d b u i l d i n g . C l i c k

    ' c r e a t e w o r l d ' a n d s t a r t e x p l o r i n g a n d b u i l d i n g .

    Y o u c o n t r o l y o u r p l a y e r w i t h t h e m o u s e a n d k e y b o a r d :

    M o v i n g - t h e m o u s e c h a n g e s w h e r e y o u l o o k , w h i l e

    t h e a r r o w k e y s m o v e y o u f o r w a r d , b a c k w a r d , l e f t

    a n d r i g h t .

    U p a n d d o w n - p r e s s i n g s p a c e w i l l m a k e y o u j u m p

    w h i l e y o u a r e w a l k i n g . D o u b l e t a p p i n g w i l l s t a r t y o u

    f l y i n g . W h i l e y o u a r e f l y i n g , s p a c e w i l l m a k e y o u

    m o v e u p a n d s h i f t w i l l m a k e y o u g o d o w n .

    I n v e n t o r y - y o u c a n c y c l e t h r o u g h y o u r i n v e n t o r y

    u s i n g t h e m o u s e s c r o l l w h e e l ( o r k e y s 1 - 8 ) a n d

    c h a n g e t h e b l o c k s i n y o u r i m m e d i a t e i n v e n t o r y ( t h e

    s t r i p a l o n g t h e b o t t o m ) b y p r e s s i n g E w h i c h b r i n g s

    u p a l l t h e b l o c k s y o u c a n u s e .

  • 7/30/2019 The MagPi Issue 11 En

    17/361 7

    D e s t r o y b l o c k s - p r e s s ( h o l d ) t h e l e f t m o u s e b u t t o n .

    P l a c e b l o c k s - p r e s s t h e r i g h t m o u s e b u t t o n .

    B a c k t o M e n u - p r e s s e s c a p e .

    T h e A P I

    T h e A P I a l l o w s y o u t o w r i t e p r o g r a m s w h i c h c o n t r o l , a l t e r

    a n d i n t e r a c t w i t h t h e M i n e c r a f t w o r l d , u n l o c k i n g a w h o l e

    l o a d o f M i n e c r a f t h a c k i n g . H o w a b o u t c r e a t i n g m a s s i v e

    s t r u c t u r e s a t t h e c l i c k o f a b u t t o n , a b r i d g e w h i c h

    a u t o m a t i c a l l y a p p e a r s u n d e r y o u r f e e t a l l o w i n g y o u t o w a l k

    a c r o s s m a s s i v e c h a s m s , a g a m e o f m i n e s w e e p e r o r a

    h u g e c l o c k ?

    M i n e c r a f t i s a w o r l d o f c u b e s

    o r b l o c k s , a l l w i t h a r e l a t i v e

    s i z e o f 1 m x 1 m x 1 m . E v e r y

    b l o c k h a s a p o s i t i o n i n t h e

    w o r l d o f x , y , z . x a n d z a r e t h e

    h o r i z o n t a l p o s i t i o n s a n d y i s

    t h e v e r t i c a l .

    T h e A P I w o r k s b y c h a n g i n g t h e s e r v e r , w h i c h r u n s

    u n d e r n e a t h t h e g a m e , a l l o w i n g y o u t o i n t e r a c t w i t h t h e s e

    b l o c k s a n d t h e p l a y e r , s u c h a s :

    G e t t h e p l a y e r s p o s i t i o n .

    C h a n g e ( o r s e t ) t h e p l a y e r s p o s i t i o n .

    G e t t h e t y p e o f b l o c k .

    C h a n g e a b l o c k .

    C h a n g e t h e c a m e r a a n g l e .

    P o s t m e s s a g e s t o t h e p l a y e r .

    L i b r a r i e s

    Y o u c a n i n t e r a c t w i t h t h e s e r v e r d i r e c t l y b y s e n d i n g a

    m e s s a g e t o i t , b u t t h e n i c e p e o p l e a t M o j a n g a l s o p r o v i d e

    l i b r a r i e s f o r P y t h o n a n d J a v a w h i c h s i m p l i f y a n d

    s t a n d a r d i s e u s i n g t h e A P I .

    T h e l i b r a r i e s a r e i n s t a l l e d a l o n g w i t h t h e g a m e i n t h e

    ~ / m c p i / a p i / j a v a a n d ~ / m c p i / a p i / p y t h o n d i r e c t o r i e s .

    T h e f o l l o w i n g e x a m p l e i s w r i t t e n i n P y t h o n a n d u s e s

    M o j a n g s P y t h o n A P I l i b r a r y . T h e f i r s t t a s k i s t o c r e a t e a

    d i r e c t o r y f o r y o u r A P I p r o g r a m a n d c o p y t h e P y t h o n l i b r a r y

    t o i t .

    A P I E x a m p l e

    1 ) C r e a t e D i r e c t o r y

    mkdir ~/minecraft-magpi

    2 ) C o p y P y t h o n L i b r a r y

    W e w i l l b e u s i n g t h e p y t h o n a p i l i b r a r y s u p p l i e d w i t h t h e

    g a m e .

    cp -r ~/mcpi/api/python/mcpi ~/minecraft-magpi/minecraft

    3 ) C r e a t e p r o g r a m

    O p e n I d l e ( o r y o u r f a v o u r i t e e d i t o r ) a n d c r e a t e a p r o g r a m

    f i l e c a l l e d minecraft-magpi.py

    i n t h e ~/minecraft-

    magpi d i r e c t o r y .

    W e a r e g o i n g t o n e e d 3 m o d u l e s : t h e m i n e c r a f t . p y a n d

    b l o c k . p y m o d u l e s f r o m t h e m i n e c r a f t d i r e c t o r y c o n t a i n i n g

    t h e l i b r a r y w e j u s t c o p i e d a n d t h e s t a n d a r d t i m e m o d u l e s o

    w e c a n i n t r o d u c e d e l a y s i n t o o u r p r o g r a m .

    import minecraft.minecraft as minecraftimport minecraft.block as blockimport time

    N e x t , w e n e e d t o u s e t h e M i n e c r a f t c l a s s i n t h e P y t h o n

    l i b r a r y t o c r e a t e a c o n n e c t i o n t o t h e g a m e s s e r v e r . W e w i l l

    u s e t h i s o b j e c t t o i n t e r a c t w i t h t h e g a m e a n d p r o v i d e

    a c c e s s t o a l l t h e f u n c t i o n s . W h e n y o u r p r o g r a m r u n s t h e

    f o l l o w i n g s t a t e m e n t M i n e c r a f t w i l l h a v e t o b e r u n n i n g a n d

    y o u w i l l n e e d t o b e i n a g a m e , o t h e r w i s e y o u w i l l g e t e r r o r s .

    mc = minecraft.Minecraft.create()

    U s i n g o u r m i n e c r a f t o b j e c t , m c , w e c a n t h e n i n t e r a c t w i t h

    t h e g a m e a n d s e n d t h e p l a y e r a m e s s a g e . W e w i l l a l s o p u t

    a d e l a y i n u s i n g t h e time.sleep()

    f u n c t i o n o t h e r w i s e t h e

    w h o l e p r o g r a m w i l l r u n t o o f a s t f o r u s t o s e e w h a t i s g o i n g

    o n .

    mc.postToChat("Hello Minecraft World")time.sleep(5)

  • 7/30/2019 The MagPi Issue 11 En

    18/368

    U s i n g t h e p r o g r a m b u i l t s o f a r , y o u c a n t e s t t o m a k e s u r e

    e v e r y t h i n g i s w o r k i n g . L o a d u p M i n e c r a f t a n d c r e a t e a ( o r

    e n t e r a n e x i s t i n g ) w o r l d . P r e s s A L T + T A B t o s w i t c h t o y o u r

    e d i t o r . I f y o u r e u s i n g I d l e s e l e c t ' R u n M o d u l e ' f r o m t h e R u n

    m e n u . I f a l l h a s b e e n s e t u p c o r r e c t l y y o u w i l l s e e t h e H e l l o

    M i n e c r a f t W o r l d m e s s a g e i n t h e g a m e .

    I n t e r a c t i n g w i t h t h e p l a y e r i s d o n e t h r o u g h t h e player

    c l a s s o f t h e mc

    o b j e c t a l l o w i n g u s t o f i n d a n d c h a n g e t h e

    p o s i t i o n o f t h e p l a y e r . T h e n e x t b l o c k o f c o d e f i n d s t h e

    p l a y e r ' s p o s i t i o n u s i n g t h e getPos()

    c o m m a n d , w h i c h

    r e t u r n s a n o b j e c t o f x , y , z c o o r d i n a t e s . T h e setPos()

    c o m m a n d i s t h e n u s e d t o m o v e t h e p l a y e r 5 0 b l o c k s u p , b y

    a d d i n g 5 0 t o t h e p l a y e r s y c o o r d i n a t e . W e t h e n a d d a

    d e l a y , s o t h e r e i s e n o u g h t i m e f o r y o u r p l a y e r t o f a l l d o w n

    t o t h e g r o u n d !

    playerPos = mc.player.getPos()

    mc.player.setPos(playerPos.x, playerPos.y +50, playerPos.z)

    mc.postToChat("Don't look down")time.sleep(5)

    Y o u c a n u s e t h e p o s i t i o n o f t h e p l a y e r a s a s t a r t i n g p o i n t

    f o r i n t e r a c t i n g w i t h b l o c k s . T h i s w a y y o u c a n f i n d o u t w h a t

    b l o c k t h e p l a y e r i s s t a n d i n g o n o r p l a c e b l o c k s a r o u n d t h e

    p l a y e r . T h e r e i s h o w e v e r a c h a l l e n g e a s t h e x , y , z

    c o o r d i n a t e s r e t u r n e d b y t h e getPos() f u n c t i o n a r e

    d e c i m a l s ( a k a f l o a t s ) a s t h e p l a y e r c a n b e i n t h e m i d d l e o f

    a b l o c k . T o i n t e r a c t w i t h b l o c k s w e n e e d t o u s e w h o l e

    n u m b e r s ( a k a i n t e g e r s ) , s o w e n e e d t o u s e t h e f u n c t i o n

    getTilePos() , w h i c h r e t u r n s t h e b l o c k ( o r t i l e ) h e s

    s t a n d i n g o n .

    T h e c o d e b e l o w g e t s t h e p l a y e r ' s t i l e p o s i t i o n . I t t h e n c a l l s

    t h e M i n e c r a f t A P I s getBlock() f u n c t i o n t o f i n d o u t t h e

    t y p e o f b l o c k t h e p l a y e r i s s t a n d i n g o n ( b y s u b t r a c t i n g 1

    f r o m t h e y c o o r d i n a t e ) b e f o r e u s i n g setBlock() t o c r e a t e

    b l o c k s o f t h e s a m e t y p e t h e p l a y e r i s s t a n d i n g o n a r o u n d

    h i m . S o i f y o u r p l a y e r i s s t a n d i n g o n D I R T , h e w i l l e n d u p

    w i t h D I R T s u r r o u n d i n g h i m , h o w e v e r i f h e i s s t a n d i n g o n

    S T O N E , S T O N E w i l l a p p e a r .

    playerTilePos = mc.player.getTilePos()blockBelowPlayerType =

    mc.getBlock(playerTilePos.x, playerTilePos.y -1, playerTilePos.z)

    mc.setBlock(playerTilePos.x + 1,playerTilePos.y + 1, playerTilePos.z,blockBelowPlayerType)

    mc.setBlock(playerTilePos.x, playerTilePos.y

    + 1, playerTilePos.z + 1, blockBelowPlayerType)mc.setBlock(playerTilePos.x - 1,

    playerTilePos.y + 1, playerTilePos.z,blockBelowPlayerType)

    mc.setBlock(playerTilePos.x, playerTilePos.y+ 1, playerTilePos.z - 1, blockBelowPlayerType)

    mc.postToChat("Trapped you")time.sleep(5)

    W e h a v e n o w t r a p p e d o u r p l a y e r w i t h i n 4 b l o c k s ( p r o v i d i n g

    h e d o e s n t b r e a k o u t ! ) , i n o r d e r t o s e t h i m f r e e w e n e e d t o

    r e m o v e a b l o c k . R e m o v i n g b l o c k s i s d o n e u s i n g

    setBlock() , b u t r a t h e r t h a n m a k i n g t h e b l o c k s o l i d l i k e W O O D o r S T O N E w e s e t i t t o A I R .

    mc.setBlock(playerTilePos.x + 1,playerTilePos.y + 1, playerTilePos.z,block.AIR)

    mc.postToChat("Be free")time.sleep(5)

    A f u l l l i s t o f a l l t h e a v a i l a b l e b l o c k s c a n b e f o u n d i n e i t h e r

    t h e M i n e c r a f t A P I s p e c i f i c a t i o n o r i n t h e b l o c k . p y m o d u l e i n

    t h e P y t h o n A P I l i b r a r y

    ~/mcpi/api/python/mcpi/block.py .

    T h e A P I a l s o a l l o w s y o u t o s e t m a n y b l o c k s a t a t i m e ,

    a l l o w i n g y o u t o c r e a t e c u b o i d s v e r y q u i c k l y u s i n g t h e

    setBlocks() c o m m a n d . I t w o r k s b y s p e c i f y i n g 2 s e t s o f x , y , z c o o r d i n a t e s b e t w e e n w h i c h i t t h e n f i l l s t h e g a p w i t h a

    c e r t a i n b l o c k y o u p a s s a s t h e f i n a l p a r a m e t e r . T h e c o d e

    b e l o w w i l l c r e a t e a d i a m o n d f l o o r u n d e r n e a t h o u r p l a y e r 5 0

    b l o c k s ( a c r o s s ) x 1 b l o c k ( u p ) x 5 0 b l o c k s ( a l o n g ) , w i t h o u r

    p l a y e r i n t h e m i d d l e ( i . e . 2 5 b e h i n d a n d t o t h e l e f t , 2 5 i n

    f r o n t a n d t o t h e r i g h t ) .

  • 7/30/2019 The MagPi Issue 11 En

    19/361 9

    mc.setBlocks(playerTilePos.x - 25,playerTilePos.y - 1, playerTilePos.z - 25,playerTilePos.x + 25, playerTilePos.y -1,playerTilePos.z + 25, block.DIAMOND_BLOCK)

    mc.postToChat("Now thats a big diamondfloor!")

    T o r e c a p o n t h e f u n c t i o n s c o v e r e d i n t h i s a r t i c l e :

    p o s t T o C h a t ( m e s s a g e ) - c o m m u n i c a t e w i t h t h e

    p l a y e r ( s ) i n t h e g a m e .

    g e t B l o c k ( x , y , z ) - g e t a b l o c k t y p e f o r a s p e c i f i c

    p o s i t i o n .

    s e t B l o c k ( x , y , z , b l o c k T y p e , b l o c k D a t a ) - s e t

    ( c h a n g e ) a b l o c k t o a s p e c i f i c b l o c k T y p e .

    s e t B l o c k s ( x 1 , y 1 , z 1 , x 2 , y 2 , z 2 , b l o c k T y p e ,

    b l o c k D a t a ) - s e t l o t s o f b l o c k s a l l a t t h e s a m e t i m e

    b y p r o v i d i n g 2 s e t s o f c o - o r d i n a t e s ( x , y , z ) a n d f i l l

    t h e g a p b e t w e e n w i t h a b l o c k T y p e .

    p l a y e r . g e t P o s ( ) - g e t t h e p r e c i s e p o s i t i o n o f a

    p l a y e r .

    p l a y e r . s e t P o s ( x , y , z ) - s e t ( c h a n g e ) t h e p l a y e r s

    p o s i t i o n .

    p l a y e r . g e t T i l e P o s ( ) - g e t t h e p o s i t i o n o f t h e b l o c k

    w h e r e t h e p l a y e r c u r r e n t .

    T h e r e a r e f e w o t h e r f u n c t i o n s a v a i l a b l e i n t h e A P I w h i c h

    s h o u l d b e e x p l o r e d . U s i n g o n l y t h e s m a l l n u m b e r

    d i s c u s s e d i n t h i s a r t i c l e i t i s p o s s i b l e w i t h s o m e

    i m a g i n a t i o n a n d a s m a l l a m o u n t o f p r o g r a m m i n g

    k n o w l e d g e t o c r e a t e s o m e q u i t e f a n t a s t i c c o n s t r u c t i o n s ,

    t o o l s a n d u t i l i t i e s .

    I l o v e M i n e c r a f t . I t i s a t r e m e n d o u s l y c r e a t i v e g a m e a n d

    w i t h t h e P i e d i t i o n s A P I i t o p e n s u p n e w l e v e l o f c r e a t i v i t y

    a n d w i l l h o p e f u l l y e n c o u r a g e m o r e p e o p l e t o t r y t h e i r h a n d

    a t p r o g r a m m i n g .

    C r e a t i n g t h e c o v e r i m a g e

    f r o m b l o c k s t o v i r t u a l i t y

    I a n M c A l p i n e c r e a t e d t h e

    i m p r e s s i v e M i n e c r a f t m a g p i e

    o n t h i s m o n t h ' s c o v e r . T h e

    M a g P i a s k e d I a n h o w h e

    a c h i e v e d t h i s .

    W i t h t h e M i n e c r a f t A P I i n s t a l l e d

    ( s e e m a i n a r t i c l e t e x t ) I d e c i d e d

    I w a n t e d t o c r e a t e a l a r g e c o p y

    o f t h e m a g a z i n e ' s l o g o t h a t

    w o u l d b e e a s i l y r e c o g n i s a b l e i n

    t h e g a m e w o r l d , u s i n g P y t h o n . T h i s w a s m y v e r y f i r s t

    P y t h o n p r o g r a m . C o d i n g t h e m a g p i e r a t h e r t h a n b u i l d i n g

    m a n u a l l y g a v e m e t h e f r e e d o m t o p l a c e t h e m a g p i e

    a n y w h e r e i n t h e M i n e c r a f t w o r l d . T h e w h o l e t h i n g w a s

    v e r y e x p e r i m e n t a l , b a l a n c i n g e f f o r t i n t e r m s o f p i x e l a t i o n

    w h i l e s t i l l r e t a i n i n g a n o b v i o u s l y r e c o g n i s a b l e i m a g e .

    H a v i n g i d e n t i f i e d t h a t I w a s l i m i t e d t o 8 s h a d e s o f

    b l a c k / g r e y / w h i t e a n d 4 s h a d e s o f b l u e i n t e r m s o f

    M i n e c r a f t b l o c k s , t h a t p a r t i a l l y d e t e r m i n e d t h e p i x e l a t i o n .

    U s i n g G i m p o n m y R a s p b e r r y P i , I p i x e l a t e d T h e M a g P i

    m a g p i e i m a g e t o 4 3 x 4 4 b l o c k s . T h i s s e e m e d t o b e t h e

    r i g h t b a l a n c e . . . b u t s t i l l m e a n t m a n u a l l y m a p p i n g 1 8 9 2

    s q u a r e s t o M i n e c r a f t b l o c k s ! I t c o u l d b e m a d e s m a l l e r

    b u t t h e p i x e l a t i o n w o u l d b e m o r e c o a r s e a n d t h e m a g p i e

    l e s s r e c o g n i s a b l e .

    I n P y t h o n , t h e i m a g e i s r e p r e s e n t e d b y a l i s t o f l i s t s ( i e a

    2 - d i m e n s i o n a l a r r a y ) . E a c h c e l l i n t h e a r r a y w a s g i v e n a

    c h a r a c t e r ; 1 - 8 f o r b l a c k , w h i t e a n d s h a d e s o f g r e y p l u s

    A - D f o r t h e s h a d e s o f b l u e . 0 r e p r e s e n t e d t h e " A i r " b l o c k

    t y p e . F i v e o t h e r v a r i a b l e s w e r e u s e d ; 3 f o r t h e x , y , z

    c o o r d i n a t e s , o n e f o r t h e a n g l e i n t h e v e r t i c a l p l a n e ( 0 ,

    9 0 , 1 8 0 o r 2 7 0 d e g r e e s ) a n d a v a r i a b l e t o i n d i c a t e i f t h e

    m a g p i e s h o u l d b e p l o t t e d o r d e l e t e d . A n e s t e d F O R -

    l o o p i t e r a t e s t h r o u g h t h e 2 - D a r r a y a n d p l o t s t h e b l o c k s .

    B e c a u s e s o m e b l o c k t y p e s c a n n o t b e p l a c e d i n f r e e

    s p a c e , i t i s i m p o r t a n t t h a t y o u b u i l d f r o m t h e b o t t o m u p .

    W h e n d e l e t i n g ( i e r e p l a c i n g a l l b l o c k s w i t h b l o c k t y p e

    " A i r " ) y o u n e e d t o d e l e t e f r o m t h e t o p d o w n . I f y o u d e l e t e

    f r o m t h e b o t t o m u p , s o m e b r i c k s w i l l f a l l b e f o r e t h e y c a n

    b e d e l e t e d t h u s l e a v i n g a p i l e o f r u b b l e o n t h e g r o u n d !

    Y o u s h o u l d t a k e a b a c k u p c o p y o f y o u r w o r l d b e f o r e

    c r e a t i n g l a r g e s t r u c t u r e s l i k e t h i s a s y o u w i l l l i k e l y u s e

    t h e w r o n g c o o r d i n a t e s . M y 1 0 y e a r o l d d a u g h t e r E m i l y

    w a s v e r y u n h a p p y w h e n I o b l i t e r a t e d h e r v i l l a g e ! T a k e a

    c o p y o f t h e h i d d e n . m i n e c r a f t d i r e c t o r y i n y o u r h o m e

    d i r e c t o r y ( u s e l s - A t o s e e a l l h i d d e n f i l e s i n a f o l d e r ) p l u s

    a l s o t h e g a m e s d i r e c t o r y i n t h e m c p i d i r e c t o r y .

  • 7/30/2019 The MagPi Issue 11 En

    20/360

    D I F F I C U L T Y : B E G I N N E R

    S t e w a r t W a t k i s s

    G u e s t W r i t e r

    P r i n t i n g w i t h C U P S

    1 - C o n f i g u r i n g C U P S

    T h i s i s a t w o p a r t t u t o r i a l o n c o n f i g u r i n g p r i n t i n g o n t h e

    R a s p b e r r y P i . I n t h i s a r t i c l e I a m g o i n g t o e x p l a i n s o m e o f

    t h e e s s e n t i a l s o f s e t t i n g u p p r i n t i n g a n d a d d i n g y o u r p r i n t e r

    i n R a s p b i a n . T h i s c o u l d b e u s e f u l f o r p r i n t i n g f r o m y o u r

    R a s p b e r r y P i o r t o t u r n y o u r U S B p r i n t e r i n t o a n e t w o r k

    e n a b l e d p r i n t e r . N e x t m o n t h w e w i l l l o o k a t h o w w e c a n

    p r i n t f r o m t h e c o m m a n d - l i n e a n d f r o m w i t h i n y o u r o w n

    P y t h o n p r o g r a m s .

    T h e s t a n d a r d m e t h o d o f p r i n t i n g o n L i n u x i s w i t h t h e

    C o m m o n U n i x P r i n t i n g S y s t e m

    T M

    b e t t e r k n o w n a s C U P S .

    D e v e l o p e d b y A p p l e I n c . , C U P S i s o p e n s o u r c e s o f t w a r e

    u s i n g s t a n d a r d b a s e d p r i n t i n g p r o t o c o l s .

    T h e j o b o f c o n v e r t i n g a n a p p l i c a t i o n p r i n t r e q u e s t t o

    s o m e t h i n g t h a t y o u r p r i n t e r c a n u n d e r s t a n d i s a m e m o r y

    h u n g r y t a s k . I t h e r e f o r e r e c o m m e n d u s i n g a 5 1 2 M B m o d e l

    B f o r p r i n t i n g d i r e c t l y f r o m t h e P i . E v e n w i t h a 5 1 2 M B

    R a s p b e r r y P i y o u c a n s t i l l r u n i n t o m e m o r y i s s u e s . I f y o u

    h a v e p r o b l e m s w i t h p r i n t i n g t h e n i t i s w o r t h t r y i n g w i t h a s

    f e w a p p l i c a t i o n s r u n n i n g a s p o s s i b l e , b e f o r e l o o k i n g a t

    o t h e r c a u s e s .

    F i r s t w e n e e d t o i n s t a l l C U P S f r o m t h e s o f t w a r e

    r e p o s i t o r i e s :

    sudo apt-get install cups

    C U P S u s e s t h e l p a d m i n g r o u p t o d e t e r m i n e w h o i s

    a u t h o r i s e d t o a d m i n i s t e r t h e p r i n t e r s . T o a d d a p r i n t e r y o u

    w i l l n e e d t o a d d y o u r u s e r t o t h e l p a d m i n g r o u p :

    sudo usermod -a -G lpadmin pi

    W e a r e n o w g o i n g t o u s e t h e C U P S w e b b a s e d i n t e r f a c e

    w h i c h i s p r o v i d e d b y d e f a u l t . A l l t h e f u n c t i o n a l i t y i s b u i l t i n t o

    C U P S , s o w e d o n o t n e e d t o w o r r y a b o u t e n a b l i n g a w e b

    s e r v e r . J u s t p o i n t y o u r b r o w s e r a t h t t p : / / 1 2 7 . 0 . 0 . 1 : 6 3 1 a n d

    u s e y o u r n o r m a l u s e r n a m e a n d p a s s w o r d w h e n p r o m p t e d

    d u r i n g c o n f i g u r a t i o n o r w h e n a d m i n i s t e r i n g t h e p r i n t e r s .

    C h o o s e t h e A d m i n i s t r a t i o n t a b a n d t h e n A d d P r i n t e r .

    C U P S w i l l s e a r c h f o r p r i n t e r s t h a t a r e p h y s i c a l l y c o n n e c t e d

    t o t h e R a s p b e r r y P i o r t h a t i t c a n s e e o n t h e l o c a l n e t w o r k .

    I n m y c a s e i t f o u n d m y n e t w o r k p r i n t e r " E p s o n S t y l u s P h o t o

    P X 7 2 0 W D " . Y o u w i l l a l s o s e e s o m e o p t i o n s f o r H P p r i n t e r s

    a s t h e s e a r e o f t e n s h o w n e v e n i f t h e r e a r e n o H P p r i n t e r s

    c o n n e c t e d .

    A f t e r s e l e c t i n g t h e a p p r o p r i a t e p r i n t e r y o u c a n c u s t o m i s e

    t h e n a m e a n d l o c a t i o n o f t h e p r i n t e r s . T h e s e c a n n o r m a l l y

    b e l e f t a t t h e i r d e f a u l t s , a l t h o u g h y o u c o u l d c h a n g e t h e

    n a m e t o o n e t h a t i s e a s i e r t o r e m e m b e r . I f y o u a r e l i k e l y t o

    b e p r i n t i n g f r o m t h e c o m m a n d l i n e t h e n i t c a n a l s o b e

    u s e f u l t o m a k e t h e p r i n t e r n a m e s h o r t e r a n d a v o i d a n y

    s p a c e s .

  • 7/30/2019 The MagPi Issue 11 En

    21/36

  • 7/30/2019 The MagPi Issue 11 En

    22/36

    S A F E A N D S I M P L E

    C O N N E C T I O N T O Y O U R

    R A S P B E R R Y P I

    F i n d o u t m o r e a t

    q u i c k 2 w i r e . c o m

    I n t e r f a c e b o a r d

    I

    2

    C P o r t E x t e n d e r

    A n a l o g u e b o a r d

    A r t i c l e s

    T u t o r i a l s

    F o r u m

    F o r G P I O , I

    2

    C , S P I

    D e v i c e L i b r a r i e s

    E x a m p l e s

    S U P P O R T H A R D W A R E

    S O F T W A R E

    I n t e r f a c e b o a r d : 1 3 . 8 6 | P o r t E x t e n d e r : 9 . 8 0 | A n a l o g u e b o a r d : 1 6 . 4 8

    P r i c e s i n c l u d e U K V A T b u t e x c l u d e P o s t a g e a n d P a c k a g i n g : f r o m 2 . 7 0

    2

    http://quick2wire.com/http://quick2wire.com/
  • 7/30/2019 The MagPi Issue 11 En

    23/362 3

    D I F F I C U L T Y : M E D I U M

    D I F F I C U L T Y : A D V A N C E D

    S e t t i n g u p a s i m p l e i n t r a n e t

    J a s o n " J a s e m a n "

    D a v i e s

    MagPi Writer

    In this tutorial I will be showing you how to set up a

    very basic intranet that is hosted from a Raspberry Pi.

    W h a t i s a n ' I n t r a n e t ' ?

    An intranet is similar to a website, however it is not

    accessible outside of your local area network (LAN).

    For example, if you set up an intranet at home, onlythe computers that are connected directly to your

    router/modem at your house can access it. If it is at

    work, only computers within your office can see it.

    It is possible to create a Virtual Private Network

    (VPN) link to gain access from outside of your LAN.

    In this case a computer that has internet access can

    connect in to your LAN and then access the intranet

    site. We won't be covering VPN connections today

    though.

    So without further ado, let's get started.

    1 . C h a n g i n g y o u r R a s p b e r r y P i ' s

    h o s t n a m e

    You will want to change the hostname (Or network

    name) of your Raspberry Pi to match the name of

    your intranet. To keep it simple, lets just call it

    'mynet'.

    From the command line type:

    s u d o n a n o / e t c / h o s t n a m e

    change the name (By default this is 'raspberry') to

    'mynet'. Press CTRL+X to save the changes (Y to

    confirm). Then type:

    s u d o n a n o / e t c / h o s t s

    Change any references of 'raspberry' to 'mynet'.

    Again Press CTRL+X to save the changes (Y to

    confirm). Then restart your Pi by typing:

    s u d o r e b o o t

    2 . I n s t a l l i n g A p a c h e 2 W e b s e r v e r

    To be able to host a webpage on your intranet, you

    need to install something called 'Apache 2

    Webserver'. To do this, go to the command line and

    type:

    s u d o a p t - g e t u p d a t e

    s u d o a p t - g e t u p g r a d e

    s u d o a p t - g e t i n s t a l l a p a c h e 2

    We have to specify the ServerName for Apache2:

    s u d o n a n o / e t c / a p a c h e 2 / a p a c h e 2 . c o n f

    At the bottom of the file add:

    S e r v e r N a m e m y n e t

    Then press CTRL+X to save the changes (Y to

    confirm). Now restart apache,

  • 7/30/2019 The MagPi Issue 11 En

    24/364

    s u d o s e r v i c e a p a c h e 2 r e s t a r t

    Note: the update and upgrade might take some time,

    but worthwhile to make sure you have the latest

    versions.

    3 . C h e c k t h a t A p a c h e 2 i s w o r k i n g

    Using another PC, tablet, netbook or smartphone on

    your network or wifi, open a web browser and visit

    http://mynet. If all is well you should see the default

    webpage for Apache2

    You might find that your device cannot resolve the

    domain name. In which case you can access the

    page using the ip address of the Pi. To find out the

    Pi's ip address, type the following at the command

    line of the pi:

    s u d o i f c o n f i g

    Look for the inet addr for eth0 - it might be something

    like 192.168.1.75

    Try browsing to http://192.168.1.75 (Or whatever the

    ip number is of your Pi)

    4 . C h a n g i n g t h e d e f a u l t w e b p a g e

    For this, it is helpful if you have some basic

    knowledge of the HTML language. There are lots of

    guides on the internet to get you started.

    On the Raspberry Pi, go to the command line and

    type:

    c d / v a r / w w w

    l s

    You should see a file listed called index.html. This isthe file that opens when you visit http://mynet

    To change the webpage we can use any text editor.

    I'm going to use nano:

    s u d o n a n o i n d e x . h t m l

    Alter the contents of index.html so that it looks like

    this:

    < h t m l >

    < h e a d >

    < t i t l e > M y I n t r a n e t < / t i t l e >

    < / h e a d >

    < b o d y >

    < c e n t e r >

    < h 1 > W e l c o m e t o m y i n t r a n e t < / h 1 > < p >

    < / b o d y >

    < / h t m l >

    Refresh or revisit http://mynet from your other

    computer to see the changes.

    5 . A d d i n g s u b - p a g e s

    Edit the index.html file again by typing:

    s u d o n a n o / v a r / w w w / i n d e x . h t m l

    Change it as follows:

    < h t m l >

    < h e a d >

    < t i t l e > M y I n t r a n e t < / t i t l e >

    < / h e a d >

    < b o d y b g c o l o r = " g r e e n " >

    < c e n t e r >

    < h 1 > W e l c o m e t o m y i n t r a n e t < / h 1 >

    < p > C l i c k < a h r e f = " p a g e 2 . h t m l " > h e r e < / a >

    f o r p a g e 2 . < / p >

    < / b o d y >

    < / h t m l >

    Then we will create page2.html. Type:

    s u d o n a n o / v a r / w w w / p a g e 2 . h t m l

    and then add,

    < h t m l >

    < h e a d >

    < t i t l e > P a g e 2 < / t i t l e >

    < / h e a d >

    < b o d y b g c o l o r = " r e d " >

    < c e n t e r >

    < h 1 > T h i s i s p a g e 2 < / h 1 >

    < p > C l i c k < a h r e f = " i n d e x . h t m l " > h e r e < / a > t o

    r e t u r n t o t h e m a i n p a g e . < / p >

    < / b o d y >

    < / h t m l >

    After saving this file, refresh your browser and try

    using the links to switch between the index.html pageand page2.html.

  • 7/30/2019 The MagPi Issue 11 En

    25/362 5

    D I F F I C U L T Y : A D V A N C E D

    D I F F I C U L T Y : A D V A N C E D

    R a s p b e r r y P i :

    A s s e m b l y L a n g u a g e

    B r u c e S m i t h

    G u e s t W r i t e r

    L e a r n h o w t o p r o g r a m y o u r f a v o u r i t e

    c o m p u t e r i n i t s n a t i v e m a c h i n e c o d e b y

    u s i n g A s s e m b l y L a n g u a g e .

    O n e o f t h e q u e s t i o n s I g e t a s k e d m o s t o f a l l i s ,

    W h a t s t h e d i f f e r e n c e b e t w e e n a s s e m b l y

    l a n g u a g e a n d m a c h i n e c o d e ? T h e a n s w e r I g i v e

    i s s i m p l e e n o u g h , A s s e m b l y l a n g u a g e c r e a t e s

    m a c h i n e c o d e . T h i s s e r i e s w i l l t e a c h y o u t h e i n s

    a n d o u t s o f a s s e m b l e r a n d w i l l s h o w y o u h o w t o

    c r e a t e y o u r o w n m a c h i n e c o d e p r o g r a m s t o r u n

    o n y o u r R a s p b e r r y P i .

    M a c h i n e c o d e i s j u s t t h a t , t h e c o d e t h a t r u n s y o u r

    m a c h i n e . E v e r y c o m p u t e r u s e s m a c h i n e c o d e ;

    h o w e v e r , t h e r e a r e d i f f e r e n t t y p e s o f m a c h i n e

    c o d e f o r d i f f e r e n t t y p e s o f c o m p u t e r s . T h e t y p e i s

    d e t e r m i n e d b y t h e m i c r o p r o c e s s o r t h e c o m p u t e r

    u s e s , a n d o n t h e R a s p b e r r y P i t h i s i s A R M .

    I n i t s p u r e s t f o r m m a c h i n e c o d e i s a s e q u e n c e o f

    n u m b e r s . E a c h n u m b e r a n d t h e o r d e r o f t h e

    n u m b e r s h a v e m e a n i n g t o y o u r A R M c h i p . N o

    m a t t e r w h a t o p e r a t i n g s y s t e m y o u h a v e r u n n i n g

    o n y o u r R a s p b e r r y P i o r w h a t s o f t w a r e y o u u s e

    o n i t , i t i s u s i n g m a c h i n e c o d e . S o w h y n o t l e a r n

    m o r e a b o u t i t ?

    A s s e m b l y l a n g u a g e p r o v i d e s a f o r m o f s h o r t h a n d

    t h a t m a k e s i t e a s i e r t o c r e a t e m a c h i n e c o d e . I t

    u s e s m n e m o n i c s t o r e p r e s e n t m a c h i n e c o d e

    o p e r a t i o n s i n t h e s a m e w a y w e u s e t h e m t o

    s h o r t e n S M S m e s s a g e s a n d T w e e t s . A s t a n d a r d

    a s s e m b l e r m n e m o n i c w i l l g e n e r a l l y h a v e t h r e e

    l e t t e r s . W h a t d o y o u t h i n k t h e s e t w o m n e m o n i c s

    m e a n ?

    T h e y a r e t h e m n e m o n i c s f o r a d d i t i o n a n d

    s u b t r a c t i o n .

    i s t h e m n e m o n i c f o r t h e m o v e c o m m a n d . I t t a k e s

    i n f o r m a t i o n f r o m o n e p l a c e a n d p l a c e s i t i n

    a n o t h e r p l a c e . H o w e a s y i s t h a t ?

    A s s e m b l i n g a M a c h i n e C o d e P r o g r a m

    O n e o f t h e e a s i e s t w a y s b u t c e r t a i n l y n o t t h e

    o n l y w a y o f w r i t i n g a n a s s e m b l e r p r o g r a m i s t o

    u s e B B C B A S I C . T h i s i s a v a i l a b l e a s p a r t o f t h e

    R I S C O S o p e r a t i n g s y s t e m w h i c h i s a v a i l a b l e i n

    R a s p b e r r y P i f l a v o u r f r o m t h e D o w n l o a d s p a g e

    o n t h e R a s p b e r r y P i w e b s i t e . S o i t s f r e e a n d

    r e a d i l y a v a i l a b l e .

    W i t h R I S C O S r u n n i n g , t h e r e w i l l b e a r a s p b e r r y

    i c o n i n t h e b o t t o m r i g h t h a n d c o r n e r o f t h e s c r e e n

    ( t h e t a s k b a r ) . M o v e t h e p o i n t e r o v e r i t a n d f r o m

    t h e p o p - u p m e n u s e l e c t T a s k W i n d o w

    ADD

    SUB

    MOV

  • 7/30/2019 The MagPi Issue 11 En

    26/366

    ( S e e i m a g e a t r i g h t ) .

    A n e w w i n d o w w i l l b e d i s p l a y e d

    s h o w i n g a n * a s a p r o m p t . I n

    t h i s w i n d o w t y p e :

    A n d p r e s s t h e R E T U R N k e y .

    T h i s w i l l s t a r t B B C B A S I C .

    Y o u c a n t h e n e n t e r t h e p r o g r a m s h o w n i n

    P A N E L 1 . W h e n y o u h a v e e n t e r e d i t , t y p e :

    a n d t h e p r o g r a m w i l l b e a s s e m b l e d . Y o u w i l l g e t

    a n a s s e m b l y l i s t i n g w h i c h w i l l l o o k s o m e t h i n g l i k e

    t h i s :

    I f y o u g e t a n y e r r o r m e s s a g e s c h e c k t h e l i n e

    i n d i c a t e d c a r e f u l l y . P r o g r a m s a r e c a s e s e n s i t i v e

    s o e n s u r e y o u h a v e e n t e r e d i t e x a c t l y a s s h o w n .

    T h e a s s e m b l y l i s t i n g h a s s e v e r a l c o l u m n s . T h e

    f i r s t i s t h e m e m o r y a d d r e s s w h e r e e a c h

    i n s t r u c t i o n i s p l a c e d ( a n d t h i s w i l l m o s t l i k e l y b e

    d i f f e r e n t o n y o u r R a s p b e r r y P i ) . T h e s e c o n d

    c o l u m n c o n t a i n s t h e m a c h i n e c o d e . T h e n u m b e r

    s e q u e n c e :

    i s t h e m a c h i n e c o d e f o r :

    T h e t h i r d c o l u m n i s w h e r e l a b e l s a r e p l a c e d .

    F i n a l l y t h e f o u r t h c o l u m n i s t h e a s s e m b l y

    l a n g u a g e f o r e a c h l i n e .

    T h e f i r s t l i n e s h o w s h o w w e c a n i n t e g r a t e B A S I C

    c o m m a n d s i n t h e a s s e m b l e r .

    W e d i d n o t h a v e t o l o o k - u p

    t h e A S C I I c o d e f o r a n

    a s t e r i s k ; w e u s e d t h e A S C

    f u n c t i o n o f B A S I C t o d o i t f o r

    u s . Y o u c a n u s e a l l B A S I C

    f u n c t i o n s i n t h i s w a y . T h i s

    i n t e g r a t i o n w i t h B A S I C c a n

    b e a s s i m p l e o r c o m p l e x a s

    r e q u i r e d . I f i t i s a l e g i t i m a t e f u n c t i o n , i t w i l l

    a s s e m b l e c o r r e c t l y . S o m e t h i n g l i k e t h i s i s

    a c c e p t a b l e a n d w i l l a s s e m b l e :

    T r y i t !

    R u n n i n g M a c h i n e C o d e

    T o e x e c u t e t h e m a c h i n e c o d e w e u s e t h e

    C A L L s t a t e m e n t t h u s :

    H e r e S T A R T i s t h e v a r i a b l e l a b e l w h i c h

    s i g n i f i e s w h e r e t h e m a c h i n e c o d e b e g i n s

    a n d w h i c h t h e B A S I C p r o g r a m d e f i n e d i n

    l i n e 5 0 . W h e n y o u C A L L t h e p r o g r a m ,

    y o u w i l l s e e a n a s t e r i s k p r i n t e d o n t h e

    s c r e e n . T h i s m a c h i n e c o d e c o u l d a l s o

    h a v e b e e n e x e c u t e d b y t y p i n g :

    o r :

    I n t h i s l a s t c a s e u s e t h e v a l u e p r i n t e d o u t o n t h e

    s c r e e n b y t h e P r o g r a m A s s e m b l e d a t :

    s t a t e m e n t , a s i t m a y b e d i f f e r e n t f o r y o u .

    S a v i n g M a c h i n e C o d e

    W e u s e d t h e S A V E c o m m a n d t o s a v e t h e

    a s s e m b l y l a n g u a g e p r o g r a m . T o s a v e m a c h i n e

    c o d e w e u s e t h e * S A V E c o m m a n d a n d w e n e e d

    t o k n o w i t s s t a r t a d d r e s s e s a n d l e n g t h o f t h e

    m a c h i n e c o d e . T h e s e s h o u l d h a v e b e e n p r i n t e d

    o u t b y l i n e s 1 0 0 a n d 1 1 0 o f t h e p r o g r a m . T h e

    c o m m a n d * S A V E h a s t h i s f o r m a t :

    F o r t h e e x a m p l e a b o v e , t h e * S A V E c o m m a n d

    w o u l d l o o k s o m e t h i n g l i k e t h i s :

    BASIC

    RUN

    00008FAC

    00008FAC .START

    00008FAC E3A0002A MOV R0, #ASC (*")

    00008FB0 EF000000 SWI OS_WriteC

    00008FB4 E1A0F00E MOV R15, R14

    PROGRAM ASSEMBLED AT: &008FAC

    PROGRAM SIZE IS: 12 BYTES LONG

    E3A0002A

    MOV R0, #ASC(*)

    MOV R0, #INT(SIN(DEG 60)*100)

    CALL START

    CALL CODE%

    C A L L & 8 F A C

    *SAVE +

    S e l e c t T a s k w i n d o w f r o m t h e R a s p b e r r y m e n u t o

    a c c e s s B B C B A S I C i n R I S C O S .

  • 7/30/2019 The MagPi Issue 11 En

    27/362 7

    Y o u w i l l n e e d t o u s e t h e n u m b e r s y o u r p r o g r a m

    p r i n t e d o u t .

    T h e * S A V E c o m m a n d d o e s n o t k n o w i t i s s a v i n g

    m a c h i n e c o d e , w h a t i t d o e s i s s a v e a n a r e a o f

    m e m o r y . T h e a d d r e s s o f t h e l a s t b y t e o f m e m o r y

    s a v e d i s g i v e n b y a d d i n g t h e l e n g t h t o t h e s t a r t .

    O n c e y o u h a v e t h e m a c h i n e c o d e f i l e s a v e d , y o u

    h a v e s o m e t h i n g y o u c a n e x e c u t e d i r e c t l y f r o m

    f i l e . T h i s c a n b e p e r f o r m e d u s i n g t h e * R U N

    c o m m a n d t h u s :

    T h e p r o g r a m w i l l l o a d i n t o t h e a d d r e s s w h e r e i t

    w a s o r i g i n a l l y a s s e m b l e d a n d t h e n r u n . T h e

    r e s u l t w i l l b e t h e a s t e r i s k a s b e f o r e .

    I f y o u a r e r u n n i n g m a c h i n e c o d e y o u c a n o m i t t h e

    R U N p o r t i o n a n d j u s t u s e t h e f i l e n a m e t h u s :

    A R M L o c a t i o n s

    T h e p r o g r a m u s e s a n a r e a o f m e m o r y t h a t i s

    i n s i d e t h e A R M c h i p c a l l e d a r e g i s t e r ( R 0 ) t o h o l d

    t h e A S C I I v a l u e o f t h e a s t e r i s k i n l i n e 6 0 a n d t h e y

    u s e a r e g i s t e r a g a i n i n l i n e 8 0 t o r e t u r n t o B A S I C .

    W e l l e x a m i n e t h i s i n d e p t h n e x t t i m e .

    B R E A K O U T P A N E L 1 :

    P r o g r a m a n d D e s c r i p t i o n

    L i n e 1 0 i s a R E M a r k s t a t e m e n t . I f y o u t y p e :

    R I S C O S w i l l u s e t h e t e x t a f t e r t h e > i n a R E M

    s t a t e m e n t i n t h e f i r s t l i n e a s a f i l e n a m e . S o t h i s

    p r o g r a m w o u l d b e s a v e d a s A S T E R I S K .

    L i n e 2 0 r e s e r v e s 1 0 0 b y t e s o f s p a c e p o i n t e d t o

    b y t h e v a r i a b l e C O D E % .

    T h e v a r i a b l e P % i s u s e d b y B B C B A S I C t o s e l e c t

    w h e r e i n m e m o r y t h e m a c h i n e c o d e w i l l b e

    s t o r e d . L i n e 3 0 p o i n t s P % a t t h e s p a c e r e s e r v e d

    b y C O D E %

    O p e n i n g a n d c l o s i n g s q u a r e b r a c k e t s ( l i n e s 4 0

    a n d 9 0 ) a r e u s e d t o m a r k t h e s t a r t a n d e n d o f t h e

    a s s e m b l y l a n g u a g e p r o g r a m . A n y t h i n g i n s i d e

    t h e s e l i n e s i s a s s u m e d t o b e a s s e m b l y l a n g u a g e .

    L i n e 5 0 i s a l a b e l c a l l e d S T A R T . L a b e l s a r e

    a l w a y s p r e c e d e d w i t h a f u l l - s t o p .

    L i n e s 6 0 , 7 0 a n d 8 0 a r e t h e a s s e m b l y l a n g u a g e

    i n s t r u c t i o n s t o p r i n t a n a s t e r i s k o n t h e s c r e e n .

    T h e A S C I I c o d e f o r a n a s t e r i s k i s l o a d e d i n t o

    R e g i s t e r ( l i n e 6 0 ) a n d t h e n p r i n t e d o n t h e s c r e e n

    ( l i n e 7 0 ) . L i n e 8 0 r e t u r n s c o n t r o l b a c k t o B A S I C .

    L i n e 1 0 0 a n d 1 1 0 p r i n t o u t w h e r e t h e p r o g r a m

    w a s a s s e m b l e d a n d h o w l o n g i t i s .

    T i p P a n e l

    I f y o u a r e h a v i n g t r o u b l e g e t t i n g a t c e r t a i n

    c h a r a c t e r s o n y o u r k e y b o a r d i n R I S C O S , t r y

    s e t t i n g y o u r k e y b o a r d d r i v e r t o t h e U S A b y

    t y p i n g :

    A b o u t t h e A u t h o r

    B r u c e S m i t h i s a n a w a r d w i n n i n g a u t h o r . H i s b o o k ,

    R a s p b e r r y P i A s s e m b l y L a n g u a g e B e g i n n e r s , i s

    n o w a v a i l a b l e f r o m A m a z o n .

    C h e c k h i m a n d h i s b o o k o u t a t

    w w w . b r u c e s m i t h . i n f o f o r m o r e R a s p b e r r y P i

    r e s o u r c e s .

    *SAVE asterx 8FAC +12

    *RUN asterx

    *asterx

    10 REM >ASTERISK

    20 DIM CODE% (100)

    30 P%=CODE%

    40 [

    50 .START

    60 MOV R0, #ASC (*)

    70 SWI OS_WriteC

    80 MOV R15, R14

    90 ]

    100 PRINT PROGRAM ASSEMBLED AT: &; ~CODE%

    110 PRINT PROGRAM SIZE IS: ; P%-START;

    BYTES LONG

    SAVE

    *KEYBOARD USA

  • 7/30/2019 The MagPi Issue 11 En

    28/368

    D I F F I C U L T Y : A D V A N C E D

    P e t e r N o w o s a d

    G u e s t W r i t e r

    A n i n t r o d u c t i o n t o C h a r m D a t a T y p e s

    A n I n t r o d u c t i o n t o C h a r m D a t a T y p e s

    C o m p u t e r s c a n a c c u r a t e l y a n d q u i c k l y

    m a n a g e , m a n i p u l a t e a n d t r a n s f o r m

    i n f o r m a t i o n i n a c c o r d a n c e w i t h s e t s o f c o d e d

    i n s t r u c t i o n s p a c k a g e d i n t o o p e r a t i n g

    s y s t e m s a n d p r o g r a m s . I n t h i s a r t i c l e o f t h e

    C h a r m s e r i e s I a m g o i n g t o c o n c e n t r a t e o n

    h o w C h a r m d e f i n e s a n d h a n d l e s d i f f e r e n t

    t y p e s o f d a t a u s e d t o r e p r e s e n t t h a t

    i n f o r m a t i o n .

    B a s i c D a t a T y p e s

    C u r r e n t l y C h a r m d e f i n e s f o u r i n t r i n s i c t y p e s

    o f d a t a a s s h o w n i n t h e f o l l o w i n g t a b l e :

    boolean v a l u e s r e p r e s e n t f l a g s t h a t a r e

    e i t h e r true o r false, a n d s t r i c t l y s p e a k i n g

    o n l y r e q u i r e o n e b i t o f d a t a . I t i s p o s s i b l e t o

    c o m b i n e u p t o 8 b o o l e a n f l a g s i n a char o r

    3 2 f l a g s i n a n int u s i n g t h e C h a r m b i t

    o p e r a t o r s . F o r c o n v e n i e n c e a n d s p e e d

    h o w e v e r , t h e c o m p i l e r d o e s n o t a t t e m p t t o

    p a c k boolean d e f i n i t i o n s t o g e t h e r b u t s t o r e s

    e a c h i n i t s o w n b y t e l o c a t i o n w h i c h i s t h e

    s m a l l e s t u n i t o f m e m o r y t h e A R M C P U c a n

    d i r e c t l y a d d r e s s .

    char v a l u e s r e p r e s e n t s i n g l e c h a r a c t e r s

    u s i n g t h e u b i q u i t o u s A S C I I c o d e . S o m e

    c h a r a c t e r s a r e n o n - p r i n t i n g a n d f o r h i s t o r i c a l

    r e a s o n s t h e s e a r e k n o w n a s c o n t r o l

    c h a r a c t e r s , t h e m o s t c o m m o n b e i n g s p a c e

    ( 3