Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. * @file main.cpp (180.ARM_Peripherals/Sources/main.cpp)
  4. * @brief Basic C++ demo using GPIO class
  5. *
  6. * Created on: 10/1/2016
  7. * Author: podonoghue
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "system.h"
  13. #include "derivative.h"
  14. #include "hardware.h"
  15. #include "lcd.h"
  16. #include "spi.h"
  17. #include "delay.h"
  18. #include <math.h>
  19. #include "i2c.h"
  20. #include "mma845x.h"
  21. #include <pit.h>
  22.  
  23. using namespace USBDM;
  24.  
  25. // I2C interface
  26. I2c0 i2c0;
  27.  
  28. // Accelerometer via I2C
  29. MMA845x accelerometer(i2c0, MMA845x::ACCEL_2Gmode);
  30.  
  31. // SPI interface
  32. Spi0 spi;
  33.  
  34. // LCD interface using SPI
  35. Lcd lcd(spi);
  36.  
  37. using Ground = GpioC<10>;
  38. using fPin = GpioA<13>;
  39.  
  40. /* ************************************************** */
  41.  
  42. /// LCD derived dimensions
  43. static constexpr int LCD_WIDTH = (LCD_X_MAX-LCD_X_MIN);
  44. static constexpr int LCD_HEIGHT = (LCD_Y_MAX-LCD_Y_MIN);
  45. static constexpr int CENTRE_X = ((LCD_X_MAX-LCD_X_MIN)/2);
  46. static constexpr int CENTRE_Y = ((LCD_Y_MAX-LCD_Y_MIN)/2);
  47.  
  48. // Colour for LCD background
  49. static constexpr int BACKGROUND_COLOUR = (PURPLE);
  50.  
  51. // Colour for LCD foreground
  52. static constexpr int FOREGROUND_COLOUR = (GREEN);
  53.  
  54. // Radius used for the moving circle
  55. static constexpr int CIRCLE_RADIUS = (15);
  56.  
  57. /*
  58. * Draws a cursor on the lcd screen
  59. *
  60. * @param x x position
  61. * @param y y position
  62. * @param colour Colour of cursor
  63. *
  64. * @note Done this way so a more sophisticated cursor can be added
  65. */
  66.  
  67. void drawCursor(int x, int y, int colour) {
  68. lcd.drawCircle(x, y, CIRCLE_RADIUS, colour);
  69.  
  70. lcd.drawLine((x - CIRCLE_RADIUS), y, (x + CIRCLE_RADIUS), y, colour);
  71. lcd.drawLine(x, (y - CIRCLE_RADIUS), x, (y + CIRCLE_RADIUS), colour);
  72. }
  73.  
  74. // Timer LED connection - change as required
  75. /**
  76. * This example uses FTM interrupts.
  77. *
  78. * It is necessary enable these in Configure.usbdmProject under the "Peripheral Parameters"->FTM tab
  79. * Select irqHandlerInstalled option.
  80. */
  81.  
  82. // Timer channel being used - change as required
  83. using Note = Ftm1;
  84.  
  85. using NoteChannel = Ftm1Channel<1>;
  86.  
  87. // Half-period for timer
  88. static volatile uint16_t timerHalfPeriod;
  89.  
  90. static const float WAVEFORM_PERIOD = 100*ms;
  91.  
  92. /**
  93. * Interrupt handler for Timer interrupts
  94. * This sets the next interrupt/pin toggle for a half-period from the last event
  95. *
  96. * @param[in] status Flags indicating interrupt source channel(s)
  97. */
  98. static void ftmCallback(uint8_t status) {
  99.  
  100. // Check channel
  101. if (status & NoteChannel::CHANNEL_MASK) {
  102.  
  103. // Note: The pin is toggled directly by hardware
  104. // Re-trigger at last interrupt time + timerHalfPeriod
  105. NoteChannel::setDeltaEventTime(timerHalfPeriod);
  106. //Note::setTickFrequency(timerHalfPeriod, 10);
  107. //printf("calll \n");
  108. //printf("freq = %i\n", Note::getTickFrequency());
  109. }
  110. }
  111.  
  112. void playNote(int distanceToCenter, double note_scale_factor) {
  113. timerHalfPeriod = 8000 - (100 + (distanceToCenter*note_scale_factor));
  114.  
  115. printf("Half Period = %i \n", timerHalfPeriod);
  116. }
  117.  
  118. int main() {
  119.  
  120. Ground::setOutput();
  121.  
  122. Ground::off();
  123.  
  124. printf("Starting\n");
  125.  
  126. uint8_t id = accelerometer.readID();
  127. printf("Device ID = 0x%02X (should be 0x1A)\n", id);
  128.  
  129. printf("Doing simple calibration\n"
  130. "Make sure the device is level!\n");
  131. waitMS(2000);
  132.  
  133. if (!accelerometer.calibrateAccelerometer()) {
  134. printf("Calibration failed!\n");
  135. __asm__("bkpt");
  136. }
  137.  
  138. // Make sure we have new values
  139. waitMS(100);
  140.  
  141. printf("After calibration\n");
  142.  
  143. // Draw pretty pattern
  144. lcd.clear(BACKGROUND_COLOUR);
  145.  
  146. // Cursor position on screen
  147. double note_scale_factor = 107.608695652;
  148. double LCD_scale_factor = 0.0113636364; //(LCD_WIDTH-2*CIRCLE_RADIUS)/ACCELEROMETER_RANGE //// (130-2*15)/8800
  149. int x = 65, y = 65;
  150. int xOld, yOld;
  151. int distanceToCenter = 0;
  152.  
  153. int accelStatus;
  154. int16_t accelX,accelY,accelZ;
  155.  
  156. // Do default configure
  157. Note::configure(FtmMode_LeftAlign, FtmClockSource_System, FtmPrescale_1);
  158.  
  159. Note::setMeasurementPeriod(1.1*WAVEFORM_PERIOD/2.0);
  160.  
  161. // Set handler for channel 1 programmatically
  162. Note::setChannelCallback(ftmCallback);
  163.  
  164. timerHalfPeriod = Note::convertSecondsToTicks(WAVEFORM_PERIOD/2.0);
  165. //printf("Half Period = %i \n", timerHalfPeriod);
  166.  
  167. Note::enableNvicInterrupts();
  168.  
  169. // Setup debouncer to execute @PIT_FREQUENCY
  170. //Note::configureChannelInTicks(1, ::SystemBusClock/timerHalfPeriod);
  171.  
  172.  
  173.  
  174. // Enable interrupts on the channel
  175. //Note::enableInterrupts(1);
  176.  
  177. NoteChannel::setDriveStrength(PinDriveStrength_High);
  178.  
  179. NoteChannel::setRelativeEventTime(100);
  180.  
  181. NoteChannel::configure(FtmChMode_OutputCompareToggle, FtmChannelIrq_Enable);
  182.  
  183. // Check for errors so far
  184. checkError();
  185.  
  186. for(;;) {
  187.  
  188. lcd.drawLine(LCD_X_MIN, CENTRE_Y, LCD_X_MAX, CENTRE_Y, FOREGROUND_COLOUR);
  189. lcd.drawLine(CENTRE_X, LCD_Y_MIN, CENTRE_X, LCD_Y_MAX, FOREGROUND_COLOUR);
  190.  
  191. accelerometer.readAccelerometerXYZ(accelStatus, accelX, accelY, accelZ);
  192.  
  193. x = 15 + (accelX+4400)*LCD_scale_factor;
  194. y = 130 - (15 + (accelY+4400)*LCD_scale_factor);
  195.  
  196. //printf("accelX = %i accelY = %i\n", accelX, accelY);
  197. //printf(" x = %i y = %i\n", x, y);
  198. //printf("distance = %i \n", distanceToCenter);
  199. //printf("fPin value = %i \n", fPin::read());
  200.  
  201. drawCursor(yOld, xOld, BACKGROUND_COLOUR);
  202.  
  203. drawCursor(y, x, FOREGROUND_COLOUR);
  204.  
  205. xOld = x;
  206. yOld = y;
  207.  
  208. distanceToCenter = sqrt(pow((x-65),2) + pow((y-65),2)); // range = [0, 92] **integers**
  209.  
  210. playNote(distanceToCenter, note_scale_factor);
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement