Last version of the Balloons Lamp Module

Dependencies:   NeoStrip mbed nRF24L01P

Fork of NeoPixels by Allen Wild

Revision:
1:5ec5616d984e
Parent:
0:f38492690f0e
--- a/main.cpp	Wed Mar 12 18:41:42 2014 +0000
+++ b/main.cpp	Thu Nov 19 12:26:19 2015 +0000
@@ -1,47 +1,111 @@
 /*
- * Adafruit NeoPixel 8x8 matrix example
- *
- * This program displays a couple simple patterns on an 8x8 NeoPixel matrix.
- *
- * 3 buttons are used for DigitalIns, 2 control the brightness up and down,
- * and the third switches patterns
+ * Balloons module Lampe
+ * Autheur : Yann DOUZE douze.yann@gmail.com
+ * Date : 2015/06/24
+ * Version 0.9 
  */
 
 #include "mbed.h"
 #include "NeoStrip.h"
-#include "gt.h"
+#include "nRF24L01P.h"
+#define N 12
+#define PATTERNS 4
+#define TRANSFER_SIZE   4
+
+
+Serial pc(USBTX, USBRX); // tx, rx
 
-#define N 64
-#define PATTERNS 3
+nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10);    // mosi, miso, sck, csn, ce, irq
 
-int hueToRGB(float h);
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut myled4(LED4);
+
 void pattern0();
 void pattern1();
 void pattern2();
+void pattern3();
+void heartbeat();
+void circle_light();
+
+unsigned char R=255;
+unsigned char G=255;
+unsigned char B=100;
+
+/*
+unsigned char R=249;
+unsigned char G=66;
+unsigned char B=58;
+*/
+//void pattern4();
+
 
 // array of function pointers to the various patterns
-void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2};
+void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2, &pattern3};
 
-NeoStrip strip(p18, N);
-DigitalIn b1(p20); // brightness up
-DigitalIn b2(p19); // brightness down
-DigitalIn b3(p21); // next pattern
+NeoStrip strip(p20, N);
+DigitalIn b1(p22); // brightness up
+DigitalIn b2(p21); // brightness down
+DigitalIn b3(p19); // bouton mamie
 
 // timer used for debugging
 Timer timer;
+float bright = 0.95;	// 20% is plenty for indoor use
+unsigned int sens=0;
+float bri =0.01;
+
+void circle_light()
+{
+	for (int i = 0; i < N; i++)
+	{
+		strip.setPixel(i, 0, 0, 0); //Black
+	}	  
+	strip.write();//eteint toute les leds
+	for (int i = 0; i < N; i++) //allume les leds une à une
+	{
+		strip.setPixel(i, R, G, B); //Yellow
+		strip.write();
+		wait_ms(100);
+	}	  
+}
 
 int main()
 {
 	b1.mode(PullDown);
 	b2.mode(PullDown);
 	b3.mode(PullDown);
-	
+	char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
+	txData[0]='M'; //M comme Mami
+	txData[1]='B'; //B comme Bouton
+	txData[3]=0x31; //comme 1 en ASCII
+    int txDataCnt = 4;
+    int rxDataCnt = 0;
+	int SendCounter=0;
 	int pattern = 0;
-	float bright = 0.2;	// 20% is plenty for indoor use
+	float bright = 0.5;	// 20% is plenty for indoor use
 	bool b3o = b3;		// old copy of button 3 to poll for changes
 
 	strip.setBrightness(bright);	// set default brightness
 	
+	 my_nrf24l01p.powerUp();
+
+    // Display the (default) setup of the nRF24L01+ chip
+    pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
+    pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
+    pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
+    pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
+    pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
+
+    pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
+
+    my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
+
+    my_nrf24l01p.setReceiveMode();
+    my_nrf24l01p.enable();
+    
+    circle_light();
+
 	while (true)
 	{
 		timer.reset(); // use a timer to measure loop execution time for debugging purposes
@@ -65,14 +129,56 @@
 			strip.setBrightness(bright);
 		}
 		
-		// button 3 changes the pattern, only do stuff when its state has changed
 		if (b3 != b3o)
 		{
-			if (b3 && ++pattern == PATTERNS)
+			b3o = b3;
+			myled1 = !myled1;
+			if (b3)
+			{
 				pattern = 0;
-			b3o = b3;
+            	pc.printf("Mamie a appuye sur le bouton ! \r\n");
+            	txData[2]=SendCounter++;
+            	if(SendCounter==255) SendCounter=0;
+            	my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );      
+            	circle_light();
+            }
 		}
 		
