2012-06-17 04:16:37 +03:00
|
|
|
/*
|
2012-06-19 00:22:56 +03:00
|
|
|
* fw/antorcha.c - Initialization of Antorcha application firmware
|
2012-06-17 04:16:37 +03:00
|
|
|
*
|
|
|
|
* Written 2012 by Werner Almesberger
|
|
|
|
* Copyright 2012 Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-06-17 04:16:37 +03:00
|
|
|
#include "rf.h"
|
2012-06-19 00:22:56 +03:00
|
|
|
#include "dispatch.h"
|
2012-06-20 13:26:52 +03:00
|
|
|
#include "sweep.h"
|
2012-06-21 14:56:56 +03:00
|
|
|
#include "accel.h"
|
2012-06-20 16:41:27 +03:00
|
|
|
#include "image.h"
|
|
|
|
|
2012-06-24 03:53:26 +03:00
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include "io.h"
|
|
|
|
|
2012-06-20 16:41:27 +03:00
|
|
|
|
|
|
|
static struct sweep sweep = {
|
2012-06-24 03:53:26 +03:00
|
|
|
.wait_ticks = 60000, /* 60 ms */
|
|
|
|
.pixel_ticks = 1000, /* 1 ms */
|
2012-06-20 16:41:27 +03:00
|
|
|
.left = 0,
|
2012-06-24 03:57:18 +03:00
|
|
|
.right = MAX_LINES-1,
|
2012-06-20 16:41:27 +03:00
|
|
|
.forward = 1,
|
|
|
|
};
|
2012-06-17 04:16:37 +03:00
|
|
|
|
|
|
|
|
2012-06-24 03:53:26 +03:00
|
|
|
static volatile enum sync_state {
|
|
|
|
IDLE,
|
|
|
|
LEFT_DECEL,
|
|
|
|
LEFT_ACCEL,
|
|
|
|
FWD_START_SWEEP,
|
|
|
|
FWD_SWEEP,
|
|
|
|
} state = IDLE;
|
|
|
|
|
|
|
|
|
|
|
|
#define LEFT_DECEL_THRESH 850
|
|
|
|
#define LEFT_REVERSE_THRESH 900
|
|
|
|
#define LEFT_ACCEL_THRESH 850
|
|
|
|
#define RIGHT_DECEL_THRESH 200
|
|
|
|
|
|
|
|
|
|
|
|
static void sync_sweep(bool x, uint16_t v)
|
|
|
|
{
|
|
|
|
static uint32_t t;
|
|
|
|
|
|
|
|
if (!x)
|
|
|
|
return;
|
|
|
|
switch (state) {
|
|
|
|
default:
|
|
|
|
case IDLE:
|
|
|
|
if (v < LEFT_DECEL_THRESH)
|
|
|
|
break;
|
|
|
|
state = LEFT_DECEL;
|
|
|
|
t = uptime_irq();
|
|
|
|
break;
|
|
|
|
case LEFT_DECEL:
|
|
|
|
if (v < LEFT_DECEL_THRESH) {
|
|
|
|
t = uptime_irq();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
if (v > LEFT_REVERSE_THRESH)
|
|
|
|
state = LEFT_ACCEL;
|
|
|
|
break;
|
|
|
|
case LEFT_ACCEL:
|
|
|
|
if (v > LEFT_ACCEL_THRESH)
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
if (v < LEFT_REVERSE_THRESH)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
if (sweeping) { /* we're confused */
|
|
|
|
state = IDLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state = FWD_START_SWEEP;
|
|
|
|
/* t = t1-(t1+t0)/2 = (t1-t0)/2 */
|
|
|
|
t = (uptime_irq()-t) >> 1;
|
|
|
|
// sweep.wait_ticks = kkkkkk
|
|
|
|
break;
|
|
|
|
case FWD_START_SWEEP:
|
|
|
|
break;
|
|
|
|
case FWD_SWEEP:
|
|
|
|
if (v > RIGHT_DECEL_THRESH)
|
|
|
|
break;
|
|
|
|
state = IDLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
static const struct handler *protos[] = {
|
2012-06-20 16:30:40 +03:00
|
|
|
&image_handler,
|
2012-06-19 02:31:20 +03:00
|
|
|
&reset_handler,
|
2012-06-21 18:12:10 +03:00
|
|
|
&sample_handler,
|
2012-06-19 00:22:56 +03:00
|
|
|
NULL
|
|
|
|
};
|
2012-06-17 04:16:37 +03:00
|
|
|
|
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[PAYLOAD+5]; /* 3 bytes header, 2 bytes CRC */
|
|
|
|
uint8_t got;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The boot loader has already initialized PORTx, DDRx, and MCUCR.PUD.
|
|
|
|
* It has also brought up RF and the underlying SPI.
|
|
|
|
*/
|
|
|
|
|
2012-06-20 13:26:52 +03:00
|
|
|
sweep_init();
|
2012-06-24 03:53:26 +03:00
|
|
|
sample = sync_sweep;
|
2012-06-21 14:56:56 +03:00
|
|
|
accel_start();
|
2012-06-24 03:53:26 +03:00
|
|
|
sei();
|
2012-06-20 13:26:52 +03:00
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
while (1) {
|
|
|
|
got = rf_recv(buf, sizeof(buf));
|
|
|
|
if (got > 2)
|
|
|
|
dispatch(buf, got-2, protos);
|
2012-06-24 03:53:26 +03:00
|
|
|
#if 1
|
|
|
|
if (state == FWD_START_SWEEP && !sweeping) {
|
|
|
|
state = FWD_SWEEP;
|
2012-06-20 16:41:27 +03:00
|
|
|
sweep_image(&sweep);
|
2012-06-24 03:53:26 +03:00
|
|
|
}
|
|
|
|
#endif
|
2012-06-17 04:16:37 +03:00
|
|
|
}
|
|
|
|
}
|