Skip to content

Commit

Permalink
Merge branch 'master' of github.com:alon/chibi-juggling
Browse files Browse the repository at this point in the history
  • Loading branch information
alon.levy committed Feb 25, 2010
2 parents 5ec2092 + 58f3f14 commit 4c147d2
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 168 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Chibi Juggling, for lack of a better name, is meant to create juggling balls around the chibi boards, to teach juggling. The chibi boards have an 8-bit atmel mcu with zigbee rf, and an adxl addon. Together, and adding some unspecified output device (lights / sounds / vibration) they can give cues to would be jugglers that the throw is not timed correctly, or not strong enough or too strong etc.
7 changes: 6 additions & 1 deletion include/juggling.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

#define STATIONARY_SHORT_ADDRESS 5
#define JUGGLED_SHORT_ADDRESS 7

// 6 bytes for accel{x,y,z} and magic is 2 bytes
#define ADXL_PACKET_LENGTH (6 + ADXL_MAGIC_LENGTH)
#define ADXL_MAGIC_LENGTH 2
#define ADXL_MAGIC "JG"
#define ADXL_MAGIC_BYTE_1 'J'
#define ADXL_MAGIC_BYTE_2 'G'

#endif //__JUGGLING_H__

9 changes: 6 additions & 3 deletions juggled/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ OBJDIR = .

# List C source files here. (C dependencies are automatically generated.)
FREAKUSB=../../root/freakusb
CHIBI=../../root/Chibi
CHIBI=../../root
SRC = $(TARGET).c \
adxl.c \
spi.c \
$(FREAKUSB)/usb/usb.c \
$(FREAKUSB)/usb/ctrl.c \
$(FREAKUSB)/usb/usb_buf.c \
Expand All @@ -109,7 +111,7 @@ else ifeq ($(MCU), atmega32u4)
SRC += $(FREAKUSB)/hw/atmega32u4/ep.c \
$(FREAKUSB)/hw/atmega32u4/hw.c \
$(FREAKUSB)/hw/atmega32u4/isr.c \
$(CHIBI)/chibi/hw/avrusb32/chb_drvr.c
$(CHIBI)/chibi/hw/avrusb32/at86rf230/chb_drvr.c
endif


Expand Down Expand Up @@ -469,7 +471,8 @@ end:

# Display size of file.
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
#ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
ELFSIZE = $(SIZE) --target=elf32-avr $(TARGET).elf

sizebefore:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
Expand Down
12 changes: 8 additions & 4 deletions juggled/cmd_tbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

static cmd_t cmd_tbl[] =
{
{"enb", cmd_enb_print},
{"adxl", cmd_enb_adxl},
{"tran", cmd_enb_transmit},
{"adxl", cmd_enb_adxl},
{"read", cmd_reg_read},
{"write", cmd_reg_write},
{"ssa", cmd_set_short_addr},
{"gsa", cmd_get_short_addr},
{"who", cmd_who},
{"csent", cmd_print_adxl_transmit_count},
{"cread", cmd_print_adxl_read_count},
{"send", cmd_send_test_message},
READ_WRITE_FLAG__CMD_TBL(read_adxl_flag),
READ_WRITE_FLAG__CMD_TBL(adxl_flag),
READ_WRITE_FLAG__CMD_TBL(print_flag),
READ_WRITE_FLAG__CMD_TBL(transmit_flag),
{NULL, NULL}
};

