peony3000

Gadgeteer Clock Game (Half Face)

Apr 25th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Presentation;
  5. using Microsoft.SPOT.Presentation.Controls;
  6. using Microsoft.SPOT.Presentation.Media;
  7. using Microsoft.SPOT.Touch;
  8.  
  9. using Gadgeteer.Networking;
  10. using GT = Gadgeteer;
  11. using GTM = Gadgeteer.Modules;
  12. using Gadgeteer.Modules.GHIElectronics;
  13. using GHIElectronics.NETMF.System;
  14.  
  15. namespace Gadgeteer_Clock_Game_01
  16. {
  17.     public partial class Program
  18.     {
  19.         // recalculate everything each time the timer ticks
  20.         GT.Timer timer = new GT.Timer(100, GT.Timer.BehaviorType.RunContinuously);
  21.  
  22.         // declare an array
  23.         double[] xPosLed = new double[12];
  24.         double[] yPosLed = new double[12];
  25.  
  26.         // keep with in a 96 radius circle with the centre at 0,0
  27.         double xPosTarget;
  28.         double yPosTarget;
  29.  
  30.         double xPosCurrent = 0;
  31.         double yPosCurrent = 0;
  32.  
  33.         double[] clockDistance = new double[12];
  34.         //double clockSize = 160;
  35.         double clockSize = 128;
  36.  
  37.         int smoothValue = 4;
  38.  
  39.         // This method is run when the mainboard is powered up or reset.  
  40.         void ProgramStarted()
  41.         {
  42.             // create the clock face
  43.             // a clock face with a radius of 160 units
  44.             // this means that the biggest value is 255 and the smallest is 64
  45.  
  46.             //for (int increment = 1; increment <= 13; increment++)
  47.                 for (int increment = 0; increment <= 6; increment++)
  48.    
  49.              {
  50.                  xPosLed[increment] = MathEx.Cos((MathEx.PI / 6) * increment) * clockSize;
  51.                  yPosLed[increment] = MathEx.Sin((MathEx.PI / 6) * increment) * clockSize;
  52.                  Debug.Print("x " + increment + " " + xPosLed[increment]);
  53.                  Debug.Print("y " + increment + " " + yPosLed[increment]);
  54.              }
  55.  
  56.                 led.GreenBlueSwapped = true;
  57.                 led1.GreenBlueSwapped = true;
  58.                 led2.GreenBlueSwapped = true;
  59.                 led3.GreenBlueSwapped = true;
  60.                 led4.GreenBlueSwapped = true;
  61.                 led5.GreenBlueSwapped = true;
  62.                 //led6.GreenBlueSwapped = true;
  63.                 //led7.GreenBlueSwapped = true;
  64.                 //led8.GreenBlueSwapped = true;
  65.                 //led9.GreenBlueSwapped = true;
  66.                 //led10.GreenBlueSwapped = true;
  67.                 //led11.GreenBlueSwapped = true;
  68.            
  69.             // create a timer robot
  70.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  71.             timer.Start();
  72.  
  73.             // tell everyone
  74.             Debug.Print("Program Started");
  75.         }
  76.  
  77.         void timer_Tick(GT.Timer timer)
  78.         {
  79.            
  80.             // get joystick position
  81.             xPosTarget = 128 - (joystick.GetJoystickPostion().X * 255); // assigns the x-direction from 0 to 1 to the variable holder xPosTarget
  82.             yPosTarget = 128 - (joystick.GetJoystickPostion().Y * 255); // assigns the y-direction from 0 to 1 to the variable holder yPosTarget
  83.            
  84.             // update current position
  85.             xPosCurrent += (xPosTarget - xPosCurrent) / smoothValue;
  86.             yPosCurrent += (yPosTarget - yPosCurrent) / smoothValue;
  87.  
  88.            
  89.  
  90.             Debug.Print(xPosCurrent + " " + yPosCurrent);
  91.                
  92.             // work out clock distances from current position
  93.             for (int increment02 = 0; increment02 <= 6; increment02++)
  94.             {
  95.                 clockDistance[increment02] = MathEx.Sqrt(((xPosLed[increment02] - xPosCurrent) * (xPosLed[increment02] - xPosCurrent)) + ((yPosLed[increment02] - yPosCurrent) * (yPosLed[increment02] - yPosCurrent)));
  96.                 Debug.Print(increment02+" "+clockDistance[increment02]);
  97.                
  98.  
  99.                 switch (increment02)
  100.                 {
  101.                     case 0:
  102.                         led.SetBlueIntensity((int)(clockDistance[increment02]));
  103.                         led.SetRedIntensity(255-(int)(clockDistance[increment02]));
  104.                         break;
  105.                     case 1:
  106.                         led1.SetBlueIntensity((int)(clockDistance[increment02]));
  107.                         led1.SetRedIntensity(255-(int)(clockDistance[increment02]));
  108.                         break;
  109.                     case 2:
  110.                         led2.SetBlueIntensity((int)(clockDistance[increment02]));
  111.                         led2.SetRedIntensity(255-(int)(clockDistance[increment02]));
  112.                         break;
  113.                     case 3:
  114.                         led3.SetBlueIntensity((int)(clockDistance[increment02]));
  115.                         led3.SetRedIntensity(255-(int)(clockDistance[increment02]));
  116.                         break;
  117.                     case 4:
  118.                         led4.SetBlueIntensity((int)(clockDistance[increment02]));
  119.                         led4.SetRedIntensity(255-(int)(clockDistance[increment02]));
  120.                         break;
  121.                     case 5:
  122.                         led5.SetBlueIntensity((int)(clockDistance[increment02]));
  123.                         led5.SetRedIntensity(255-(int)(clockDistance[increment02]));
  124.                         break;
  125.                     case 6:
  126.                         //led6.SetBlueIntensity((int)(clockDistance[increment02]));
  127.                         break;
  128.                     case 7:
  129.                         //led7.SetBlueIntensity((int)(clockDistance[increment02]));
  130.                         break;
  131.                     case 8:
  132.                         //led8.SetBlueIntensity((int)(clockDistance[increment02]));
  133.                         break;
  134.                     case 9:
  135.                         //led9.SetBlueIntensity((int)(clockDistance[increment02]));
  136.                         break;
  137.                     case 10:
  138.                         //led10.SetBlueIntensity((int)(clockDistance[increment02]));
  139.                         break;
  140.                     case 11:
  141.                         //led11.SetBlueIntensity((int)(clockDistance[increment02]));
  142.                         break;
  143.                     default:
  144.                         break;
  145.                 }
  146.             }
  147.              
  148.             // random target angle and distance?
  149.  
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment