Example #1
0
  /*
   * re-program firmware: this entire function and all subroutines must run from RAM
   * (don't make this static!)
   */
  __attribute__ ((section (".coderam")))
  void flashFirmware(uint8_t* source, uint32_t size){
    __disable_irq(); // Disable ALL interrupts. Can only be executed in Privileged modes.
    setLed(RED);
    eeprom_unlock();
    if(size > (16+16+64+128)*1024){
      eeprom_erase_sector(FLASH_Sector_6);
      toggleLed(); // inline
    }
    if(size > (16+16+64)*1024){
      eeprom_erase_sector(FLASH_Sector_5);
      toggleLed();
    }
    if(size > (16+16)*1024){
      eeprom_erase_sector(FLASH_Sector_4);
      toggleLed();
    }
    if(size > 16*1024){
      eeprom_erase_sector(FLASH_Sector_3);
      toggleLed();
    }
    eeprom_erase_sector(FLASH_Sector_2);
    toggleLed();
    eeprom_write_block(ADDR_FLASH_SECTOR_2, source, size);
    eeprom_lock();
    eeprom_wait();
    NVIC_SystemReset(); // (static inline)
  }

  void programFlashTask(void* p){
    int sector = flashSectorToWrite;
    uint32_t size = flashSizeToWrite;
    uint8_t* source = (uint8_t*)flashAddressToWrite;
    if(sector >= 0 && sector < MAX_USER_PATCHES && size <= 128*1024){
      uint32_t addr = getFlashAddress(sector);
      eeprom_unlock();
      int ret = eeprom_erase(addr);
      if(ret == 0)
	ret = eeprom_write_block(addr, source, size);
      eeprom_lock();
      registry.init();
      if(ret == 0){
	// load and run program
	int pc = registry.getNumberOfPatches()-MAX_USER_PATCHES+sector;
	program.loadProgram(pc);
	// program.loadDynamicProgram(source, size);
	program.resetProgram(false);
      }else{
	setErrorMessage(PROGRAM_ERROR, "Failed to write program to flash");
      }
    }else if(sector == 0xff && size < MAX_SYSEX_FIRMWARE_SIZE){
      flashFirmware(source, size);
    }else{
      setErrorMessage(PROGRAM_ERROR, "Invalid flash program command");
    }
    vTaskDelete(NULL);
  }

  void eraseFlashTask(void* p){
    int sector = flashSectorToWrite;
    if(sector == 0xff){
      for(int i=0; i<MAX_USER_PATCHES; ++i)
	eraseFlashProgram(i);
      settings.clearFlash();
    }else if(sector >= 0 && sector < MAX_USER_PATCHES){
      eraseFlashProgram(sector);
    }else{
      setErrorMessage(PROGRAM_ERROR, "Invalid flash erase command");
    }
    registry.init();
    vTaskDelete(NULL);
  }

  // static int midiMessagesToSend = 0;
  // void sendMidiDataTask(void* p){
  //   switch(midiMessagesToSend){
  //   case SYSEX_PRESET_NAME_COMMAND:
  //     midi.sendPatchNames();
  //     break;
  //   // case 0:
  //   //   midi.sendDeviceInfo();
  //   //   break;
  //   // case SYSEX_PARAMETER_NAME_COMMAND:
  //   //   midi.sendPatchParameterNames();
  //   //   break;
  //   // case SYSEX_FIRMWARE_VERSION:
  //   //   midi.sendFirmwareVersion();
  //   //   break;
  //   // case SYSEX_DEVICE_ID:
  //   //   midi.sendDeviceId();
  //   //   break;
  //   // case SYSEX_DEVICE_STATS:
  //   //   midi.sendDeviceStats();
  //   //   break;
  //   // case SYSEX_PROGRAM_MESSAGE:
  //   //   midi.sendProgramMessage();
  //   //   break;
  //   // case SYSEX_PROGRAM_STATS:
  //   //   midi.sendProgramStats();
  //   //   break;
  //   // case PATCH_BUTTON:
  //   //   midi.sendCc(PATCH_BUTTON, isPushButtonPressed() ? 127 : 0);
  //   //   break;
  //   // case LED:
  //   //   midi.sendCc(LED, getLed() == NONE ? 0 : getLed() == GREEN ? 42 : 84);
  //   //   break;
  //   // case 127:
  //   //   midi.sendSettings();
  //   //   break;
  //   }
  //   midiMessagesToSend = -1;
  //   vTaskDelete(NULL);    
  // }

#ifdef BUTTON_PROGRAM_CHANGE
#ifndef abs
#define abs(x) ((x)>0?(x):-(x))
#endif /* abs */
  void programChangeTask(void* p){
    setLed(RED);
    int pc = settings.program_index;
    int bank = getAnalogValue(0)*5/4096;
    int prog = getAnalogValue(1)*8/4096+1;
    do{
      float a = getAnalogValue(0)*5/4096.0 - 0.5/5;
      float b = getAnalogValue(1)*8/4096.0 - 0.5/8;
      //      if(a - (int)a < 0.8) // deadband each segment: [0.8-1.0)
      if(a > 0 && abs(a - (int)a - 0.1) > 0.2) // deadband each segment: [0.9-1.1]
	bank = (int)a;
      if(b > 0 && abs(b-(int)b - 0.1) > 0.2)
	prog = (int)b+1;
      if(pc != bank*8+prog){
	toggleLed();
	pc = bank*8+prog;
	updateProgramIndex(pc);
	vTaskDelay(20);
      }
    }while(isPushButtonPressed() || pc < 1 || pc >= (int)registry.getNumberOfPatches());
    setLed(RED);
    program.loadProgram(pc);
    program.resetProgram(false);
    for(;;); // wait for program manager to delete this task
  }