Expand Down
192 changes: 90 additions & 102 deletions juggled/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,90 +36,63 @@
\ingroup cdc_demo
*/
/*******************************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <util/delay.h>

#include "main.h"
#include "cmd.h"
#include "chb.h"
#include "chb_drvr.h" // for short address
#include "at86rf230/chb_drvr.h" // for short address
#include "juggling.h"

#define SPI_ENB() do {PORTC &= ~(_BV(PORTC6));} while(0)
#define SPI_DIS() do {PORTC |= _BV(PORTC6);} while(0)
#include "adxl.h"
#include "spi.h"

static bool rd_adxl = true; // default to on. But allow usb cmd to change it.
static bool print_adxl = false;
static bool transmit_adxl = true;

U16 acc_X = 0, acc_Y = 0, acc_Z = 0;
U32 adxl_read_count = 0;
U32 adxl_transmit_count = 0;

/**************************************************************************/
/*!
#define CYCLES_PER_TRANSMIT 200

READ_WRITE_FLAG__FLAG_IMP(read_adxl_flag);
READ_WRITE_FLAG__FLAG_IMP_DEFAULT(adxl_flag, false);
READ_WRITE_FLAG__FLAG_IMP_DEFAULT(print_flag, false);
READ_WRITE_FLAG__FLAG_IMP(transmit_flag);

static bool transmit_adxl = true;
static U16 time_to_transmit = false;

#define CYCLES_INIT_DONE 10000

*/
/**************************************************************************/
void spi_init()
{
// configure the SPI slave_select, spi clk, and mosi pins as output. the miso pin
// is cleared since its an input.
DDRC |= _BV(PORTC6);
PORTC |= _BV(PORTC6);

DDRB |= _BV(PORTB1) | _BV(PORTB2);
PORTB |= _BV(PORTB1);

// set to master mode
// set clock polarity to be high idle
// set clock phase to sample on trailing edge of sclk
// set the clock freq to fck/128.
SPCR = _BV(MSTR) | _BV(CPOL) | _BV(CPHA);
SPSR = _BV(SPI2X);

// enable the SPI master
SPCR |= (1 << SPE);
}

/**************************************************************************/
/*!
This function both reads and writes data. For write operations, include data
to be written as argument. For read ops, use dummy data as arg. Returned
data is read byte val.
*/
/**************************************************************************/
U8 spi_xfer_byte(U8 data)
{
SPDR = data;
while (!(SPSR & (1<<SPIF)));
return SPDR;
}

