2532

Dependencies:   QEI WS2812 PixelArray DFPlayerMini MODSERIAL PCA9685_ pca9685

main.cpp

Committer:
dimavb
Date:
2019-06-02
Revision:
0:474fb16588bf
Child:
2:ed1f45f9b06a

File content as of revision 0:474fb16588bf:

/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "stats_report.h"
#include "PCA9685.h"
#include "QEI.h"
#include "WS2812.h"
#include "PixelArray.h"
#include "DebounceIn.h"


//InterruptIn int0 (PB_0);
//enc pb2 pb4
//zero pb5
DebounceIn zerosens(PB_6);
QEI enc(PB_2,PB_4,NC,1);
I2C i2c (PB_9,PB_8);
PCA9685 pwmouts(0x40<<1,i2c,500);
WS2812 ws(PB_15,500, 0, 18, 10, 15);
PixelArray px(500);
DigitalOut led1(LED1);

#define SLEEP_TIME                  200 // (msec)
#define PRINT_AFTER_N_LOOPS         20
#define REVCNT 500

void int0f (void)
{
    led1=!led1;
}
void encworker()
{
    printf("hello encoder\n");
    int pos=0;
    int pos_=0;
    int npos=0;
    int lastdirection=0;
    while(1)
    {
        if (zerosens.ntrig()) 
        {
            printf("zero\n");
            npos=enc.getPulses();
            enc.reset();
        }
        pos=enc.getPulses();
        
        printf("pos: %d \t%d \t%d\n",pos,npos,zerosens.read());
        wait(0.1);    
    }
}
void ledworker()
{
    printf("hello leds\n");
    
    ws.useII(WS2812::OFF); // use no intensity scaling
    Timer t;
    t.start();
    while(1)
    {
        int p=(enc.getPulses()%160)/10;
        for (uint8_t i=0; i<15; i++)
        {
            if (i==p) pwmouts.set_pwm_duty(i,1); else pwmouts.set_pwm_duty(i,0);
        }
        px.SetAll(3000000+t.read_ms());
        
        //ws.write(px.getBuf());
        wait(0.02);
    }
}
int main()
{
    Thread ledsthread;
    Thread encthread;
    
    ledsthread.start(ledworker);
    encthread.start(encworker);
    printf("hello\n");
    pwmouts.init();
    float v=0;
    Timer t;
    t.start();
    while (true) 
    {
        wait(10);
        printf("TITS!\n (.)(.)\n");
    }
}