![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A blinky variation for the mBuino with a bunch of different blink modes, deep-sleeping between iterations.
Dependencies: Crypto RNG mbed WakeUp
Fork of mBuinoBlinky by
mBuino blinky experiments.
main.cpp@14:cdc203a0bdc1, 2014-09-09 (annotated)
- Committer:
- mikewebkist
- Date:
- Tue Sep 09 17:29:17 2014 +0000
- Revision:
- 14:cdc203a0bdc1
- Parent:
- 13:292326e5d3bb
Change sleep mode and timing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Experiment626 | 0:aa0e1ad88af5 | 1 | #include "mbed.h" |
mikewebkist | 2:fd6008aa85cd | 2 | #include "RNG/Random.h" |
mikewebkist | 11:be76674d2dbd | 3 | #include "WakeUp/WakeUp.h" |
Experiment626 | 0:aa0e1ad88af5 | 4 | |
Experiment626 | 0:aa0e1ad88af5 | 5 | DigitalOut LED[] = {(P0_7), (P0_8), (P0_2), (P0_20), (P1_19), (P0_17), (P0_23)};// declare 7 LEDs |
Experiment626 | 0:aa0e1ad88af5 | 6 | |
mikewebkist | 2:fd6008aa85cd | 7 | int rndLED = 0; |
mikewebkist | 2:fd6008aa85cd | 8 | Random RNG = Random(); |
Experiment626 | 0:aa0e1ad88af5 | 9 | |
mikewebkist | 10:8a901b6d8cfa | 10 | void myDeepSleep() { |
mikewebkist | 10:8a901b6d8cfa | 11 | LPC_PMU->PCON = 0x1; |
mikewebkist | 10:8a901b6d8cfa | 12 | SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; |
mikewebkist | 10:8a901b6d8cfa | 13 | LPC_SYSCON->PDAWAKECFG &= 0xFFFFF800; |
mikewebkist | 10:8a901b6d8cfa | 14 | __WFI(); |
mikewebkist | 10:8a901b6d8cfa | 15 | } |
mikewebkist | 10:8a901b6d8cfa | 16 | |
mikewebkist | 14:cdc203a0bdc1 | 17 | void myPowerDown() { |
mikewebkist | 14:cdc203a0bdc1 | 18 | LPC_PMU->PCON = 0x2; |
mikewebkist | 14:cdc203a0bdc1 | 19 | SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; |
mikewebkist | 14:cdc203a0bdc1 | 20 | LPC_SYSCON->PDAWAKECFG &= 0xFFFFF800; |
mikewebkist | 14:cdc203a0bdc1 | 21 | __WFI(); |
mikewebkist | 14:cdc203a0bdc1 | 22 | } |
mikewebkist | 14:cdc203a0bdc1 | 23 | |
mikewebkist | 12:c94348aca325 | 24 | void sweepMode(float delayTime) |
Experiment626 | 0:aa0e1ad88af5 | 25 | { |
mikewebkist | 1:47c61bf9c81e | 26 | for(int x=0; x<7; x++) |
Experiment626 | 0:aa0e1ad88af5 | 27 | { |
mikewebkist | 1:47c61bf9c81e | 28 | LED[x] = 1; |
mikewebkist | 1:47c61bf9c81e | 29 | wait(delayTime); |
Experiment626 | 0:aa0e1ad88af5 | 30 | } |
mikewebkist | 2:fd6008aa85cd | 31 | |
mikewebkist | 2:fd6008aa85cd | 32 | for(int x=6; x>=0; x--) |
Experiment626 | 0:aa0e1ad88af5 | 33 | { |
mikewebkist | 2:fd6008aa85cd | 34 | LED[x] = 0; |
mikewebkist | 1:47c61bf9c81e | 35 | wait(delayTime); |
Experiment626 | 0:aa0e1ad88af5 | 36 | } |
mikewebkist | 4:eea1a71b9a11 | 37 | } |
mikewebkist | 4:eea1a71b9a11 | 38 | |
mikewebkist | 6:fe79549495e0 | 39 | void bounceMode(float delayTime) |
mikewebkist | 6:fe79549495e0 | 40 | { |
mikewebkist | 6:fe79549495e0 | 41 | for(int x=0; x<7; x++) |
mikewebkist | 6:fe79549495e0 | 42 | { |
mikewebkist | 6:fe79549495e0 | 43 | LED[x] = 1; |
mikewebkist | 6:fe79549495e0 | 44 | wait(delayTime); |
mikewebkist | 6:fe79549495e0 | 45 | LED[x] = 0; |
mikewebkist | 6:fe79549495e0 | 46 | } |
mikewebkist | 6:fe79549495e0 | 47 | |
mikewebkist | 6:fe79549495e0 | 48 | for(int x=6; x>=0; x--) |
mikewebkist | 6:fe79549495e0 | 49 | { |
mikewebkist | 6:fe79549495e0 | 50 | LED[x] = 1; |
mikewebkist | 6:fe79549495e0 | 51 | wait(delayTime); |
mikewebkist | 6:fe79549495e0 | 52 | LED[x] = 0; |
mikewebkist | 6:fe79549495e0 | 53 | } |
mikewebkist | 6:fe79549495e0 | 54 | } |
mikewebkist | 6:fe79549495e0 | 55 | |
mikewebkist | 9:7ae675372031 | 56 | void multiSweepMode(float delayTime) |
mikewebkist | 9:7ae675372031 | 57 | { |
mikewebkist | 9:7ae675372031 | 58 | for(int w=1; w<13; w++) |
mikewebkist | 9:7ae675372031 | 59 | { |
mikewebkist | 9:7ae675372031 | 60 | for(int x=1-w; x<13; x++) |
mikewebkist | 9:7ae675372031 | 61 | { |
mikewebkist | 9:7ae675372031 | 62 | for(int z=0; z<w; z++) { |
mikewebkist | 9:7ae675372031 | 63 | if(x+z>=0 && x+z < 7) { |
mikewebkist | 9:7ae675372031 | 64 | LED[x+z]=1; |
mikewebkist | 9:7ae675372031 | 65 | } |
mikewebkist | 9:7ae675372031 | 66 | } |
mikewebkist | 9:7ae675372031 | 67 | wait(delayTime); |
mikewebkist | 9:7ae675372031 | 68 | for(int z=0; z<w; z++) { |
mikewebkist | 9:7ae675372031 | 69 | if(x+z>=0 && x+z < 7) { |
mikewebkist | 9:7ae675372031 | 70 | LED[x+z]=0; |
mikewebkist | 9:7ae675372031 | 71 | } |
mikewebkist | 9:7ae675372031 | 72 | } |
mikewebkist | 9:7ae675372031 | 73 | |
mikewebkist | 9:7ae675372031 | 74 | } |
mikewebkist | 9:7ae675372031 | 75 | } |
mikewebkist | 9:7ae675372031 | 76 | } |
mikewebkist | 9:7ae675372031 | 77 | |
mikewebkist | 7:c9e707fc8aa3 | 78 | void pileMode(float delayTime) |
mikewebkist | 7:c9e707fc8aa3 | 79 | { |
mikewebkist | 7:c9e707fc8aa3 | 80 | for(int x=0; x<7; x++) |
mikewebkist | 7:c9e707fc8aa3 | 81 | { |
mikewebkist | 7:c9e707fc8aa3 | 82 | for(int y=0; y<(6-x); y++) { |
mikewebkist | 7:c9e707fc8aa3 | 83 | LED[y]=1; |
mikewebkist | 7:c9e707fc8aa3 | 84 | wait(delayTime); |
mikewebkist | 7:c9e707fc8aa3 | 85 | LED[y]=0; |
mikewebkist | 7:c9e707fc8aa3 | 86 | } |
mikewebkist | 7:c9e707fc8aa3 | 87 | LED[6-x]=1; |
mikewebkist | 7:c9e707fc8aa3 | 88 | } |
mikewebkist | 8:41a5757f3bb9 | 89 | wait(delayTime); |
mikewebkist | 7:c9e707fc8aa3 | 90 | for(int x=0; x<7; x++) |
mikewebkist | 7:c9e707fc8aa3 | 91 | { |
mikewebkist | 7:c9e707fc8aa3 | 92 | LED[x] = 0; |
mikewebkist | 7:c9e707fc8aa3 | 93 | } |
mikewebkist | 7:c9e707fc8aa3 | 94 | } |
mikewebkist | 7:c9e707fc8aa3 | 95 | |
mikewebkist | 5:48b81f5fd186 | 96 | void randomMode(float delayTime) |
mikewebkist | 4:eea1a71b9a11 | 97 | { |
mikewebkist | 5:48b81f5fd186 | 98 | for(int x=0; x<128; x++) { |
mikewebkist | 5:48b81f5fd186 | 99 | rndLED = (int) RNG.getByte() % 8; |
mikewebkist | 5:48b81f5fd186 | 100 | LED[rndLED]=1; |
mikewebkist | 6:fe79549495e0 | 101 | wait(0.01); |
mikewebkist | 6:fe79549495e0 | 102 | LED[rndLED]=0; |
mikewebkist | 5:48b81f5fd186 | 103 | wait(delayTime); |
mikewebkist | 5:48b81f5fd186 | 104 | } |
mikewebkist | 5:48b81f5fd186 | 105 | } |
mikewebkist | 5:48b81f5fd186 | 106 | |
mikewebkist | 5:48b81f5fd186 | 107 | void binaryMode(float delayTime) { |
mikewebkist | 3:f3e484ae4439 | 108 | for(int n=0; n<128; n++) { |
mikewebkist | 3:f3e484ae4439 | 109 | for(int b=0; b<7; b++) { |
mikewebkist | 3:f3e484ae4439 | 110 | if(n & (1 << b)) { |
mikewebkist | 3:f3e484ae4439 | 111 | LED[6 - b]=1; |
mikewebkist | 3:f3e484ae4439 | 112 | } |
mikewebkist | 3:f3e484ae4439 | 113 | } |
mikewebkist | 5:48b81f5fd186 | 114 | wait(delayTime); |
mikewebkist | 3:f3e484ae4439 | 115 | for(int b=0; b<7; b++) { |
mikewebkist | 3:f3e484ae4439 | 116 | LED[b]=0; |
mikewebkist | 3:f3e484ae4439 | 117 | } |
mikewebkist | 3:f3e484ae4439 | 118 | } |
mikewebkist | 5:48b81f5fd186 | 119 | } |
mikewebkist | 5:48b81f5fd186 | 120 | |
mikewebkist | 5:48b81f5fd186 | 121 | void grayMode(float delayTime) { |
mikewebkist | 7:c9e707fc8aa3 | 122 | for(int b=0; b<7; b++) { LED[b]=0; } |
mikewebkist | 7:c9e707fc8aa3 | 123 | |
mikewebkist | 7:c9e707fc8aa3 | 124 | int prev=0; |
mikewebkist | 7:c9e707fc8aa3 | 125 | for(int n=1; n<128; n++) { |
mikewebkist | 7:c9e707fc8aa3 | 126 | int prevGray = (prev >> 1) ^ prev; |
mikewebkist | 4:eea1a71b9a11 | 127 | int gray = (n >> 1) ^ n; |
mikewebkist | 7:c9e707fc8aa3 | 128 | int diffBit = prevGray ^ gray; |
mikewebkist | 4:eea1a71b9a11 | 129 | for(int b=0; b<7; b++) { |
mikewebkist | 7:c9e707fc8aa3 | 130 | if(diffBit & (1 << b)) { |
mikewebkist | 7:c9e707fc8aa3 | 131 | LED[6 - b] = (diffBit & prevGray) ? 0 : 1; |
mikewebkist | 7:c9e707fc8aa3 | 132 | break; |
mikewebkist | 4:eea1a71b9a11 | 133 | } |
mikewebkist | 4:eea1a71b9a11 | 134 | } |
mikewebkist | 7:c9e707fc8aa3 | 135 | prev = n; |
mikewebkist | 5:48b81f5fd186 | 136 | wait(delayTime); |
mikewebkist | 4:eea1a71b9a11 | 137 | } |
mikewebkist | 5:48b81f5fd186 | 138 | } |
mikewebkist | 5:48b81f5fd186 | 139 | |
mikewebkist | 12:c94348aca325 | 140 | void bumpMode(float delayTime) { |
mikewebkist | 12:c94348aca325 | 141 | for(int x=0; x<3; x++) { |
mikewebkist | 12:c94348aca325 | 142 | LED[x]=1; |
mikewebkist | 12:c94348aca325 | 143 | LED[6-x]=1; |
mikewebkist | 12:c94348aca325 | 144 | wait(delayTime * 2); |
mikewebkist | 12:c94348aca325 | 145 | LED[x]=0; |
mikewebkist | 12:c94348aca325 | 146 | LED[6-x]=0; |
mikewebkist | 12:c94348aca325 | 147 | } |
mikewebkist | 12:c94348aca325 | 148 | LED[3]=1; |
mikewebkist | 12:c94348aca325 | 149 | wait(delayTime * 2); |
mikewebkist | 12:c94348aca325 | 150 | LED[3]=0; |
mikewebkist | 12:c94348aca325 | 151 | for(int x=0; x<3; x++) { |
mikewebkist | 12:c94348aca325 | 152 | LED[2-x]=1; |
mikewebkist | 12:c94348aca325 | 153 | LED[4+x]=1; |
mikewebkist | 12:c94348aca325 | 154 | wait(delayTime * 2); |
mikewebkist | 12:c94348aca325 | 155 | LED[2-x]=0; |
mikewebkist | 12:c94348aca325 | 156 | LED[4+x]=0; |
mikewebkist | 12:c94348aca325 | 157 | } |
mikewebkist | 12:c94348aca325 | 158 | wait(delayTime * 4); |
mikewebkist | 12:c94348aca325 | 159 | } |
mikewebkist | 12:c94348aca325 | 160 | |
mikewebkist | 5:48b81f5fd186 | 161 | int main() |
mikewebkist | 5:48b81f5fd186 | 162 | { |
mikewebkist | 5:48b81f5fd186 | 163 | while(1) |
mikewebkist | 5:48b81f5fd186 | 164 | { |
mikewebkist | 12:c94348aca325 | 165 | bumpMode(0.05); |
mikewebkist | 12:c94348aca325 | 166 | int x = (int) RNG.getByte() % 7; |
mikewebkist | 11:be76674d2dbd | 167 | switch(x) { |
mikewebkist | 11:be76674d2dbd | 168 | case 0: |
mikewebkist | 14:cdc203a0bdc1 | 169 | binaryMode(0.025); |
mikewebkist | 11:be76674d2dbd | 170 | break; |
mikewebkist | 11:be76674d2dbd | 171 | case 1: |
mikewebkist | 14:cdc203a0bdc1 | 172 | randomMode(0.025); |
mikewebkist | 11:be76674d2dbd | 173 | break; |
mikewebkist | 11:be76674d2dbd | 174 | case 2: |
mikewebkist | 14:cdc203a0bdc1 | 175 | grayMode(0.025); |
mikewebkist | 11:be76674d2dbd | 176 | break; |
mikewebkist | 11:be76674d2dbd | 177 | case 3: |
mikewebkist | 11:be76674d2dbd | 178 | bounceMode(0.05); |
mikewebkist | 11:be76674d2dbd | 179 | break; |
mikewebkist | 11:be76674d2dbd | 180 | case 4: |
mikewebkist | 11:be76674d2dbd | 181 | pileMode(0.05); |
mikewebkist | 11:be76674d2dbd | 182 | break; |
mikewebkist | 11:be76674d2dbd | 183 | case 5: |
mikewebkist | 14:cdc203a0bdc1 | 184 | multiSweepMode(0.025); |
mikewebkist | 11:be76674d2dbd | 185 | break; |
mikewebkist | 12:c94348aca325 | 186 | case 6: |
mikewebkist | 12:c94348aca325 | 187 | sweepMode(0.05); |
mikewebkist | 12:c94348aca325 | 188 | break; |
mikewebkist | 11:be76674d2dbd | 189 | } |
mikewebkist | 12:c94348aca325 | 190 | bumpMode(0.05); |
mikewebkist | 14:cdc203a0bdc1 | 191 | WakeUp::set(30); |
mikewebkist | 14:cdc203a0bdc1 | 192 | myPowerDown(); |
Experiment626 | 0:aa0e1ad88af5 | 193 | } |
Experiment626 | 0:aa0e1ad88af5 | 194 | } |