Starter firmware project for the Imperial College EEE SWITCH lab
Dependencies: Adafruit_GFX mbed
main.cpp@2:a5b7373c4644, 2016-04-19 (annotated)
- Committer:
- estott
- Date:
- Tue Apr 19 15:38:38 2016 +0000
- Revision:
- 2:a5b7373c4644
- Parent:
- 1:5aebe57872ed
Rotated display; Defined switch and update variables as volatile
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
estott | 0:775d3ce39c34 | 1 | #include "mbed.h" |
estott | 0:775d3ce39c34 | 2 | #include "Adafruit_SSD1306.h" |
estott | 0:775d3ce39c34 | 3 | |
estott | 0:775d3ce39c34 | 4 | //Switch input definition |
estott | 0:775d3ce39c34 | 5 | #define SW_PIN p22 |
estott | 0:775d3ce39c34 | 6 | |
estott | 0:775d3ce39c34 | 7 | //Sampling period for the switch oscillator (us) |
estott | 0:775d3ce39c34 | 8 | #define SW_PERIOD 20000 |
estott | 0:775d3ce39c34 | 9 | |
estott | 0:775d3ce39c34 | 10 | //Display interface pin definitions |
estott | 0:775d3ce39c34 | 11 | #define D_MOSI_PIN p5 |
estott | 0:775d3ce39c34 | 12 | #define D_CLK_PIN p7 |
estott | 0:775d3ce39c34 | 13 | #define D_DC_PIN p8 |
estott | 0:775d3ce39c34 | 14 | #define D_RST_PIN p9 |
estott | 0:775d3ce39c34 | 15 | #define D_CS_PIN p10 |
estott | 0:775d3ce39c34 | 16 | |
estott | 0:775d3ce39c34 | 17 | //an SPI sub-class that sets up format and clock speed |
estott | 0:775d3ce39c34 | 18 | class SPIPreInit : public SPI |
estott | 0:775d3ce39c34 | 19 | { |
estott | 0:775d3ce39c34 | 20 | public: |
estott | 0:775d3ce39c34 | 21 | SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk) |
estott | 0:775d3ce39c34 | 22 | { |
estott | 0:775d3ce39c34 | 23 | format(8,3); |
estott | 0:775d3ce39c34 | 24 | frequency(2000000); |
estott | 0:775d3ce39c34 | 25 | }; |
estott | 0:775d3ce39c34 | 26 | }; |
estott | 0:775d3ce39c34 | 27 | |
estott | 0:775d3ce39c34 | 28 | //Interrupt Service Routine prototypes (functions defined below) |
estott | 0:775d3ce39c34 | 29 | void sedge(); |
estott | 0:775d3ce39c34 | 30 | void tout(); |
estott | 0:775d3ce39c34 | 31 | |
estott | 0:775d3ce39c34 | 32 | //Output for the alive LED |
estott | 0:775d3ce39c34 | 33 | DigitalOut alive(LED1); |
estott | 0:775d3ce39c34 | 34 | |
estott | 0:775d3ce39c34 | 35 | //External interrupt input from the switch oscillator |
estott | 0:775d3ce39c34 | 36 | InterruptIn swin(SW_PIN); |
estott | 0:775d3ce39c34 | 37 | |
estott | 0:775d3ce39c34 | 38 | //Switch sampling timer |
estott | 0:775d3ce39c34 | 39 | Ticker swtimer; |
estott | 0:775d3ce39c34 | 40 | |
estott | 2:a5b7373c4644 | 41 | //Registers for the switch counter, switch counter latch register and update flag |
estott | 2:a5b7373c4644 | 42 | volatile uint16_t scounter=0; |
estott | 2:a5b7373c4644 | 43 | volatile uint16_t scount=0; |
estott | 2:a5b7373c4644 | 44 | volatile uint16_t update=0; |
estott | 0:775d3ce39c34 | 45 | |
estott | 0:775d3ce39c34 | 46 | //Initialise SPI instance for communication with the display |
estott | 0:775d3ce39c34 | 47 | SPIPreInit gSpi(D_MOSI_PIN,NC,D_CLK_PIN); //MOSI,MISO,CLK |
estott | 0:775d3ce39c34 | 48 | |
estott | 0:775d3ce39c34 | 49 | //Initialise display driver instance |
estott | 0:775d3ce39c34 | 50 | Adafruit_SSD1306_Spi gOled1(gSpi,D_DC_PIN,D_RST_PIN,D_CS_PIN,64,128); //SPI,DC,RST,CS,Height,Width |
estott | 0:775d3ce39c34 | 51 | |
estott | 0:775d3ce39c34 | 52 | int main() { |
estott | 0:775d3ce39c34 | 53 | //Initialisation |
estott | 2:a5b7373c4644 | 54 | gOled1.setRotation(2); //Set display rotation |
estott | 0:775d3ce39c34 | 55 | |
estott | 0:775d3ce39c34 | 56 | //Attach switch oscillator counter ISR to the switch input instance for a rising edge |
estott | 0:775d3ce39c34 | 57 | swin.rise(&sedge); |
estott | 0:775d3ce39c34 | 58 | |
estott | 0:775d3ce39c34 | 59 | //Attach switch sampling timer ISR to the timer instance with the required period |
estott | 0:775d3ce39c34 | 60 | swtimer.attach_us(&tout, SW_PERIOD); |
estott | 0:775d3ce39c34 | 61 | |
estott | 0:775d3ce39c34 | 62 | //Write some sample text |
estott | 0:775d3ce39c34 | 63 | gOled1.printf("%ux%u OLED Display\r\n", gOled1.width(), gOled1.height()); |
estott | 0:775d3ce39c34 | 64 | |
estott | 0:775d3ce39c34 | 65 | //Main loop |
estott | 0:775d3ce39c34 | 66 | while(1) |
estott | 0:775d3ce39c34 | 67 | { |
estott | 0:775d3ce39c34 | 68 | //Has the update flag been set? |
estott | 0:775d3ce39c34 | 69 | if (update) { |
estott | 0:775d3ce39c34 | 70 | //Clear the update flag |
estott | 0:775d3ce39c34 | 71 | update = 0; |
estott | 0:775d3ce39c34 | 72 | |
estott | 0:775d3ce39c34 | 73 | //Set text cursor |
estott | 0:775d3ce39c34 | 74 | gOled1.setTextCursor(0,0); |
estott | 0:775d3ce39c34 | 75 | |
estott | 0:775d3ce39c34 | 76 | //Write the latest switch osciallor count |
estott | 0:775d3ce39c34 | 77 | gOled1.printf("\n%05u ",scount); |
estott | 0:775d3ce39c34 | 78 | |
estott | 0:775d3ce39c34 | 79 | //Copy the display buffer to the display |
estott | 0:775d3ce39c34 | 80 | gOled1.display(); |
estott | 0:775d3ce39c34 | 81 | |
estott | 0:775d3ce39c34 | 82 | //Toggle the alive LED |
estott | 0:775d3ce39c34 | 83 | alive = !alive; |
estott | 0:775d3ce39c34 | 84 | } |
estott | 0:775d3ce39c34 | 85 | |
estott | 0:775d3ce39c34 | 86 | |
estott | 0:775d3ce39c34 | 87 | } |
estott | 0:775d3ce39c34 | 88 | } |
estott | 0:775d3ce39c34 | 89 | |
estott | 0:775d3ce39c34 | 90 | |
estott | 1:5aebe57872ed | 91 | //Interrupt Service Routine for rising edge on the switch oscillator input |
estott | 0:775d3ce39c34 | 92 | void sedge() { |
estott | 0:775d3ce39c34 | 93 | //Increment the edge counter |
estott | 0:775d3ce39c34 | 94 | scounter++; |
estott | 0:775d3ce39c34 | 95 | } |
estott | 0:775d3ce39c34 | 96 | |
estott | 1:5aebe57872ed | 97 | //Interrupt Service Routine for the switch sampling timer |
estott | 0:775d3ce39c34 | 98 | void tout() { |
estott | 0:775d3ce39c34 | 99 | //Read the edge counter into the output register |
estott | 0:775d3ce39c34 | 100 | scount = scounter; |
estott | 0:775d3ce39c34 | 101 | //Reset the edge counter |
estott | 0:775d3ce39c34 | 102 | scounter = 0; |
estott | 0:775d3ce39c34 | 103 | //Trigger a display update in the main loop |
estott | 0:775d3ce39c34 | 104 | update = 1; |
estott | 0:775d3ce39c34 | 105 | } |
estott | 0:775d3ce39c34 | 106 |