First Simon Says version
Dependencies: mbed DebounceIn WS2812
Revision 0:47b1ab4dd893, committed 2020-02-13
- Comitter:
- elab
- Date:
- Thu Feb 13 09:07:14 2020 +0000
- Commit message:
- first version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebounceIn.lib Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/DebounceIn/#91a2e988ba9d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/LED_WS2812.cpp Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,209 @@ +#include "LED_WS2812.h" + + + +LED_WS2812::LED_WS2812(PinName _PinOut, int _nbLeds) { + nbLeds = _nbLeds; + double period_ns; + Timer tuneTimings; + int sum = 0; + int nopRun; + + for(int kavg = 0; kavg<20;kavg++) { + tuneTimings.reset(); + tuneTimings.start(); + for(int nopCount=0; nopCount < 10000; nopCount ++) { + __nop(); + } + tuneTimings.stop(); + nopRun = tuneTimings.read_us(); + sum=nopRun+sum; + } + period_ns = sum/200; /* *1000 for nanoseconds /20 average /10000 count */ + + int zero_high = ZERO_HIGH/period_ns; + int zero_low = ZERO_LOW/period_ns; + int one_high = ONE_HIGH/period_ns; + int one_low = ONE_LOW/period_ns; + + ws = new WS2812(_PinOut, nbLeds, zero_high, zero_low, one_high, one_low); + ws->useII(WS2812::PER_PIXEL); + + pxArray = new PixelArray(nbLeds); + pxInsert = new PixelArray(nbLeds); + ResetColor(); + rotationState = false; + rotationPosition = nbLeds-1; + blinkState = false; + blinkONOFF = false; + intensity = 0xff; +}; + +LED_WS2812::~LED_WS2812() { + delete(ws); + delete(pxArray); +} + + + +void LED_WS2812::SetColor(LED_COLORS _color, int position) { + SetColor((unsigned int) _color, position); +}; + +void LED_WS2812::SetColor(unsigned int _color, int position) { + if(position < nbLeds && position >=0 ) { + pxArray->Set(position, _color); + pxArray->SetI(position,intensity); + + } + __writeBuf(0); + nbInsert = 0; + if(rotationState) StopRotation(); + rotationPosition = nbLeds; +}; + +void LED_WS2812::SetColor(LED_COLORS _color) { + SetColor((unsigned int) _color); +}; + +void LED_WS2812::SetColor(unsigned int _color) { + for(int i=0;i<nbLeds;i++) { + pxArray->Set(i, _color); + pxArray->SetI(i,intensity); + } + __writeBuf(0); + nbInsert = 0; + if(rotationState) StopRotation(); + rotationPosition = nbLeds; +}; + + +void LED_WS2812::ResetColor() { + SetColor(BLACK); + + } + +void LED_WS2812::__writeBuf(int z) { + ws->write_offsets(pxArray->getBuf(),z,z,z); + wait(0.01); + } + +void LED_WS2812::__insert2buf() { + for(int i=0;i<nbLeds;i++) { + pxArray->Set(i, pxInsert->Get(i%nbInsert)); + } + __writeBuf(0); + rotationPosition = nbLeds; +} + +void LED_WS2812::__insertColor(unsigned int _color, int _intensity) { + pxInsert->Set(nbInsert%nbLeds,_color); + pxInsert->SetI(nbInsert%nbLeds,_intensity); + nbInsert++; + +}; + +void LED_WS2812::InsertColor(unsigned int _color) { + InsertColor(_color,intensity*100/0xFF); +}; + + +void LED_WS2812::InsertColor(unsigned int _color, float brightness) { + int pixelIntensity = brightness*0xFF/100; + __insertColor(_color, pixelIntensity); + __insert2buf(); +}; + + +void LED_WS2812::InsertColorNtimes(int N, unsigned int _color, float brightness) { + for(int i=0;i<N;i++) { + InsertColor(_color, brightness); + } +}; + +void LED_WS2812::InsertColorNtimes(int N, unsigned int _color) { + InsertColorNtimes(N, _color, intensity*100/0xFF); +}; + + +void LED_WS2812::InsertColor(LED_COLORS _color) { + InsertColor((unsigned int)_color); +}; + + +void LED_WS2812::InsertColor(LED_COLORS _color, float brightness) { + InsertColor((unsigned int)_color, brightness); +}; + + +void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color, float brightness) { + InsertColorNtimes(N, (unsigned int)_color, brightness); +}; + +void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color) { + InsertColorNtimes(N, _color, intensity*100/0xFF); +}; + + +void LED_WS2812::SetIntensity(float perCent) { + intensity = perCent*0xFF/100; + ws->setII(intensity); + for(int i=0;i<nbLeds;i++) { + pxArray->SetI(i,intensity); + } +} + +void LED_WS2812::StartRotation(float interval) { + if(rotationState==false) { + rotationState = true; + LEDSystemTick.attach_us(callback(this, &LED_WS2812::Rotate), interval*1000000); + } +} + + +void LED_WS2812::StopRotation() { + if(rotationState==true) { + rotationState = false; + rotationPosition = 0; + LEDSystemTick.detach(); + } +} + +void LED_WS2812::Rotate() { + rotationPosition--; + if (rotationPosition == -1) + rotationPosition = nbLeds-1; + if(!blinkState) __writeBuf(rotationPosition); +} + + +void LED_WS2812::StartBlink(float interval) { + StopBlink(); + if(blinkState==false) { + blinkState = true; + LEDBlinkSystemTick.attach_us(callback(this, &LED_WS2812::Blink), interval*1000000); + } +} + + +void LED_WS2812::StopBlink() { + if(blinkState==true) { + blinkState = false; + LEDBlinkSystemTick.detach(); + } +} + +void LED_WS2812::Blink() { + blinkONOFF = !blinkONOFF; + if (blinkONOFF) + __writeBuf(rotationPosition); + else { + ws->useII(WS2812::GLOBAL); + ws->setII(0); + __writeBuf(rotationPosition); + ws->useII(WS2812::PER_PIXEL); + ws->setII(intensity); + + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/LED_WS2812.h Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,84 @@ + +#ifndef LED_WS2812_H +#define LED_WS2812_H + +#include "mbed.h" +#include "WS2812.h" +#include "PixelArray.h" + +/* +#define ZERO_HIGH 250.0 +#define ZERO_LOW 1000.0 +#define ONE_HIGH 1000.0 +#define ONE_LOW 250.0 +*/ +#define ZERO_HIGH 200.0 +#define ZERO_LOW 800.0 +#define ONE_HIGH 800.0 +#define ONE_LOW 200.0 + + typedef enum _LED_COLORS { + BLUE = 0x0000FF, + LIGHTBLUE = 0x00FFF6, + RED = 0xFF0000, + ORANGE = 0xFF3500, + GREEN = 0X00FF00, + BLACK = 0X000000, + WHITE = 0XFFFFFF, + PURPLE = 0XFF00FF, + PINK = 0XFF84A3, + YELLOW = 0XFFFF00, + DARK_YELLOW = 0X555500, + DEFAULT = 0x000000 + } LED_COLORS; + +class LED_WS2812 +{ +public: + LED_WS2812(PinName _PinOut, int _nbLeds); + ~LED_WS2812(); + void SetColor(LED_COLORS _color, int position); + void SetColor(unsigned int _color, int position); + void SetColor(unsigned int _color); + void SetColor(LED_COLORS _color); + void ResetColor(); + void SetIntensity(float perCent); + + void InsertColor(unsigned int _color); + void InsertColor(unsigned int _color, float brightness); + void InsertColorNtimes(int N, unsigned int _color, float brightness); + void InsertColorNtimes(int N, unsigned int _color); + + void InsertColor(LED_COLORS _color); + void InsertColor(LED_COLORS _color, float brightness); + void InsertColorNtimes(int N, LED_COLORS _color, float brightness); + void InsertColorNtimes(int N, LED_COLORS _color); + + void StartRotation(float interval); // interval in s + void StopRotation(); // + void Rotate(); // One Rotation + + void StartBlink(float interval); // interval in s + void StopBlink(); // + void Blink(); // One Rotation + +private: + void __writeBuf(int z); + void __insert2buf(); + void __insertColor(unsigned int _color, int _intensity); + + WS2812 *ws; + int nbLeds; + PixelArray *pxArray; + int nbInsert; + PixelArray *pxInsert; + Ticker LEDSystemTick; // System Callback for Rotation + Ticker LEDBlinkSystemTick; // System Callback for Rotation + bool rotationState; + bool blinkState; + int rotationPosition; + bool blinkONOFF; // ON = true, OFF = false + int intensity; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/Pixel/PixelArray.cpp Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,173 @@ +#include "PixelArray.h" + +PixelArray::PixelArray(int size) +{ + pbufsize = size; + pbuf = new int[pbufsize]; + SetAll(0x0); // initialise memory to zeros + +} + +PixelArray::~PixelArray() +{ + delete[] pbuf; +} + +void PixelArray::SetAll(unsigned int value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel(i,value); + } +} + + +void PixelArray::SetAllI(unsigned char value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel_component(i,3,value); + } +} + + + +void PixelArray::SetAllR(unsigned char value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel_component(i,2,value); + } +} + +void PixelArray::SetAllG(unsigned char value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel_component(i,1,value); + } +} + +void PixelArray::SetAllB(unsigned char value) +{ + // for each pixel + for (int i=0 ; i < pbufsize; i++) { + __set_pixel_component(i,0,value); + } +} + + + + + +void PixelArray::Set(int i, unsigned int value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel(i,value); + } +} + + + +void PixelArray::SetI(int i, unsigned char value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel_component(i,3,value); + } +} + + +void PixelArray::SetR(int i, unsigned char value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel_component(i,2,value); + } +} + +void PixelArray::SetG(int i, unsigned char value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel_component(i,1,value); + } +} + +void PixelArray::SetB(int i, unsigned char value) +{ + if ((i >= 0) && (i < pbufsize)) { + __set_pixel_component(i,0,value); + } +} + + +unsigned int PixelArray::Get(int i) +{ + return __get_pixel(i); +} + +unsigned int PixelArray::GetI(int i) +{ + return __get_pixel_component(i,3); +} + +unsigned int PixelArray::GetR(int i) +{ + return __get_pixel_component(i,2); +} + +unsigned int PixelArray::GetG(int i) +{ + return __get_pixel_component(i,1); +} + +unsigned int PixelArray::GetB(int i) +{ + return __get_pixel_component(i,0); +} + + + +int* PixelArray::getBuf() +{ + return (pbuf); +} + + +// set either the I,R,G,B value of specific pixel channel +void PixelArray::__set_pixel_component(int index, int channel, int value) +{ + + // AND with 0x00 shifted to the right location to clear the bits + pbuf[index] &= ~(0xFF << (8 * channel)); + + // Set the bits with an OR + pbuf[index] |= (value << (8 * channel)); +} + + +// set either the I,R,G,B value of specific pixel channel +void PixelArray::__set_pixel(int index, int value) +{ + // AND with 0x00 shifted to the right location to clear the bits + pbuf[index] = value; +} + + +// get specific pixel +int PixelArray::__get_pixel(int index) +{ + if ((index >= 0) && (index < pbufsize)) { + return pbuf[index]; + } else { + return 0; + } +} + + +// get specific pixel +int PixelArray::__get_pixel_component(int index, int channel) +{ + // AND with 0xFF shifted to the right location to get the bits + return ( (__get_pixel(index) & (0xFF << (8 * channel))) >> (8*channel) ); +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/Pixel/PixelArray.h Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,77 @@ +/* Copyright (c) 2012 cstyles, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef PixelArray_H +#define PixelArray_H + +#include "mbed.h" + +//!Library for the WS2812 RGB LED with integrated controller +/*! +PixelArray +*/ +class PixelArray +{ +public: + //!Creates an instance of the class. + /*! + Pixel Array + */ + PixelArray(int); + + /*! + Destroys instance. + */ + ~PixelArray(); + + int* getBuf(); + + void SetAll(unsigned int); + void SetAllI(unsigned char); + void SetAllR(unsigned char); + void SetAllG(unsigned char); + void SetAllB(unsigned char); + + // location, value + void Set(int, unsigned int); + void SetI(int, unsigned char); + void SetR(int, unsigned char); + void SetG(int, unsigned char); + void SetB(int, unsigned char); + + unsigned int Get(int); + unsigned int GetI(int); + unsigned int GetR(int); + unsigned int GetG(int); + unsigned int GetB(int); + +private: + + int *pbuf; + int pbufsize; + + void __set_pixel_component(int index, int channel, int value); + void __set_pixel(int index, int value); + + int __get_pixel_component(int index, int channel); + int __get_pixel(int index); + +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LED_WS2812/WS2812.lib Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/bridadan/code/WS2812/#6e647820f587
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,158 @@ +#include "mbed.h" +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include "LED_WS2812.h" + +Serial pc(USBTX, USBRX); +DigitalOut myled(LED1); +DigitalIn greenButton(D3,PullUp); +DigitalIn redButton(D4,PullUp); +DigitalIn blueButton(D5,PullUp); +DigitalIn yellowButton(D6, PullUp); +LED_WS2812 led(D9,4); + +int buttonArray[32]; +int array[32]; // LEDS colors array + +int readButton(int turn){ + int currentValueG, currentValueR, currentValueB, currentValueY, status=0; + int max=400; + for (int i=0;i<32;i++){ + buttonArray[i]=55; + } + printf("readButton\n"); + for (int i=0; i<= turn; i++){ + for (int t=0; t<max; t++){ + currentValueG = greenButton.read(); + currentValueR = redButton.read(); + currentValueB = blueButton.read(); + currentValueY = yellowButton.read(); + if (currentValueG == 0){ + printf("Green button\n"); + wait_ms(250); + buttonArray[i]=0; + t=max; + status=0; + } + else if (currentValueR == 0){ + printf("RedButton\n"); + wait_ms(250); + buttonArray[i]=1; + t=max; + status=0; + } + else if(currentValueB == 0){ + printf("BlueButton\n"); + wait_ms(250); + buttonArray[i]=2; + t=max; + status=0; + } + else if(currentValueY == 0){ + printf("Yellow button\n"); + wait_ms(250); + buttonArray[i]=3; + t=max; + status=0; + } + else if(t>=(max-1)){ + printf("time out !!! \n"); + status=1; + } + wait_ms(10); + } + } + + for(int j=0; j<=turn; j++) { + printf("buttonArray[%d]=%d\n", j, buttonArray[j]); + } + printf("end of readButton \n"); + return status; +} + +int main() { + int maxTurn=5, status, diff=0; + srand(time(0)); + led.SetColor(BLACK); + led.SetIntensity(25); + for(int t=0; t<=maxTurn; t++){ + // generate the array and ON the LEDs + printf("turn = %d\n", t); + array[t]=rand()%4; + for (int i=0; i<=t; i++){ + switch(array[i]) + { + case 0: + printf(" >>>>>>> green\n"); + led.SetColor(GREEN,0); + wait_ms(500); + led.SetColor(BLACK); + wait_ms(50); + break; + case 1: + printf(" >>>>>>> red\n"); + led.SetColor(RED,1); + wait_ms(500); + led.SetColor(BLACK); + wait_ms(50); + break; + case 2: + printf(" >>>>>>> blue\n"); + led.SetColor(BLUE,2); + wait_ms(500); + led.SetColor(BLACK); + wait_ms(50); + break; + case 3: + printf(" >>>>>>> yellow\n"); + led.SetColor(YELLOW,3); + wait_ms(500); + led.SetColor(BLACK); + wait_ms(50); + break; + default: + printf(" >>>>>>> no color \n"); + break; + } + } + status = readButton(t); + // compare two arrays + if (status==1){ + printf("Time out, game over !\n"); + led.SetColor(ORANGE); + t=maxTurn;// force t to maxTurn to exit + } + else { + for(int j=0; j<=t; j++){ + if(array[j]!=buttonArray[j]){ + j=t; + diff=1; + } + + } + if (diff==1){ + t=maxTurn; + printf("Color no match, Game Over !\n"); + led.InsertColor(RED); + led.StartBlink(0.2); + } + else{ + if(t<maxTurn){ + printf("t=%d\n",t); + printf("next level !!!\n"); + } + else{ + printf("t=%d\n",t); + printf("You win !\n"); + led.InsertColor(GREEN); + led.StartBlink(0.2); + wait(3); + led.StopBlink(); + } + } + } + printf("Turn finished\n"); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main_copy.txt Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,140 @@ +#include "mbed.h" +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include "LED_WS2812.h" + +Serial pc(USBTX, USBRX); +DigitalOut myled(LED1); +DigitalIn greenButton(D3,PullUp); +DigitalIn redButton(D4,PullUp); +DigitalIn blueButton(D5,PullUp); +DigitalIn yellowButton(D6, PullUp); +LED_WS2812 led(D9,4); + +int buttonArray[32]; +int array[32]; // LEDS colors array + +int readButton(int turn){ + int currentValueG, currentValueR, currentValueB, currentValueY, status=0; + int max=500; + for (int i=0;i<32;i++){ + buttonArray[i]=55; + } + printf("readButton\n"); + for (int i=0; i<= turn; i++){ + for (int t=0; t<max; t++){ + currentValueG = greenButton.read(); + currentValueR = redButton.read(); + currentValueB = blueButton.read(); + currentValueY = yellowButton.read(); + if (currentValueG == 0){ + printf("Green button\n"); + wait_ms(250); + buttonArray[i]=0; + t=max; + status=0; + } + else if (currentValueR == 0){ + printf("RedButton\n"); + wait_ms(250); + buttonArray[i]=1; + t=max; + status=0; + } + else if(currentValueB == 0){ + printf("BlueButton\n"); + wait_ms(250); + buttonArray[i]=2; + t=max; + status=0; + } + else if(currentValueY == 0){ + printf("Yellow button\n"); + wait_ms(250); + buttonArray[i]=3; + t=max; + status=0; + } + else if(t>=(max-1)){ + printf("time out !!! \n"); + status=1; + } + wait_ms(10); + } + } + + for(int j=0; j<=turn; j++) { + printf("buttonArray[%d]=%d\n", j, buttonArray[j]); + } + printf("end of readButton \n"); + return status; +} + +int main() { + int maxTurn=2, status, diff=0; + srand(time(0)); + led.SetIntensity(25); + led.SetColor(RED); + for(int t=0; t<=maxTurn; t++){ + // generate the array and ON the LEDs + printf("turn = %d\n", t); + array[t]=rand()%4; + for (int i=0; i<=t; i++){ + switch(array[i]) + { + case 0: + printf(" >>>>>>> green\n"); + led.SetColor(0,GREEN); + wait(1); + led.SetColor(0,BLACK); + break; + case 1: + printf(" >>>>>>> red\n"); +// led.SetColor(1,RED); +// wait(1); +// led.SetColor(1,BLACK); + break; + case 2: + printf(" >>>>>>> blue\n"); + led.SetColor(2,BLUE); + wait(1); + led.SetColor(2,BLACK); + break; + case 3: + printf(" >>>>>>> yellow\n"); + led.SetColor(3,YELLOW); + wait(1); + led.SetColor(3,BLACK); + break; + default: + printf(" >>>>>>> no color \n"); + break; + } + } + status = readButton(t); + // compare two arrays + if (status==1){ + printf("Time out, game over !\n"); + t=maxTurn;// force t to maxTurn to exit + } + else { + for(int j=0; j<=t; j++){ + if(array[j]!=buttonArray[j]){ + j=t; + diff=1; + } + + } + if (diff==1){ + t=maxTurn; + printf("Color no match, Game Over !\n"); + } + else{ + printf("You win !\n"); + } + } + printf("Turn finished\n"); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Feb 13 09:07:14 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file