/**************************************************************************/
/*!
Globals and small helpers for main
*/
/**************************************************************************/
void adxl_init()
{
// set power bit
SPI_ENB();
spi_xfer_byte(0x2d);
spi_xfer_byte(0x28);
SPI_DIS();

// set full resolution
SPI_ENB();
spi_xfer_byte(0x31);
spi_xfer_byte(0x08);
SPI_DIS();
U8 dat[ADXL_PACKET_LENGTH]; // data of packet to be sent
U8* read_start = dat + ADXL_MAGIC_LENGTH;

// clear fifo
SPI_ENB();
spi_xfer_byte(0x38);
spi_xfer_byte(0x00);
SPI_DIS();
void main_read_adxl()
{
// reinit adxl
spi_init();
adxl_init();
// read adxl
adxl_read_count++;
read_adxl(read_start);
acc_X = (read_start[1] << 8)
+ read_start[0];
acc_Y = (read_start[3] << 8)
+ read_start[2];
acc_Z = (read_start[5] << 8)
+ read_start[4];
}

/**************************************************************************/
Expand All @@ -129,59 +102,64 @@ void adxl_init()
/**************************************************************************/
int main()
{
// lame time counter, should be using timer interrupt
unsigned int t = 0;
// turn on the led
DDRC |= 1<<PORTC7;
PORTC |= 1<<PORTC7;

spi_init();
adxl_init();

cmd_init();

// turn off the led
PORTC &= ~(1<<PORTC7);

chb_init();

// init the spi
spi_init();
adxl_init();

// set our short address - I think its eeprom, so only set it if it
// isn't that already
if (chb_get_short_addr() != JUGGLED_SHORT_ADDRESS)
chb_set_short_addr(JUGGLED_SHORT_ADDRESS);

spi_init(); // don't ask me why this is done twice. It just is. I WILL INVESTIGATE!

// turn on the led
PORTC |= 1<<PORTC7;

// and off we go...
dat[0] = ADXL_MAGIC_BYTE_1;
dat[1] = ADXL_MAGIC_BYTE_2;

bool initing = true;

while (1)
{
++t;
if (initing && t == CYCLES_INIT_DONE) {
initing = false;
PORTC &= ~(1<<PORTC7);
time_to_transmit = CYCLES_PER_TRANSMIT;
}
cmd_poll();

if (rd_adxl)
{
U8 i, dat[6];
main_read_adxl();

// read adxl
SPI_ENB();
spi_xfer_byte(0xf2);

for (i=0; i<6; i++)
{
dat[i] = spi_xfer_byte(0);
}
SPI_DIS();
// write packet - just the raw bytes from SPI will do.
if (transmit_adxl)
chb_write(STATIONARY_SHORT_ADDRESS, dat, 6);

acc_X = (dat[1] << 8) + dat[0]; // TODO - or reverse?
acc_Y = (dat[3] << 8) + dat[2]; // TODO - or reverse?
acc_Z = (dat[5] << 8) + dat[4]; // TODO - or reverse?

if (print_adxl)
printf_P(PSTR("%d\t%d\t%d\n"), acc_X, acc_Y, acc_Z);
if (transmit_flag && time_to_transmit <= 0) {
adxl_transmit_count++;
time_to_transmit = CYCLES_PER_TRANSMIT;
// reinit chibi stack (no need to reset address)
chb_init();
chb_write(STATIONARY_SHORT_ADDRESS, dat, ADXL_PACKET_LENGTH);
}
if (time_to_transmit > 0) --time_to_transmit;
if (print_flag)
printf_P(PSTR("%d %d\t%d\t%d\n"), adxl_read_count, acc_X, acc_Y, acc_Z);

_delay_ms(100);
//_delay_ms(100);
}
}
}
Expand Down Expand Up @@ -234,54 +212,64 @@ void cmd_reg_write(U8 argc, char **argv)
*/
/**************************************************************************/
void cmd_enb_adxl(U8 argc, char **argv)
void cmd_set_short_addr(U8 argc, char **argv)
{
U8 val = strtol(argv[1], NULL, 10);
rd_adxl = val;
U16 addr = strtol(argv[1], NULL, 16);
chb_set_short_addr(addr);
}

/**************************************************************************/
/*!
*/
/**************************************************************************/
void cmd_enb_print(U8 argc, char **argv)
void cmd_get_short_addr(U8 argc, char **argv)
{
U8 val = strtol(argv[1], NULL, 10);
print_adxl = val;
U16 addr = chb_get_short_addr();
printf_P(PSTR("Short Addr = %04X.\n"), addr);
}

/**************************************************************************/
/*!
*/
/**************************************************************************/
void cmd_enb_transmit(U8 argc, char **argv)
void cmd_who(U8 argc, char **argv)
{
U8 val = strtol(argv[1], NULL, 10);
transmit_adxl = val;
printf_P(PSTR("Juggled"));
}


/**************************************************************************/
/*!
*/
/**************************************************************************/
void cmd_set_short_addr(U8 argc, char **argv)
void cmd_print_adxl_transmit_count(U8 argc, char **argv)
{
U16 addr = strtol(argv[1], NULL, 16);
chb_set_short_addr(addr);
printf_P(PSTR("adxl transmitted %d\n"), adxl_transmit_count);
}


/**************************************************************************/
/*!
*/
/**************************************************************************/
void cmd_get_short_addr(U8 argc, char **argv)
void cmd_print_adxl_read_count(U8 argc, char **argv)
{
U16 addr = chb_get_short_addr();
printf_P(PSTR("Short Addr = %04X.\n"), addr);
printf_P(PSTR("adxl read %d\n"), adxl_read_count);
}


void cmd_send_test_message(U8 argc, char **argv)
{
static U8 test_count = 0;
U8 str[10];
sprintf(str, "%d", test_count++);
chb_init();
chb_write(STATIONARY_SHORT_ADDRESS, str, strlen(str));
}

READ_WRITE_FLAG__CMD_IMPL(read_adxl_flag)

Loading

0 comments on commit 4c147d2

Please sign in to comment.