+		if ( my_nrf24l01p.readable() ) {
+
+            // ...read the data into the receive buffer
+            rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
+
+            // Display the receive buffer contents via the host serial link
+            //for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
+			if(rxData[3]==12){
+                pc.putc( rxData[0] );
+                pc.putc( rxData[1] );
+                pc.printf(" Message %d \r\n",rxData[2]);
+                pattern=1;
+                
+            }
+            if(rxData[3]==13){
+                pc.putc( rxData[0] );
+                pc.putc( rxData[1] );
+                pc.printf(" Message %d \r\n",rxData[2]);
+                pattern=2;
+                
+            }
+            if(rxData[3]==14){
+                pc.putc( rxData[0] );
+                pc.putc( rxData[1] );
+                pc.printf(" Message %d \r\n",rxData[2]);
+                pattern=3;
+                
+            }
+			
+            // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
+            myled2 = !myled2;
+            sens=0;
+            bri =0.01;
+        }
+		
 		// run the pattern update function which sets the strip's pixels
 		patterns[pattern]();
 		strip.write();
@@ -88,93 +194,56 @@
 
 // pattern0 displays a static image
 void pattern0()
-{
-	strip.setPixels(0, N, gt_img);
-}
-
-// display a shifting rainbow, all colors have maximum
-// saturation and value, with evenly spaced hue
-void pattern1()
-{
-	static float dh = 360.0 / N;
-	static float x = 0;
-
-	for (int i = 0; i < N; i++)
-		strip.setPixel(i, hueToRGB((dh * i) - x));
-	
-	x += 1;
-	if (x > 360)
-		x = 0;
-}
-
-// display a shifting gradient between red and blue
-void pattern2()
-{
-	// offset for each pixel to allow the pattern to move
-	static float x = 0;
-
-	float r, b, y;
-
+{   
 	for (int i = 0; i < N; i++)
 	{
-		// y is a scaled position between 0 (red) and 1.0 (blue)
-		y = 1.0 * i / (N - 1) + x;
-		if (y > 1)
-			y -= 1;
+		strip.setPixel(i, R, G, B); //Yellow
+	}
+	strip.setBrightness(bright);
+	
+}
 
-		// if on the left half, red is decreasing and blue is increasng
-		if (y < 0.5)
-		{
-			b = 2 * y;
-			r = 1 - b;
-		}
-
-		// else red is increasing and blue is decreasing
-		else
-		{
-			r = 2 * (y - 0.5);
-			b = 1 - r;
-		}
-
-		// scale to integers and set the pixel
-		strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200));
+void heartbeat()
+{
+	if (bri>0.95f) sens =1;
+	if (bri<0.25f) sens=0;
+	
+	if (sens==1)
+	{
+		bri -= 0.007;
 	}
 
-	x += 0.003;
-	if (x > 1)
-		x = 0;
+	if (sens==0)
+	{
+		bri += 0.007;
+	}
+	strip.setBrightness(bri);
 }
 
-// Converts HSV to RGB with the given hue, assuming
-// maximum saturation and value
-int hueToRGB(float h)
+void pattern1()
 {
-	// lots of floating point magic from the internet and scratching my head
-	float r, g, b;
-	if (h > 360)
-		h -= 360;
-	if (h < 0)
-		h += 360;
-	int i = (int)(h / 60.0);
-	float f = (h / 60.0) - i;
-	float q = 1 - f;
-	
-	switch (i % 6)
+	for (int i = 0; i < N; i++)
 	{
-		case 0: r = 1; g = f; b = 0; break;
-		case 1: r = q; g = 1; b = 0; break;
-		case 2: r = 0; g = 1; b = f; break;
-		case 3: r = 0; g = q; b = 1; break;
-		case 4: r = f; g = 0; b = 1; break;
-		case 5: r = 1; g = 0; b = q; break;
-		default: r = 0; g = 0; b = 0; break;
+		strip.setPixel(i, 0, 191, 255); //DeepSkyBlue
 	}
-	
-	// scale to integers and return the packed value
-	uint8_t R = (uint8_t)(r * 255);
-	uint8_t G = (uint8_t)(g * 255);
-	uint8_t B = (uint8_t)(b * 255);
-
-	return (R << 16) | (G << 8) | B;
+	heartbeat();
 }
 
+void pattern2()
+{
+	for (int i = 0; i < N; i++)
+	{
+		strip.setPixel(i, 255, 0, 255); //magenta
+	}	
+	heartbeat();
+}
+
+void pattern3()
+{
+	for (int i = 0; i < N; i++)
+	{
+		strip.setPixel(i, 255, 127, 0); //DarkOrange
+	}	
+	heartbeat();
+}
+