Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
MainWithFreq.cpp
- Committer:
- abuitrag
- Date:
- 2014-06-23
- Revision:
- 1:c3ba9212f103
- Parent:
- 0:b738b4b05ed8
- Child:
- 2:7900c18cebd5
File content as of revision 1:c3ba9212f103:
#include "mbed.h"
SPI spiR(p11, p12, p13); // mosi, miso, sclk
DigitalOut lpR(p10);
DigitalOut bpR(p14);
SPI spiL(p5, p6, p7);
DigitalOut lpL(p8);
DigitalOut bpL(p9);
void latch()
{
lpR = 1;
lpL = 1;
lpR = 1;
lpL = 1;
lpR = 1;
lpL = 1;
lpR = 0;
lpL = 0;
}
//FREQ CONTROL CONFIG
Ticker tick;
InterruptIn in(p22);
Timer t1;
Timer t2;
//INTERRUPT FEEDBACK LED
DigitalOut led(LED1);
//RUNTIME VARS
int t_period = 0; // This is the period between interrupts in microseconds
float t_freq = 0;
int last = -1;
int curt = 0;
//DUMMY FUNCTION AS THERE IS NO HEADER FILE
void flip();
void flip()
{
t_period = t1.read_us(); // Get time since last interrupt
t_freq = (1/(float)t_period)*1000000; // Convert period (in us) to frequency (Hz)
t1.reset(); // Reset timer and wait for next interrupt
t2.reset();
led = !led;
}
short outp[48][48]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xFFF},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xFFF,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xFFF,0,0},
{0,0,0,0,0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FF,0xFFF,0xFFF};
short radaroff[24]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
short radaron[24]={0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,
0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF,0xFFF};
short radardot[24]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xFFF,0,0,0,0,0,0,0,0};
int lookupport(int j)
{
int led, p;
if(j%2 == 1) //Alle ungeradezahligen Ports
//werden vom rechten Controller angesteuert
//entsprechend wird "R" als "Side" zugewiesen
{
p = j/2; //Durch Division /2 bekommen wir eine "normierte Reihe" also (0,1 ,2 ... 12)
if (p%2 == 0) //nochmal %2 da Ports sich abwechseln (0, 23, 1, 22... siehe Tabelle)
led = 23 - (p/2); //also von "p" ausgegangen, beginnen gerade Zahlen von hinten an (Ports 23, 22, 21... 12)
else
led = (p/2); // ungerade Zahlen beginnen von vorne an (Port 0, 1, 2... 11)
}
else
{
p = (j-1)/2;
if (p%2 == 0)
led = 23 - (p/2);
else
led = (p/2);
}
return led;
}
//returns 1 if right Port, else 0
int lookupLR(int j){
return j % 2;
}
//Hier kommt die Funktion
int main()
{
lpR = 0;
lpL = 0;
spiR.format(12, 0);
spiR.frequency(30 * 1000 * 1000);
spiL.format(12, 0);
spiL.frequency(30 * 1000 * 1000);
bpR=0;
bpL = 0;
for(x=0;x<2;x++) {
for(int i=47;i>=0;i--){
bpR=1;
bpL = 1;
for(int j = 0;j<sizeof(outp[0])/sizeof(outp[0][0]);j++){
if (lookupLR(j)){
actualrowR[lookupport(j)] = outp[i][j];
}else{
actualrowL[lookupport(j)] = outp[i][j];
}
}
for (int k =0; k<sizeof(actualrowR)/sizeof(actualrowR[0]);++k){
spiR.write(actualrowR[k]);
}
for (int k =0; k<sizeof(actualrowL)/sizeof(actualrowL[0]);++k){
spiL.write(actualrowL[k]);
}
latch();
bpR=0;
bpL = 0;
wait(0.08);
}
for(int i=0;i<sizeof(outp)/sizeof(outp[0]);i++){
bpR=1;
bpL = 1;
for(int j = 0;j<sizeof(outp[0])/sizeof(outp[0][0]);j++){
if (lookupLR(j)){
actualrowR[lookupport(j)] = outp[i][j];
}else{
actualrowL[lookupport(j)] = outp[i][j];
}
}
for (int k =0; k<sizeof(actualrowR)/sizeof(actualrowR[0]);++k){
spiR.write(actualrowR[k]);
}
for (int k =0; k<sizeof(actualrowL)/sizeof(actualrowL[0]);++k){
spiL.write(actualrowL[k]);
}
latch();
bpR=0;
bpL = 0;
wait(0.08);
}
}
//****************************//
//CAKE FUNCTION - YUM YUM
//****************************//
{
in.mode(PullDown); // Set the pin to Pull Down mode.
in.rise(&flip); // Set up the interrupt for rising edge
t1.start(); // start the timer
t2.start();
int a = 0;
while(a<40) { //as a placeholder until we find a way to navigate between programmes
a++;
if(t_period == 0) {
continue;
}
curt = t2.read_us();
if (curt < t_period/8
|| (t_period/4 < curt && curt < (t_period/8)*3 )
|| (t_period/2 < curt && curt < (t_period/8)*5 )
|| ((t_period/4)*3 < curt && curt < (t_period/8)*7 )
) {
} else {
if (last != 0) {
//cake empty
bpL=1;
bpR=1;
for (int j = 0; j<48; j++){
spiR.write(0x000);
spiL.write(0x000);
}
//for(int j=0; j<sizeof(cakeempty)/sizeof(cakeempty[0]); j++) {
// spiR.write(cakeempty[j]);
//spiL.write(cakeempty[j]);
//}
latch();
bpL=0;
bpR=0;
last = 0;
}
if (last != 1) {
//cake full
bpL=1;
bpR=1;
for (int j = 0; j<48; j++){
spiR.write(0x00F);
spiL.write(0x00F);
}
//for(int j=0; j<sizeof(cakefull)/sizeof(cakefull[0]); j++) {
// spiR.write(cakefull[j]);
//spiL.write(cakefull[j]);
//}
latch();
bpL=0;
bpR=0;
last = 1;
}
}
bpL=1;
bpR=1;
for(j=0;j<sizeof(radaron)/sizeof(radaron[0]);j++){
spiR.write(radaron[j]);
}
latch();
bpL=0;
bpR=0;
wait(0.05); //tentative wait value
//right side off
bpL=1;
bpR=1;
for(j=0;j<sizeof(radaroff)/sizeof(radaroff[0]);j++){
spiR.write(radaroff[j]);
}
bpL=0;
bpR=0;
wait(t_period+0.05); //tentative additional wait value
}
}
/*********************************/
// RADAR FUNCTION FUCK YEAH //
/**********************************/
{
in.mode(PullDown); // Set the pin to Pull Down mode.
in.rise(&flip); // Set up the interrupt for rising edge
t1.start(); // start the timer
t2.start();
int a = 0;
while(a<40) { //as a placeholder until we find a way to navigate between programmes
a++;
if(t_period == 0) {
continue;
}
curt = t2.read_us();
if (curt !=t_period)
{
} else {
if (last != 0) {
//cake empty
//HERE COMES THE DOT, YO
bpL=1;
for(j=0;j<sizeof(radardot)/sizeof(radardot[0]);j++){
spiL.write(radardot[j]);
}
latch();
bpL=0;
bpR=0;
last = 0;
}
if (last != 1) {
bpL=1;
for(j=0;j<sizeof(radardot)/sizeof(radardot[0]);j++){
spiL.write(radaroff[j]);
}
latch();
bpL=0;
bpR=0;
last = 1;
}
}
}
}
//Please notice that the wait times for the needle and the dot are different - THIS IS INTENDED
//radaron
/*for(x=0;x<20;x++){
bpL=1;
bpR=1;
for(j=0;j<sizeof(radaron)/sizeof(radaron[0]);j++){
spiR.write(radaron[j]);
}
latch();
bpL=0;
bpR=0;
wait(0.15);
//radaroff
bpL=1;
bpR=1;
for(j=0;j<sizeof(radaroff)/sizeof(radaroff[0]);j++){
spiR.write(radaroff[j]);
}
bpL=0;
bpR=0;
wait(0.15);
//HERE COMES THE DOT, YO
bpL=1;
for(j=0;j<sizeof(radardot)/sizeof(radardot[0]);j++){
spiL.write(radardot[j]);
}
latch();
bpL=0;
bpR=0;
wait(0.1);
bpL=1;
for(j=0;j<sizeof(radardot)/sizeof(radardot[0]);j++){
spiL.write(radaroff[j]);
}
latch();
bpL=0;
bpR=0;
wait(0.1);
}*/
}