Example #2
0
/*
 * This method is called continouusly, i.e. in a while(1) loop. This is
 * where the exciting stuff happens. Well, "exciting" depends on your
 * point of view and level of experience, I guess.
 */
void loop() {
	// Place the cursor in the second line of the display and output the current
	// sensor values. Note that I didn't bother to translate those values to
	// an actual light level and temperature - I only scaled the light sensor's
	// output so it looked nicer on the display.
	// I use hex codes instead of decimal because it's more 31337.
	lcd_setcursor(0,2);
	int ldrValue = map(getAnalogValue(SEN_LDR_PIN), 0x050, 0x3ff, 0x000, 0xfff);
	int trmValue = getAnalogValue(SEN_TRM_PIN);

	sprintf(buffer, "LDR %03x  TRM %03x", ldrValue, trmValue);
	lcd_string(buffer);

	// Show the sensor readings for some time ...
	_delay_ms(TIME_AWAKE);

	// ... then say that we're sleepy ...
	lcd_home();
	lcd_string("Sleep ...");
	_delay_ms(TIME_BEFORE_SLEEP);

	// ... switch off the LCD and put the processor in deep sleep mode.
	lcd_light_state(0);

	set_sleep_mode(SLEEP_MODE_PWR_DOWN);
	sleep_mode();

	// When the button is pressed, the program will resume right here after
	// the interrupt handler has been executed. A good moment for reactivating
	// the LCD.
	lcd_home();
	lcd_string("Wake up! ");
	lcd_light_state(1);
}
Example #3
0
/**
 * Get a scaled sample straight from the channel on this module.
 *
 * The value is scaled to units of Volts using the calibrated scaling data from GetLSBWeight() and GetOffset().
 *
 * @param analog_port_pointer Pointer to the analog port to use.
 * @return A scaled sample straight from the channel on this module.
 */
float getAnalogVoltage(void* analog_port_pointer, int32_t *status) {
  int16_t value = getAnalogValue(analog_port_pointer, status);
  uint32_t LSBWeight = getAnalogLSBWeight(analog_port_pointer, status);
  int32_t offset = getAnalogOffset(analog_port_pointer, status);
  float voltage = LSBWeight * 1.0e-9 * value - offset * 1.0e-9;
  return voltage;
}
Example #4
0
/**
 * Get a sample straight from this channel.
 * The sample is a 12-bit value representing the 0V to 5V range of the A/D
 * converter in the module.
 * The units are in A/D converter codes.  Use GetVoltage() to get the analog
 * value in calibrated units.
 * @return A sample straight from this channel.
 */
int16_t AnalogInput::GetValue() const {
    if (StatusIsFatal()) return 0;
    int32_t status = 0;
    int16_t value = getAnalogValue(m_port, &status);
    wpi_setErrorWithContext(status, getHALErrorMessage(status));
    return value;
}
Example #5
0
/*
 * Class:     edu_wpi_first_wpilibj_hal_AnalogJNI
 * Method:    getAnalogValue
 * Signature: (Ljava/nio/ByteBuffer;Ljava/nio/IntBuffer;)S
 */
JNIEXPORT jshort JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogJNI_getAnalogValue
  (JNIEnv * env, jclass, jobject id, jobject status)
{
	void ** javaId = (void**)env->GetDirectBufferAddress(id);
	//ANALOGJNI_LOG(logDEBUG) << "Analog Ptr = " << *javaId;
	jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
	jshort returnValue = getAnalogValue( *javaId, statusPtr );
	//ANALOGJNI_LOG(logDEBUG) << "Status = " << *statusPtr;
	//ANALOGJNI_LOG(logDEBUG) << "Value = " << returnValue;
	return returnValue;
}
void loop(){
  seq.rotation.update(getAnalogValue(SEQUENCER_ROTATE_CONTROL));
  seq.step.update(getAnalogValue(SEQUENCER_STEP_CONTROL));
  seq.fill.update(getAnalogValue(SEQUENCER_FILL_CONTROL));
  seq.update();

#ifdef SERIAL_DEBUG
  if(serialAvailable() > 0){
    serialRead();
    printString("a: [");
    seq.dump();
    printString("] ");
    seq.print();
    if(clockIsHigh())
      printString(" clock high");
    if(resetIsHigh())
      printString(" reset high");
    printNewline();
  }
#endif
}
Example #7
0
int Multiplexer::getAnalogValueAt(uint8_t num){
	selectPin(num);
	return getAnalogValue();
}
Example #8
0
 //% help=pins/analog-read-pin weight=25
 //% blockId=device_get_analog_pin block="analog read|pin %name" blockGap="8"
 //% name.fieldEditor="gridpicker" name.fieldOptions.columns=4
 //% name.fieldOptions.tooltips="false"
 int analogReadPin(AnalogPin name) {
     PINREAD(getAnalogValue());
 }
inline uint16_t getRepetition(){
  uint16_t value = getAnalogValue(REPCV_ADC_CHANNEL);
  value >>= 8; // range 0 to 15
  value += 1;
  return value;
}
inline uint16_t getMultiplication(){
  uint16_t value = getAnalogValue(MULCV_ADC_CHANNEL);
  value >>= 8; // range of 0 to 15
  value += 1;
  return value;
}
inline uint16_t getDuration(){
  return getAnalogValue(DURCV_ADC_CHANNEL);
  // todo: use StiffValue for hysteresis
}