Riad_ Bajraktarevic
/
Bajraktarevic_Test
Main
main.cpp@4:9e935689c3c4, 2020-01-13 (annotated)
- Committer:
- riad
- Date:
- Mon Jan 13 17:21:21 2020 +0000
- Revision:
- 4:9e935689c3c4
- Parent:
- 3:f80e65dd4ee3
- Child:
- 5:eb2e43da141c
nibbleLeds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
riad | 0:7925301ed8e4 | 1 | #include "mbed.h" |
riad | 0:7925301ed8e4 | 2 | #define BUTTON1 p14 |
riad | 0:7925301ed8e4 | 3 | #define BUTTON! A1 |
riad | 0:7925301ed8e4 | 4 | |
riad | 0:7925301ed8e4 | 5 | int modifyBit(int x, unsingned char position, bool State); |
riad | 0:7925301ed8e4 | 6 | int lauflicht(bool richtung, int time, int &anz); |
riad | 0:7925301ed8e4 | 7 | void nibbleLeds(int value); |
riad | 2:47d9c6c7eb9e | 8 | void printb(uint8_t x); |
riad | 0:7925301ed8e4 | 9 | |
riad | 0:7925301ed8e4 | 10 | |
riad | 0:7925301ed8e4 | 11 | BusOut myleds(LED1,LED2,LED3,LED4); |
riad | 0:7925301ed8e4 | 12 | DigitalIn button(BUTTON1); |
riad | 0:7925301ed8e4 | 13 | |
riad | 0:7925301ed8e4 | 14 | const int INIT = 0x03; |
riad | 0:7925301ed8e4 | 15 | |
riad | 2:47d9c6c7eb9e | 16 | int main () |
riad | 2:47d9c6c7eb9e | 17 | { |
riad | 0:7925301ed8e4 | 18 | int anzahl, anz; |
riad | 0:7925301ed8e4 | 19 | uint8_t value = INIT; |
riad | 2:47d9c6c7eb9e | 20 | |
riad | 2:47d9c6c7eb9e | 21 | |
riad | 1:a3d6a365dc15 | 22 | } |
riad | 1:a3d6a365dc15 | 23 | |
riad | 2:47d9c6c7eb9e | 24 | int modifyBit (int x, uint8_t position, bool State) |
riad | 2:47d9c6c7eb9e | 25 | { |
riad | 1:a3d6a365dc15 | 26 | int mask = 1 << position; |
riad | 1:a3d6a365dc15 | 27 | int state = int(State); |
riad | 1:a3d6a365dc15 | 28 | return (x & ~mask) | ((state << position) & mask); |
riad | 2:47d9c6c7eb9e | 29 | } |
riad | 2:47d9c6c7eb9e | 30 | |
riad | 2:47d9c6c7eb9e | 31 | int lauflicht (bool richtung, int time, int &anz) |
riad | 2:47d9c6c7eb9e | 32 | { |
riad | 2:47d9c6c7eb9e | 33 | int i; |
riad | 2:47d9c6c7eb9e | 34 | uint8_t lauf = 0x01; |
riad | 2:47d9c6c7eb9e | 35 | |
riad | 2:47d9c6c7eb9e | 36 | if(!richtung) |
riad | 2:47d9c6c7eb9e | 37 | lauf = 0x08; |
riad | 2:47d9c6c7eb9e | 38 | while(1) { |
riad | 2:47d9c6c7eb9e | 39 | nibbleLeds(lauf&0x0F); |
riad | 2:47d9c6c7eb9e | 40 | if(richtung) { |
riad | 2:47d9c6c7eb9e | 41 | lauf = lauf << 1; |
riad | 2:47d9c6c7eb9e | 42 | if(lauf > 8) |
riad | 2:47d9c6c7eb9e | 43 | lauf = 0x01; |
riad | 2:47d9c6c7eb9e | 44 | } else { |
riad | 2:47d9c6c7eb9e | 45 | lauf = lauf >> 1; |
riad | 2:47d9c6c7eb9e | 46 | if(lauf == 0) |
riad | 2:47d9c6c7eb9e | 47 | lauf = 0x08; |
riad | 2:47d9c6c7eb9e | 48 | } |
riad | 2:47d9c6c7eb9e | 49 | if(button) |
riad | 2:47d9c6c7eb9e | 50 | break; |
riad | 2:47d9c6c7eb9e | 51 | wait_ms(time); |
riad | 2:47d9c6c7eb9e | 52 | anz++; |
riad | 2:47d9c6c7eb9e | 53 | } |
riad | 2:47d9c6c7eb9e | 54 | return anz; |
riad | 3:f80e65dd4ee3 | 55 | } |
riad | 3:f80e65dd4ee3 | 56 | |
riad | 3:f80e65dd4ee3 | 57 | void printb(uint8_t x) |
riad | 3:f80e65dd4ee3 | 58 | { |
riad | 3:f80e65dd4ee3 | 59 | for(int i=sizeof (x) << 3; i; i--) |
riad | 3:f80e65dd4ee3 | 60 | putchar('0' + ((x>>(i-1))&1)); |
riad | 3:f80e65dd4ee3 | 61 | printf("\n"); |
riad | 4:9e935689c3c4 | 62 | } |
riad | 4:9e935689c3c4 | 63 | |
riad | 4:9e935689c3c4 | 64 | void nibbleLeds(int value) |
riad | 4:9e935689c3c4 | 65 | { |
riad | 4:9e935689c3c4 | 66 | myleds = value%16; |
riad | 2:47d9c6c7eb9e | 67 | } |